473,747 Members | 2,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 21722
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*********@gm ail.com> wrote in
news:11******** **************@ g44g2000cwa.goo glegroups.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*********@gm ail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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*********@gm ail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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
2797
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 into this property in its Load event. So, I gave the BaseForm and the property 'abstract' modifier, and put the implementation of the property in the inherited class; say MyForm. However, when I did this, I no longer can open MyForm in the design...
8
2264
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 then instantiation ? Many Thanks Vishal Gandhi
16
3522
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?) What about abstract classes? Should abstract classes have a destructor and or constructor? What about pure abstract classes?
1
12379
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 Base(); or just Base base;. But when you instansiate an object of class Derived the constructor in Base is called implicitly.
2
9377
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 that I'd like to manipulate some of the derived controls using the VS.NET designer. However, when the base class is made abstract, I get the error "The designer must create an instance of type <...> but it cannot because the type is declared as...
9
7558
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 be implemented. So if you have a class which inherits base class that inherts an interface, then your classes will have a standard. I suppose you can also check for interface at run time say when dll is loaded and see if it implememts whats...
7
1805
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 such that i have to define data members in class LogBuffer.i.e ************************************************************************************ class LogBuffer
0
2831
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 magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g., forgetting to implement a method required by the given interface). This is handy when unit tests or...
5
3018
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 C# even has Iinterfaces. I took some Java programming classes and Interfaces are a main staple of Java. And in VB.Net I use interfaces for setting up definitions of classes. I am guessing that Abstract classes in C# do the same thing as...
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9522
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9355
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9307
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8234
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.