Virtual Constructors

T

Thomas Matthews

Hi,

Is placing the keyword "virtual" in front of a constructor
allowed as in the sample below?
class TTable
{
virtual TTable();
};

My compiler, Borland Builder 5.2, has system libraries
with this syntax.

My understanding is that the virtual constructor is an
idiom in which a virtual member function is called by
the constructor. I didn't think that one could declare
a constructor as virtual.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

Jeff Schwab

Thomas said:
Hi,

Is placing the keyword "virtual" in front of a constructor
allowed as in the sample below?
No.

class TTable
{
virtual TTable();
};

My compiler, Borland Builder 5.2, has system libraries
with this syntax.

My understanding is that the virtual constructor is an
idiom in which a virtual member function is called by
the constructor. I didn't think that one could declare
a constructor as virtual.
 
R

Ron Natalie

Thomas Matthews said:
Hi,

Is placing the keyword "virtual" in front of a constructor
allowed as in the sample below?

Of course not. What would it do? You sure it wasn't the destructor?
Either that or it is some goofy Borland extension
 
C

Cy Edmunds

Thomas Matthews said:
Hi,

Is placing the keyword "virtual" in front of a constructor
allowed as in the sample below?
class TTable
{
virtual TTable();
};

My compiler, Borland Builder 5.2, has system libraries
with this syntax.

My understanding is that the virtual constructor is an
idiom in which a virtual member function is called by
the constructor. I didn't think that one could declare
a constructor as virtual.

You were right.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

A constructor can't be virtual for the simple reason that when you are
initializing an object you must know what the object is. However you can
generate an object without knowing what type it is by a using a virtual
function:

class Base
{
public:
virtual Base *clone() const = 0;
virtual ~Base() {}
};

class Derived : public Base
{
public:
...
virtual Base *clone() const {return new Derived(*this);}
};

A member function like clone() is sometimes called a virtual constructor.
 
T

Thomas Matthews

Ron said:
Of course not. What would it do? You sure it wasn't the destructor?
Either that or it is some goofy Borland extension

Here is the code from the header file (Include\vcl\dbtables.hpp):
class DELPHICLASS TTable;
class PASCALIMPLEMENTATION TTable : public TDBDataSet
{
// snip -- irrelevant declarations

public:
__fastcall virtual TTable(Classes::TComponent* AOwner);
__fastcall virtual ~TTable(void);

// snip -- more declarations
};

The above was cut and pasted directly from the file.

I got a compilation error when I tried to create a descendant of
TTable using a normal constructor. The error went away when I
prefixed the declaration with "virtual" as the declaration above.

This compiler would be so much better without all this
nonstandard crap.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

John Carson

Thomas Matthews said:
Hi,

Is placing the keyword "virtual" in front of a constructor
allowed as in the sample below?
class TTable
{
virtual TTable();
};

My compiler, Borland Builder 5.2, has system libraries
with this syntax.

My understanding is that the virtual constructor is an
idiom in which a virtual member function is called by
the constructor. I didn't think that one could declare
a constructor as virtual.

You can't have a virtual constructor. See Stroustrup TC++PL, p. 424-5.
 
D

Darko Miletic

Here is the code from the header file (Include\vcl\dbtables.hpp):
class DELPHICLASS TTable;
class PASCALIMPLEMENTATION TTable : public TDBDataSet
{
// snip -- irrelevant declarations

public:
__fastcall virtual TTable(Classes::TComponent* AOwner);
__fastcall virtual ~TTable(void);

// snip -- more declarations
};


Here is an excerpt from C++ Builder help:

"The delphiclass argument is used for declarations for classes derived
from TObject. These classes will be created with the following VCL
compatibility:

VCL-compatible RTTI
VCL-compatible constructor/destructor behavior
VCL-compatible exception handling

A VCL-compatible class has the following restrictions.

No virtual base classes are allowed.
No multiple inheritance is allowed.
Must be dynamically allocated by using the global new operator.
Must have a destructor.
Copy constructors and assignment operators are not compiler-generated
for VCL-derived classes.

A class declaration that is translated from Object Pascal will need
this modifier if the compiler needs to know that the class is derived
from TObject."

So in short this is special extension that enables C++ Builder to use
VCL which, of course, is written in Delphi and is not intended to be
used in any other way.
This compiler would be so much better without all this
nonstandard crap.

You should not use this extension as it is related to VCL only. And
without that "nonstandard crap" there would be no easy VCL usage. And
without that C++ Builder would not be very usable.

Darko
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top