473,785 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.l earn.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 2635
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************ *************@s bcglobal.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************ *************@s bcglobal.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.l earn.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************ *************@s bcglobal.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\db tables.hpp):
class DELPHICLASS TTable;
class PASCALIMPLEMENT ATION 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.l earn.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************ *************@s bcglobal.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************ *************@s bcglobal.net> wrote:
Here is the code from the header file (Include\vcl\db tables.hpp):
class DELPHICLASS TTable;
class PASCALIMPLEMENT ATION 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 "nonstandar d 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
4234
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
1477
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 complain that controller::controller() is private, even though it is never used. What do others do to work-around this ? I suppose I can simply not implement controller::controller(), that way I get a linking error if there exists errant code....
26
3986
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 add a virtual destructor like this : " virtual ~vechile". I get this error: Undefined first referenced symbol in file vtable for vechile /var/tmp//ccC9yD6Z.o
7
2564
by: vaividhya | last post by:
We can have virtual destructors.Why we can't have virtual constructors?
5
3782
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 C++ ? waitng for the answers.
7
3092
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 item 7 for why this is virtual ...
6
616
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 solve. This paragraph is confusing me: "As we have seen (section 14.2) C++ supports virtual destructors. Like many other object oriented languages (e.g., Java), however, the notion of a virtual constructor is not supported. The absence of a virtual...
17
3549
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;" instead? I think declaring a function as "=0" is the same
8
2337
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
4
1629
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: virtual ~Shape() { } // A virtual destructor virtual void draw() = 0; // A pure virtual function
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10346
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
10157
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...
0
9956
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
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
7504
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
6742
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();...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.