Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pure Virtual Constructor

Reply
Thread Tools

Pure Virtual Constructor

 
 
Thomas Kowalski
Guest
Posts: n/a
 
      01-25-2007
Hi everyone,
is there a way to enforce that every class that inherit from BASE have
to implement a given constructor?
Example:

class Base {
virtual Base (int,int) = 0;
}

class INHERIT : public Base {
virtual INHERIT(int, int);
}

guess the only way is to have something like a protected init method
that should be called in the constructor??

Regards,
T.Kowalski

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      01-25-2007
On Jan 25, 9:47 am, "Thomas Kowalski" <t...@gmx.de> wrote:
> is there a way to enforce that every class that inherit from BASE have
> to implement a given constructor?
> Example:
>
> class Base {
> virtual Base (int,int) = 0;
> }
>
> class INHERIT : public Base {
> virtual INHERIT(int, int);
> }
>
> guess the only way is to have something like a protected init method
> that should be called in the constructor??


First, your constructor is private, so only friends or member functions
(but not derived classes) can access it.

Second, you can't call a virtual init function from your base class
constructor
(http://www.parashift.com/c++-faq-lit...html#faq-10.7).

Third, to finally answer your question, no. But there may be other ways
to accomplish your desired end (e.g.,
http://www.parashift.com/c++-faq-lit...tml#faq-20.8-).
What is it you really want to accomplish?

Cheers! --M

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-25-2007
Thomas Kowalski wrote:
> is there a way to enforce that every class that inherit from BASE have
> to implement a given constructor?


No. Why would you want to do that?

> Example:
>
> class Base {
> virtual Base (int,int) = 0;


Constructors cannot be virtual (and hence cannot be pure).

> }

;
>
> class INHERIT : public Base {
> virtual INHERIT(int, int);
> }

;

> guess the only way is to have something like a protected init method
> that should be called in the constructor??


Why? If 'Base's constructor requires two arguments, the 'INHERIT's
constructor *has to* construct the base class subobject by passing
two integers. Why does 'INHERIT's constructor have to have the same
arguments?

I think you misunderstand how constructors are used and what they are
for.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      01-25-2007
Thomas Kowalski a écrit :
> Hi everyone,
> is there a way to enforce that every class that inherit from BASE have
> to implement a given constructor?
> Example:
>


No. 12.1/4 of the standard.

> guess the only way is to have something like a protected init method
> that should be called in the constructor??


Yes. But from 10.3/6 of the standard this is undefined.
The reason is that when the virtual function is called, the derived
object doesn't already exists and cannot use member data.

Michael

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-25-2007
Michael DOUBEZ wrote:
> Thomas Kowalski a écrit :
>
>> guess the only way is to have something like a protected init method
>> that should be called in the constructor??

>
> Yes. But from 10.3/6 of the standard this is undefined.
> The reason is that when the virtual function is called, the derived
> object doesn't already exists and cannot use member data.
>


I'm not sure what it is that you think is undefined, nor how the note in
10.3/6 relates to this question.

Calling a virtual function from a constructor is well-defined, but
typically isn't particularly useful. It calls the version of the
function for the class that's currently being constructed, not the one
in the derived class.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-26-2007
Thomas Kowalski wrote:
> class Base {
> virtual Base (int,int) = 0;
>
> };


Constructor of any calss is _always_ like virtual. The keyword virtual
means "use class of real object". When you are creating object, you
must explicit write name of concrete class of object to create.

If you need to create object of unknown derived class, you must to
define ordinary member inside any class, to create concrete class at
runtime.

class Base{};
class Derived1: public Base{};
class Derived2: public Base{};

class Factory
{
public:
Base* create();
};

Member Factory::create() will encapsulate knowledge how to create
concrete derived class from Base, for example:

Base* Factory::create(){ return new Derived2; }

Class Factory can remove implementation of Factory::create() to derived
from Factory, in the case Factory::create() must be virtual

class Factory
{
public:
virtual Base* create()=0;
};

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      01-26-2007


On Jan 25, 7:30 am, Michael DOUBEZ <michael.dou...@free.fr> wrote:
> Thomas Kowalski a écrit :


> > guess the only way is to have something like a protected init method
> > that should be called in the constructor??

> Yes. But from 10.3/6 of the standard this is undefined.
> The reason is that when the virtual function is called, the derived
> object doesn't already exists and cannot use member data.


The yes is a bit misleading though. It never works right. IMO
anything undefined is considered, "no, you can't do that," especially
when the usual result is anything but what you think you want.

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-26-2007
Noah Roberts wrote:
>
> On Jan 25, 7:30 am, Michael DOUBEZ <michael.dou...@free.fr> wrote:
>> Thomas Kowalski a écrit :

>
>>> guess the only way is to have something like a protected init method
>>> that should be called in the constructor??

>> Yes. But from 10.3/6 of the standard this is undefined.
>> The reason is that when the virtual function is called, the derived
>> object doesn't already exists and cannot use member data.

>
> The yes is a bit misleading though. It never works right. IMO
> anything undefined is considered, "no, you can't do that," especially
> when the usual result is anything but what you think you want.
>


But the "It never works right" is also a bit misleading <g>, since the
effect of calling a virtual function from a constructor is well defined,
unless the function is pure virtual. On the other hand, it won't solve
the original problem, because the function belonging to the base
currently being constructed is called, and not the function belonging to
the derived class.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
virtual vs pure virtual member function sam_cit@yahoo.co.in C++ 7 05-02-2007 10:00 AM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
Can pure virtual function be called in base class constructor? PengYu.UT@gmail.com C++ 10 10-18-2005 03:45 PM
calling pure virtual in abstract class's constructor vsgdp C++ 7 09-25-2005 06:42 PM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57