Why an Abstract Base Class cannot be instantiated ?

Discussion in 'C++' started by Dev, Oct 9, 2005.

  1. Dev

    Dev Guest

    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
    Dev, Oct 9, 2005
    #1
    1. Advertising

  2. 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.
    Jacques Labuschagne, Oct 9, 2005
    #2
    1. Advertising

  3. Dev

    Dev Guest

    I am trying to understand the instantiation from a compiler author
    perspective.

    Dev.
    Dev, Oct 9, 2005
    #3
  4. 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.
    Jacques Labuschagne, Oct 9, 2005
    #4
  5. Dev

    Dev Guest

    virtual f()=0;
    is in effect suggesting a null function pointer.

    Is this all or is there something more to this ?

    Dev.
    Dev, Oct 9, 2005
    #5
  6. Dev

    Greg Guest

    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
    Greg, Oct 9, 2005
    #6
  7. Dev

    rookkey Guest

    "Dev" <> wrote in
    news::

    > 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.
    rookkey, Oct 9, 2005
    #7
  8. Dev

    Cy Edmunds Guest

    "Dev" <> wrote in message
    news:...
    > 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/
    Cy Edmunds, Oct 9, 2005
    #8
  9. Dev

    Greg Guest

    Cy Edmunds wrote:
    > "Dev" <> wrote in message
    > news:...
    > > 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
    Greg, Oct 9, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    416
    R.F. Pels
    May 22, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,736
    Smokey Grindel
    Dec 2, 2006
  3. Alf P. Steinbach
    Replies:
    6
    Views:
    530
    John Carson
    Sep 3, 2005
  4. Replies:
    4
    Views:
    797
    Rolf Magnus
    May 17, 2006
  5. Replies:
    2
    Views:
    284
    Pavel Lepin
    Jul 5, 2007
Loading...

Share This Page