C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Virtual Constructors

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Thomas Matthews
Guest





PostPosted: Fri Dec 26, 2003 6:01 pm    Post subject: Virtual Constructors Reply with quote



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

Back to top
Jeff Schwab
Guest





PostPosted: Fri Dec 26, 2003 6:03 pm    Post subject: Re: Virtual Constructors Reply with quote



Thomas Matthews wrote:
Quote:
Hi,

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

No.

Quote:
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.



Back to top
Ron Natalie
Guest





PostPosted: Fri Dec 26, 2003 6:08 pm    Post subject: Re: Virtual Constructors Reply with quote




"Thomas Matthews" <Thomas_MatthewsSpamBotsSuck (AT) sbcglobal (DOT) net> wrote

Quote:
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



Back to top
Cy Edmunds
Guest





PostPosted: Fri Dec 26, 2003 6:31 pm    Post subject: Re: Virtual Constructors Reply with quote

"Thomas Matthews" <Thomas_MatthewsSpamBotsSuck (AT) sbcglobal (DOT) net> wrote in
message news:3FEC7785.30806 (AT) sbcglobal (DOT) net...
Quote:
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.

Quote:

--
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/



Back to top
Thomas Matthews
Guest





PostPosted: Fri Dec 26, 2003 7:58 pm    Post subject: Re: Virtual Constructors Reply with quote

Ron Natalie wrote:
Quote:
"Thomas Matthews" <Thomas_MatthewsSpamBotsSuck (AT) sbcglobal (DOT) net> wrote


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 (Includevcldbtables.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


Back to top
John Carson
Guest





PostPosted: Sat Dec 27, 2003 1:14 am    Post subject: Re: Virtual Constructors Reply with quote

"Thomas Matthews" <Thomas_MatthewsSpamBotsSuck (AT) sbcglobal (DOT) net> wrote in
message news:3FEC7785.30806 (AT) sbcglobal (DOT) net
Quote:
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)

Back to top
Darko Miletic
Guest





PostPosted: Sun Jan 04, 2004 11:43 am    Post subject: Re: Virtual Constructors Reply with quote

On Fri, 26 Dec 2003 19:58:53 GMT, Thomas Matthews
<Thomas_MatthewsSpamBotsSuck (AT) sbcglobal (DOT) net> wrote:

Quote:
Here is the code from the header file (Includevcldbtables.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.

Quote:
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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.