Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > paradox when constructor of an pure abstract base class called?

Reply
Thread Tools

paradox when constructor of an pure abstract base class called?

 
 
ypjofficial@indiatimes.com
Guest
Posts: n/a
 
      01-19-2006
Hello All,

I have following doubt..

class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};

class concreteclassublic 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! ]

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-19-2006
wrote:
> I have following doubt..
>
> class abstractclass
> {
> public:
> abstractclass(){}
> virtual void method()=0;
> };
>
> class concreteclassublic abstractclass
> {
> public:
> concreteclass(){}
> void method(){}
> };
>
> void main()


int 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?


We can, only as a subobject of another object. We can't create
a _stand-alone_ object of abstract class.

> or to put in another way is there any paradox when the constructor of
> an abstract class gets called?


No.

V
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-19-2006
* :
>
> class abstractclass
> {
> public:
> abstractclass(){}
> virtual void method()=0;
> };
>
> class concreteclassublic 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.


> 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! ]

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      01-19-2006

wrote:
> Hello All,
>
> I have following doubt..
>
> class abstractclass
> {
> public:
> abstractclass(){}
> virtual void method()=0;
> };
>
> class concreteclassublic abstractclass
> {
> public:
> concreteclass(){}
> void method(){}
> };
>
> void main()


This should be int main() by the way.

> {
>
> 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.

>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! ]

 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      01-19-2006

wrote:
> Hello All,
>
> I have following doubt..
>
> class abstractclass
> {
> public:
> abstractclass(){}
> virtual void method()=0;
> };
>
> class concreteclassublic 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! ]

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      01-19-2006
In article < .com>,
wrote:

> Hello All,
>
> I have following doubt..
>
> class abstractclass
> {
> public:
> abstractclass(){}
> virtual void method()=0;
> };
>
> class concreteclassublic 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! ]

 
Reply With Quote
 
Zara
Guest
Posts: n/a
 
      01-19-2006
On 19 Jan 2006 07:06:01 -0500, wrote:

>Hello All,
>
>I have following doubt..
>
>class abstractclass
>{
>public:
>abstractclass(){}
>virtual void method()=0;
>};
>
>class concreteclassublic 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! ]

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      01-19-2006
wrote:
> Hello All,
>
> I have following doubt..
>
> class abstractclass
> {
> public:
> abstractclass(){}
> virtual void method()=0;
> };
>
> class concreteclassublic abstractclass
> {
> public:
> concreteclass(){}
> void method(){}
> };
>
> void main()


int main()

See http://www.parashift.com/c++-faq-lit....html#faq-29.3

> {
>
> concreteclass c;
> }
>
> now when I create the object of concreteclass, the constructor of
> abstractclass will be called.


Correct.

> 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.

> 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! ]

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-19-2006
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.

--

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! ]

 
Reply With Quote
 
v.manojkumar@gmail.com
Guest
Posts: n/a
 
      01-19-2006
Hi,


> the constructor of abstractclass will be called.

This is absoulutly right. Understand the "ISA" rule... the
derived class is
a base class+ something extra.

>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! ]

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Abstract base class with pure virtual functions Arne Schmitz C++ 4 01-17-2007 06:16 PM
what is the difference between abstract class and pure abstract class? skishorev@yahoo.co.in C++ 4 05-17-2006 08:07 AM
calling pure virtual in abstract class's constructor vsgdp C++ 7 09-25-2005 06:42 PM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM



Advertisments