473,398 Members | 2,113 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,398 software developers and data experts.

Virtual Constructors

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

Jul 22 '05 #1
6 2589
Thomas Matthews wrote:
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.


Jul 22 '05 #2

"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in message news:3F************@sbcglobal.net...
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
Jul 22 '05 #3
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:3F************@sbcglobal.net...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4
Ron Natalie wrote:
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in message news:3F************@sbcglobal.net...
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


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

Jul 22 '05 #5
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:3F************@sbcglobal.net
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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #6
On Fri, 26 Dec 2003 19:58:53 GMT, Thomas Matthews
<Th*************************@sbcglobal.net> wrote:
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
Jul 22 '05 #7

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

Similar topics

0
by: Alexander Stippler | last post by:
I've got an inheritance structure with two coupled "dreaded diamonds" like shown below: A / \ / \ B C \ / \ \ / \ D E \ /
3
by: Gianni Mariani | last post by:
In the code below, controller::controller() is never invoked, however, it appears there is no way to make a compile-time rule that this should not happen. The code below seems to make compilers...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
7
by: vaividhya | last post by:
We can have virtual destructors.Why we can't have virtual constructors?
5
by: Alok | last post by:
hii Would somebody clear my doubts on following qustions . What are virtual constructors and virtual destructors ? Why can't we have virtual constructors ? What are demerits of inheritence in...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
6
by: Carl R. Davies | last post by:
I was reading this link http://www.icce.rug.nl/documents/cplusplus/cplusplus14.html#l198 heading "14.10 Virtual Constructors" I am struggling to understand the issue the author is trying to...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
8
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
4
by: Rahul | last post by:
Hi Everyone, I understand that the constructors can't be virtual and parashift has the following example, to have an workaround for the constructors to be virtual, class Shape { public:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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,...
0
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...

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.