 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
ypjofficial@indiatimes.co Guest
|
Posted: Thu Jan 19, 2006 12:06 pm Post subject: paradox when constructor of an pure abstract base class call |
|
|
Hello All,
I have following doubt..
class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};
class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};
void main()
{
concreteclass c;
}
now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
or to put in another way is there any paradox when the constructor of
an abstract class gets called?
Thanks and Regards,
Yogesh Joshi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jan 19, 2006 2:32 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
[email]ypjofficial (AT) indiatimes (DOT) com[/email] wrote:
| Quote: | I have following doubt..
class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};
class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};
void main()
|
int main()
| Quote: | {
concreteclass c;
}
now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
|
We can, only as a subobject of another object. We can't create
a _stand-alone_ object of abstract class.
| Quote: | or to put in another way is there any paradox when the constructor of
an abstract class gets called?
|
No.
V
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Thu Jan 19, 2006 2:59 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
* [email]ypjofficial (AT) indiatimes (DOT) com[/email]:
| Quote: |
class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};
class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};
void main()
{
concreteclass c;
}
|
'main' must have result type 'int'.
If your compiler accepts the above, then it's non-conforming in this
respect.
| Quote: | now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
or to put in another way is there any paradox when the constructor of
an abstract class gets called?
|
The reason C++ forbids you to instantiate an abstract class on its own
is that it's not meaningful: an abstract class _depends_ on its pure
virtual functions to do the crucial things (that's why it's abstract).
As a base class part of another object those pure virtual functions are
defined, by the derived class, i.e. you have totally different
situation, and that's the whole point, the way it's meant to be used.
However, there is a possibility of erronously calling a pure virtual
function from the abstract class constructor, via some other member
function. The result of that is undefined behavior, but most likely it
will be detected and cause a crash. It simply means that static
(compile-time) type checking and rules based on such checking can not
protect against all run-time errors, which we knew anyway.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gavin Deane Guest
|
Posted: Thu Jan 19, 2006 2:59 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
[email]ypjofficial (AT) indiatimes (DOT) com[/email] wrote:
| Quote: | Hello All,
I have following doubt..
class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};
class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};
void main()
|
This should be int main() by the way.
| Quote: | {
concreteclass c;
}
now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
or to put in another way is there any paradox when the constructor of
an abstract class gets called?
|
Your premise is that it is impossible, by definition, to create an
instance of an abstract class. This leads you think that you have a
logical paradox when an instance of abstractclass is created within
concreteclass. However, your premise is wrong.
| Quote: | From 10.4/2
An abstract class is a class that can be used only as a base class of |
some other class; no objects of an abstract class can be created except
as subobjects of a class derived from it.
Couldn't be clearer. If you apply logic to an incorrect premise, you
shouldn't be surprised if the conclusion you reach doesn't make sense.
Gavin Deane
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Earl Purple Guest
|
Posted: Thu Jan 19, 2006 2:59 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
[email]ypjofficial (AT) indiatimes (DOT) com[/email] wrote:
| Quote: | Hello All,
I have following doubt..
class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};
class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};
void main()
{
concreteclass c;
}
now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
or to put in another way is there any paradox when the constructor of
an abstract class gets called?
|
Just because its constructor gets called doesn't mean you have an
instance of it.
And main should return int.
And you should really give your abstract base class a virtual
destructor in case anyone holds a pointer to one created with new.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Thu Jan 19, 2006 4:07 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
In article <1137617635.272973.281170 (AT) g44g2000cwa (DOT) googlegroups.com>,
[email]ypjofficial (AT) indiatimes (DOT) com[/email] wrote:
| Quote: | Hello All,
I have following doubt..
class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};
class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};
void main()
{
concreteclass c;
}
now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
or to put in another way is there any paradox when the constructor of
an abstract class gets called?
|
Yes, there is. For example if 'method()' (or any other virtual
member-function) is called from within the abstractclass c_tor.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Zara Guest
|
Posted: Thu Jan 19, 2006 4:07 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
On 19 Jan 2006 07:06:01 -0500, [email]ypjofficial (AT) indiatimes (DOT) com[/email] wrote:
| Quote: | Hello All,
I have following doubt..
class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};
class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};
void main()
{
concreteclass c;
}
now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
or to put in another way is there any paradox when the constructor of
an abstract class gets called?
Thanks and Regards,
Yogesh Joshi
|
The only "paradox" you may speak of, is that from the moment that
abstractclass object is completely constructed till the moment that
concreteclass is completely constructed, you may try to call method()
with undefined results.
Regards,
Zara
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Thu Jan 19, 2006 4:10 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
[email]ypjofficial (AT) indiatimes (DOT) com[/email] wrote:
| Quote: | Hello All,
I have following doubt..
class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};
class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};
void main()
|
int main()
See http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3
| Quote: | {
concreteclass c;
}
now when I create the object of concreteclass, the constructor of
abstractclass will be called.
|
Correct.
| Quote: | and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
|
Because the derived class is a kind of the base class. Think of the
classic example with Shape and Circle classes. Shape is an abstract
concept that doesn't make sense to instantiate on its own (what would
the area of a Shape be?). But when you instantiate a Circle, you have
instantiated a kind of Shape, and the area can be calculated according
to the concrete class' implementation.
| Quote: | or to put in another way is there any paradox when the constructor of
an abstract class gets called?
|
Nope.
Cheers! --M
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Thu Jan 19, 2006 6:46 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
Earl Purple wrote:
| Quote: | And you should really give your abstract base class a virtual
destructor in case anyone holds a pointer to one created with new.
|
Base classes should have virtual destructors only if their design calls
for deleting objects of derived types through pointers to the base.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
v.manojkumar@gmail.com Guest
|
Posted: Thu Jan 19, 2006 6:47 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
Hi,
| Quote: | the constructor of abstractclass will be called.
This is absoulutly right. Understand the "ISA" rule... the |
derived class is
a base class+ something extra.
| Quote: | and as per the logic constructors are
called while creating the object,object of abstractclass is being
created.. so how come we can create an object of a abstract class?
|
Now this isnt creaing a abstract object... this is creating the
base part
of the derived object which eventually happens to be abstract.
The basic logic is if a pure virtual function is present... then if the
compiler
allows creation of objects then... the function call to that fn is
undefined...
--
manoj
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Fri Jan 20, 2006 12:08 am Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
[email]ypjofficial (AT) indiatimes (DOT) com[/email] wrote:
| Quote: | I have following doubt..
class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};
class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};
void main()
{
concreteclass c;
}
now when I create the object of concreteclass, the constructor
of abstractclass will be called.and as per the logic
constructors are called while creating the object,object of
abstractclass is being created.. so how come we can create an
object of a abstract class? or to put in another way is there
any paradox when the constructor of an abstract class gets
called?
|
Sort of. During the construction of abstractclass, the dynamic
type of the object under construction is abstractclass -- it's
the most derived type.
I think that the answer is that you don't have a complete object
until all of the constructors have run. And the restriction is
that you cannot have a complete object of type abstractclass.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kamil.pawlowski@gmail.com Guest
|
Posted: Fri Jan 20, 2006 12:09 am Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
For proper destructor semantics, isn't the abstract class required to
declare a virtual destructor as well?
For example this code:
#include <stdio.h>
class A {
public:
A(){};
virtual void foo() = 0;
//virtual ~A(){ printf("~An"); }
};
class B : public A {
public:
B() : A() {}
virtual void foo(){ printf("foo!n"); }
virtual ~B(){ printf("~Bn"); }
};
int main(int argc, char** argv){
A* a = dynamic_cast<A*>( new B() );
a->foo();
delete a;
}
will print:
$ ./a.exe
foo!
Where as commenting back in:
virtual ~A(){ printf("~An"); }
will lead to the proper
$ ./a.exe
foo!
~B
~A
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Jan 20, 2006 12:15 am Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
Pete Becker wrote:
| Quote: | Earl Purple wrote:
And you should really give your abstract base class a virtual
destructor in case anyone holds a pointer to one created with new.
Base classes should have virtual destructors only if their design calls
for deleting objects of derived types through pointers to the base.
|
That's generally true. But is there any _real_ harm in declaring the
destructor virtual if there is already virtual functions present? I will
stipulate that in some rare cases growing the virtual function table by
one entry could take it over the top (the straw that broke the camel's
back), but that instance is unlikely, no?
Or a related topic:
I vaguely remember some conversation here or in c.l.c++.m, or in c.s.c++,
to make the compiler-provided destructor virtual by if there is at least
one virtual function present, has there been any resolution on it?
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Eugene Kalenkovich Guest
|
Posted: Fri Jan 20, 2006 12:43 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
"Pete Becker" <petebecker (AT) acm (DOT) org> wrote
| Quote: | Earl Purple wrote:
And you should really give your abstract base class a virtual
destructor in case anyone holds a pointer to one created with new.
Base classes should have virtual destructors only if their design calls
for deleting objects of derived types through pointers to the base.
|
Presence of virtual functions in class makes this call pretty loud...
-- EK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gavin Deane Guest
|
Posted: Fri Jan 20, 2006 12:51 pm Post subject: Re: paradox when constructor of an pure abstract base class |
|
|
[email]kamil.pawlowski (AT) gmail (DOT) com[/email] wrote:
| Quote: | For proper destructor semantics, isn't the abstract class required to
declare a virtual destructor as well?
|
You must have a virtual destructor *if* you intend to delete a derived
class object via a pointer to the base class, as you did in your code
example.
But the use of an abstract class does not necessarily imply polymorphic
deletion is going to happen. For example, the OP's code was correct in
this respect without a virtual destructor. So the answer to your
question is: No, the abstract class is not *required* to declare a
virtual destructor.
In practice, I'm sure the majority of abstract classes do have virtual
destructors because they are (or could in the future be) deleted
polymorphically.
Gavin Deane
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|