473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Why an Abstract Base Class cannot be instantiated ?

Dev
Hello,

Why an Abstract Base Class cannot be instantiated ?

Does anybody know of the object construction internals ?

What is the missing information that prevents the construction ?

TIA.
Dev

Oct 9 '05 #1
8 21697
Dev wrote:
Hello,

Why an Abstract Base Class cannot be instantiated ?

Does anybody know of the object construction internals ?

What is the missing information that prevents the construction ?

TIA.
Dev


An abstract base class is one with pure virtual functions, i.e. it
defines an interface and not a complete implementation. You can't
instantiate it because some of its functions don't have definitions.
What would you expect to happen if you could call one of these
non-existant functions?

If you find yourself trying to instantiate an ABC then there's probably
something wrong with your design.

Jacques.
Oct 9 '05 #2
Dev
I am trying to understand the instantiation from a compiler author
perspective.

Dev.

Oct 9 '05 #3
Dev wrote:
I am trying to understand the instantiation from a compiler author
perspective.


My answer stands. The instantiation would not be meaningful because the
object has functions missing. I've never written a compiler, but I
imagine that you'd run into problems trying to populate the virtual
function table with pointers to functions that don't exist. Even if you
could do it, it would be useless to the user.

Jacques.
Oct 9 '05 #4
Dev
virtual f()=0;
is in effect suggesting a null function pointer.

Is this all or is there something more to this ?

Dev.

Oct 9 '05 #5
Jacques Labuschagne wrote:
Dev wrote:
I am trying to understand the instantiation from a compiler author
perspective.


My answer stands. The instantiation would not be meaningful because the
object has functions missing. I've never written a compiler, but I
imagine that you'd run into problems trying to populate the virtual
function table with pointers to functions that don't exist. Even if you
could do it, it would be useless to the user.

Jacques.


But even when the abstract class provides definitions for each of its
pure virtual functions, the class still cannot be directly
instantiated. There really is no compelling technical reason why
abstract classes could not be instantiated; after the language rules
could simply be adjusted and have the compiler provide an empty
definition for a pure virtual function - in much the same way that the
compiler provides a default constructor when needed. Instead, the
prohibition against instantiating an abstract class is a deliberate
feature of the language. An abstract class cannot be instantiated
because the definition of an abstract class is one that cannot be
instantiated.

In C++, abstract classes help keep separate class interface from
implementation. An abstract class need specify only an interface.
Because it cannot be instantiated, it has no need no provide any
implementation of its own. Instead the pure virtual mechanism forces
subclasses to provide part or all of the implementation. By keeping the
two separate, the resulting code is more portable, has few dependencies
and is overall "cleaner" than one in which the interface and
implementation are tightly intertwined.

Greg

Oct 9 '05 #6
"Dev" <de*********@gmail.com> wrote in
news:11**********************@g44g2000cwa.googlegr oups.com:
virtual f()=0;
is in effect suggesting a null function pointer.


The "=0" hanging off the end of the function declaration is just a
syntactical peculiarity that says a given function is "pure" virtual.

Bjarne Stroustroup in his "The Design and Evolution of C++" book says that
"=0" was chosen as the way to declare pure virtual functions because the
C++ language designers did not want to go through the pain of introducing a
new keyword (such as "pure") to the language. If a new keyword was
introduced, it would break all the existing C++ code that uses that
identifier for its variables, class names and the like. Using "=0" to
declare such functions would not break any previously legal code.

So, the "=0" syntax has nothing to do with "null functions" or nullness at
all. It's just another one of those compatibility quirks that give C++ its
spicy flavor.
Oct 9 '05 #7
"Dev" <de*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hello,

Why an Abstract Base Class cannot be instantiated ?

Does anybody know of the object construction internals ?

What is the missing information that prevents the construction ?

TIA.
Dev


Actually, an ABC can be instantiated. You just can't declare a variable of
that type. Consider:

class base
{
public:
base() {/* ABC "base" is instantiated at this point */}
virtual void pure() = 0;
virtual ~base() {}
};

class derived : public base
{
public:
virual void pure() {}
};

int main()
{
derived d;
}

Note that within base::base() the object is truly a "base", not a "derived".
Just don't try to call base::pure() from base::base().

This may sound like a nit pick, but it bears on your point. Given that ABC's
are actually instantiated as part of the construction process, it is clear
that the language could have allowed you to declare ABC variables. However,
I'm glad it didn't; such usage would have been very dangerous.

--
Cy
http://home.rochester.rr.com/cyhome/
Oct 9 '05 #8
Cy Edmunds wrote:
"Dev" <de*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hello,

Why an Abstract Base Class cannot be instantiated ?

Does anybody know of the object construction internals ?

What is the missing information that prevents the construction ?

TIA.
Dev

Actually, an ABC can be instantiated. You just can't declare a variable of
that type. Consider:


I don't think there is much of a distinction to be made here. Declaring
a variable of a class type instantiates an instance of that class type.

class base
{
public:
base() {/* ABC "base" is instantiated at this point */}
The base class constructor initializes - but does not instantiate - an
instance of the class base. The instantiation of this instance would
had to have occured earlier in the program's execution in order for the
object to be under construction and be executing this code.
virtual void pure() = 0;
virtual ~base() {}
};

class derived : public base
{
public:
virual void pure() {}
};

int main()
{
derived d;
}

Note that within base::base() the object is truly a "base", not a "derived".
Just don't try to call base::pure() from base::base().
It's perfectly legal to call base::pure from base::base, but doing so
imposes a requirement upon the program. The Standard states that if a
pure virtual function is explicitly called within a program, than that
program must implement the pure virtual method that was called. So in
this case, should base::base call base::pure, the program would then be
obligated to provide an implementation for base::pure (which may simply
do nothing) in order to ensure that the call to the pure virtual method
invokes an actual, defined method.

This may sound like a nit pick, but it bears on your point. Given that ABC's
are actually instantiated as part of the construction process, it is clear
that the language could have allowed you to declare ABC variables. However,
I'm glad it didn't; such usage would have been very dangerous.


It's prefectly legal to declare a pointer to an abstract class and then
call pure virtual methods through the pointer. But as I just pointed
out, doing so forces the abstract class to implement whatever pure
virtual methods are being called directly. But whether its virtual
methods are defined or not, the class itself remains abstract and, as
such, cannot be directly instantiated.

Greg

Oct 9 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Yasutaka Ito | last post by:
Hi folks! I have a BaseForm class that inherits System.Windows.Forms.Form. It has a property, whose value I need supplied by the class that inherits it. The BaseForm usees the value supplied...
8
by: Vishal Gandhi | last post by:
Hi , Please help me by advising an real life scenario where Abstract Classes should be used over Interfaces or vice versa . Whats the basic difference between Abstract Class and interface other...
16
by: Merlin | last post by:
Hi Been reading the GOF book and started to make the distinction between Class and Interface inheritance. One question though: Do pure abstract classes have representations? (data members?)...
1
by: Tony Johansson | last post by:
Hello!! Assume you have two classes one class called Base which is an abstract class and one derived class called Derived. You are not allowed to create an object of class Base like new...
2
by: Joe Vrba | last post by:
I'm building a family of components derived from UserControl. There's an abstract base class to ensure basic functionality and then numerous other controls derived from that. The problem is...
9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
7
by: ankitjain.bvcoe | last post by:
Hi i have the following problem in my design :::: i want to define an abstract class LogBuffer and derive two singleton classes from it i.e AlarmBuffer and FireWallBuffer.For this my design is...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.