Link to home
Start Free TrialLog in
Avatar of Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux)Flag for India

asked on

Why Obj. creation is not possible from an Abstract class

Why Object creation of abstract class is not possible.

In my opinion, It's bcz of the pure virtual function. Suppose object will be created for abstract class. Then how it'll deal with that pure virtual function.Most people says pure virtual function doesn't have any body but in reality it can have body.But once getting a pure virtual func. our compiler will deny to create object.The invention of pure virtual function is only to create abstract class / Interface which prevents object creation.

Is there anything else I'm missing? Is there any hidden thing lies inside the object/VTABLE/VPTR/memory ...etc.
Avatar of Didier Vx
Didier Vx
Flag of France image

"Any class with one or more pure virtual functions is an abstract class, and it is illegal to instantiate an object of it. Trying to do so will cause a compile-time error. Putting a virtual function in your class signals two things to clients of your class:
*Don't make an object of this class, derive from it.
*Make sure you override the pure virtual function."

http://www.cplusplus.com/forum/general/4153/
Avatar of Subrat (C++ windows/Linux)

ASKER

>>it is illegal to instantiate an object of it
Why it's illegal? What's the mechanism behind it.
Please read my original Question and descriptions carefully.
The definition of an abstract class implies (and in some cases explicitly states) that it cannot be instantiated.

If you want to be able to instantiate your class, then don't make it abstract. If you want the class to describe/enforce an interface which other classes have to implement, then make it abstract.
Hi Infinity,

I know this. But why I'm asking this Q, bcz in one interview, I got this Q. and my ans. was as mentioned in my description of original Q. But I found the interviewer was not satisfied with it. So I thought there might be anything else I'm missing. So I asked it here. and I hope you, Evilrix, Jkr, and others may have the perfect answer.
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to add to above comments:

there is not really a technical reason for abstract classes are not being allowed to get instantiated (though of course it would crash if a pure virtual function without implementation would be called for a baseclass object). it is more the only purpose of making pure virtual functions to not allow baseclass-only instances and to force derived classes to either be abstract as well or provide an implementation for the pure virtual functions.

Sara
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lol at "pointless futility" ;)
evilrix, the compiler also refuses instantiation if you would add an implementation for the pure virtual function. but in that case the futility wouldn't be so much pointless, right?

Sara

>>  the compiler also refuses instantiation if you would add an implementation for the pure virtual function. but in that case the futility wouldn't be so much pointless, right?

Well, no -- because it is no longer abstract then is it!?. Sorry, did I miss something?

If we go back to my analogy, that would be like adding the ability to start/stop... so the engine is then not such a "pointless futility".

Please tell me if you disagree with anything I have said, I'd be glad to simplify it further for you.
>> Well, no

no, meaning -- assuming there are no other pure virtual functions of course (otherwise it's still abstract and the compiler will still get pretty grumpy with you).

I'm still not really sure what your point was :)
a pure virtual function (means declared with = 0) nevertheless can have a function body. that function could be called for example from the derived classes and may provide a baseclass functionality.

the function would be pure virtual and the class would be abstract despite of that implementation.

Sara
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it is pointless not because of a possible poor or incomplete baseclass implementation but cause the = 0 explicitly tells that only derived classes with an implementation of the function can be instantiated.

as infinity08 told, if you want instances of the baseclass don't make it abstract (don't add pure virtual functions) !

Sara
>> it is pointless not because of a possible poor or incomplete baseclass implementation but cause the = 0

Agreed... but the asker is trying to understand why this is the case. The standard defines it as not possible but why? And the reason is because it makes no sense. And why is this? Because the very fact it's abstract means it is an incomplete definition.
Thanks to all.
ID:36526697 should be assisted soln.
ID:36523027 should be Accepted Soln.
Reason:
When a class is abstract you can think of the pure virtual functions as having an address of 0 in the v-table. That's more of less what virtual void foo() = 0 means even if that pure virtual func. having body.