473,385 Members | 1,848 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,385 software developers and data experts.

Pure Virtual Constructor

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

Jan 25 '07 #1
7 9353
On Jan 25, 9:47 am, "Thomas Kowalski" <t...@gmx.dewrote:
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...html#faq-20.8).
What is it you really want to accomplish?

Cheers! --M

Jan 25 '07 #2
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
Jan 25 '07 #3
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

Jan 25 '07 #4
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)
Jan 25 '07 #5
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;
};

Jan 26 '07 #6


On Jan 25, 7:30 am, Michael DOUBEZ <michael.dou...@free.frwrote:
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.

Jan 26 '07 #7
Noah Roberts wrote:
>
On Jan 25, 7:30 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>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)
Jan 26 '07 #8

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

Similar topics

37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
7
by: vsgdp | last post by:
I have an abstract class A: class A { public: A(){ f(); } virtual void f() = 0; }; class B : public A
10
by: PengYu.UT | last post by:
Hi, A pure function is called in the base function constructor. It generate a run time error: "pure virtual method called". My problem is that class A have some derived classes. I want A's...
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
22
by: ypjofficial | last post by:
Hello All, I have following doubt.. class abstractclass { public: abstractclass(){} virtual void method()=0; };
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
4
by: Corno | last post by:
Hi all, I thought that the 2 following functions would have the same effect; void first() { ClassA a("bla"); anotherFunction(a); }
3
by: a | last post by:
Hi, I need clarification for virtual method and pure virtual method. e.g Class Base{ virtual void func(){ ---- } } Class Child : public Base{ void func()
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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...

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.