Solved

Why Obj. creation is not possible from an Abstract class

Posted on 2011-09-11
17
314 Views
Last Modified: 2012-05-12
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.
0
Comment
  • 5
  • 4
  • 4
  • +2
17 Comments
 
LVL 5

Expert Comment

by:Didier Vally
ID: 36520816
"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/
0
 
LVL 8

Author Comment

by:Subrat (C++ windows/Linux)
ID: 36522128
>>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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 36522203
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.
0
 
LVL 8

Author Comment

by:Subrat (C++ windows/Linux)
ID: 36522345
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.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 36522442
If the question is why instantiation isn't allowed for abstract classes, then the answer is that that's according to the definition of the term "abstract class". It doesn't make sense to want to instantiate an abstract class, because that wouldn't mean anything (not anything useful anyway).

If the person asking you the question isn't satisfied by that answer, then you either misunderstood the question, or the person wasn't clear about what he was actually asking about. Either way, it's up to the person to clarify :)

Practically, if the compiler would allow you to instantiate an abstract class, then you'd get in trouble when trying to call a pure virtual function, as you correctly mentioned yourself.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 36522687
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
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 400 total points
ID: 36523027
>> Why it's illegal? What's the mechanism behind it.
Consider it this way...

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 -- the implementation detail is compiler specific but thinking of it this way might make things make a little more sense.

Trying to invoke that would make no sense (what would it do other than blow up?) so, basically, you can't and -- thus -- you can't create a class that would allow you to do this. The compiler is preventing you doing something completely pointless (and dangerous)

Put it another way... it would be like trying to create an engine without a method of starting or stopping it. It would make no sense. It would be pointless. The compiler (because this is what the C++ standard defines -- as already noted by Infinity08 above); therefore, don't let you do this as it would be an act of pointless futility.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 36523067
lol at "pointless futility" ;)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

Expert Comment

by:sarabande
ID: 36523128
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

0
 
LVL 40

Expert Comment

by:evilrix
ID: 36523715
>>  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.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36523735
>> 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 :)
0
 
LVL 32

Expert Comment

by:sarabande
ID: 36523929
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
0
 
LVL 40

Accepted Solution

by:
evilrix earned 400 total points
ID: 36526697
Ah. Right... I understand what you mean now (I think we got a little lost in translation) :)

Yes, in the case where a pure-virtual has an implementation, it is still abstract and cannot be called because it is still considered to be abstract. In that case the pure-virtual function is intended only to be called from a derived implementation that implements the concrete function.

>> but in that case the futility wouldn't be so much pointless, right?

Well, still yes :)

The general reason for implementing an abstract function in the abstract class is generally to provide some common behaviour that the concrete function may (or possible should) call but by itself is still meaningless.

Going back to my motor example, the abstract implementation might perform some pre-checks on the engine before it is started. We might have different ways to start the engine but in general these pre-checks need doing. The concrete implementations for different ways of starting the engine may call the abstract implementation to do this for them, but those checks by themselves are pretty meaningless if the engine doesn't have a mechanism to start it.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 36528811
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
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36528846
>> 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.
0
 
LVL 8

Author Closing Comment

by:Subrat (C++ windows/Linux)
ID: 36529495
Thanks to all.
0
 
LVL 8

Author Comment

by:Subrat (C++ windows/Linux)
ID: 36529515
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.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Before You Read The Article Please make sure you understand these two concepts: Variable Scope (http://www.php.net/manual/en/language.variables.scope.php) and Property Visibility (http://www.php.net/manual/en/language.oop5.visibility.php).  And to …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now