Pure Virtual Constructor

T

Thomas Kowalski

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
 
M

mlimber

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-lite/ctors.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-lite/virtual-functions.html#faq-20.8).
What is it you really want to accomplish?

Cheers! --M
 
V

Victor Bazarov

Thomas said:
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
 
M

Michael DOUBEZ

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
 
P

Pete Becker

Michael said:
Thomas Kowalski a écrit :


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)
 
G

Grizlyk

Thomas said:
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;
};
 
N

Noah Roberts

Thomas Kowalski a écrit :
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.
 
P

Pete Becker

Noah said:
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)
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top