473,738 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

use base class constructors always

hi,
In the following code, I would like not to have to declare the
constructors in bar ( either the default or the (int a) constructor ).
When I remove them, bar has no idea of construction with an int, so I
get compile errors. I currently have to declare them for every class
inheriting foo, and it would be much nicer just with the init. Is there
a nice way of doing this? I am currently using a
macro, like CONSTRUCTOR(bar ) which feels a bit squiffy to me.
Also, if I'm not pushing my luck I would like some way to make the init
pure virtual to force implementation in inheritors, without getting the
linker error for foo::init that I get when I try.
thanks,
iain

class foo
{
public:
foo() {}
foo( int a ) {init(a);}
virtual void init ( int a ) {};
};

class bar: public foo
{
public:
bar() {}
bar( int a ):foo(a) {}
virtual void init( int a ){/*etc.*/}
};

void main()
{
bar a( 1 );
bar b;
b.init( 2 );
}

Jul 23 '05 #1
4 1447
pixelbeast wrote:
In the following code, I would like not to have to declare the
constructors in bar ( either the default or the (int a) constructor ).
When I remove them, bar has no idea of construction with an int, so I
get compile errors.
Post the code which gives you the compile errors, don't post speculation.
I currently have to declare them for every class
inheriting foo, and it would be much nicer just with the init.
'init' is a deceitful name. True initialisation happens during object's
construction. If you don't want to initialise during construction, it's
your right, but your base class object ('foo' part of any derived class)
will be initialised (or at least attempted at initialising) when the
derived class object is constructed, not when 'init' is called.
Is there
a nice way of doing this?
A nice way of doing what, exactly?
I am currently using a
macro, like CONSTRUCTOR(bar ) which feels a bit squiffy to me.
Where? I don't see any 'CONSTRUCTOR' macro in the code posted.
Also, if I'm not pushing my luck I would like some way to make the init
pure virtual to force implementation in inheritors, without getting the
linker error for foo::init that I get when I try.
Again, what linker errors? Post the code that produces the errors.
thanks,
iain

class foo
{
public:
foo() {}
foo( int a ) {init(a);}
Here the 'init' of the class 'foo' will be called, not the 'init' of
the most derived class (as you might hope).
virtual void init ( int a ) {};
};

class bar: public foo
{
public:
bar() {}
bar( int a ):foo(a) {}
virtual void init( int a ){/*etc.*/}
};

void main()
There is no 'void main' in C++, BTW.
{
bar a( 1 );
bar b;
b.init( 2 );
}

V
Jul 23 '05 #2
i think you are right ofcourse, I need to re-evaluate this, the
splitting of constructors and inits was done a long time ago in my
codebase, but looking at the current situation, and the problems it is
causing, I will go back and investigate why I did it.

for completeness, you requested code that causes the compile errors,
The following code (removing the constructors from bar)....

class foo
{
public:
foo() {}
foo( int a ) {init(a);}
virtual void init ( int a ) {};
};

class bar: public foo
{
public:
//comment bar() {}
//comment bar( int a ):foo(a) {}
virtual void init( int a ){}
};

void main()
{
bar a( 1 );
bar b;
b.init( 2 );
}

... gives the following error..
d:\dev\test\tes t\t.cpp(20) : error C2664: 'bar::bar(const bar &)' :
cannot convert parameter 1 from 'int' to 'const bar &'
Reason: cannot convert from 'int' to 'const bar'
No constructor could take the source type, or constructor
overload resolution was ambiguous

... and trying to make "init" pure virtual as follows...

class foo
{
public:
foo() {}
foo( int a ) {init(a);}
virtual void init ( int a ) =0;
};

class bar: public foo
{
public:
bar() {}
bar( int a ):foo(a) {}
virtual void init( int a ){}
};

void main()
{
bar a( 1 );
bar b;
b.init( 2 );
}

... gives the following linker error...
t.obj : error LNK2019: unresolved external symbol "public: virtual void
__thiscall foo::init(int)" (?init@foo@@UAE XH@Z) referenced in function
"public: __thiscall foo::foo(int)" (??0foo@@QAE@H@ Z)
Debug/test.exe : fatal error LNK1120: 1 unresolved externals

.... finally the macro i was attempting to use, was equivalent to the
one used in following, where I was testing your analysis of my init...
#define CONSTRUCTORS(_n ame) \
_name() {} \
_name(int a) : foo(a) {}

class foo
{
public:
foo() {}
foo( int a ) {init(a);}
virtual void init ( int a ) {};
};

class bar: public foo
{
public:
CONSTRUCTORS(ba r)
virtual void init( int a ){ val = a;}
int val;
};

void main()
{
bar a( 1 );
bar b;
b.init( 2 );

bar c(3); // doesn't work - as Victor predicted.
// virtual init in foo is called
bar d;
d.init(3);

bar e;
foo *pe = &e;
pe->init(3);
}
.... however, as is obvious - my splitting of contructor
responsibilitie s is producing problems where there shouldn't be any and
I shall be changing it .

Thanks for being frank, sorry about the void main();

Jul 23 '05 #3
pixelbeast wrote:
i think you are right ofcourse, I need to re-evaluate this, the
splitting of constructors and inits was done a long time ago in my
codebase, but looking at the current situation, and the problems it is
causing, I will go back and investigate why I did it.

for completeness, you requested code that causes the compile errors,
The following code (removing the constructors from bar)....

class foo
{
public:
foo() {}
foo( int a ) {init(a);}
virtual void init ( int a ) {};
};

class bar: public foo
{
public:
//comment bar() {}
//comment bar( int a ):foo(a) {}
virtual void init( int a ){}
};

void main()
{
bar a( 1 );
bar b;
b.init( 2 );
}

.. gives the following error..
d:\dev\test\tes t\t.cpp(20) : error C2664: 'bar::bar(const bar &)' :
cannot convert parameter 1 from 'int' to 'const bar &'
Reason: cannot convert from 'int' to 'const bar'
No constructor could take the source type, or constructor
overload resolution was ambiguous
What else to expect? You haven't defined a constructor in the 'bar'
class which would accept an argument of type 'int', how should the
compiler know what you want to do when you write

bar a( 1 );

??? Remember, constructors are *not* inherited.
.. and trying to make "init" pure virtual as follows...

class foo
{
public:
foo() {}
foo( int a ) {init(a);}
Again, this would call 'foo::init', which is pure, and you'll have
undefined behaviour.
virtual void init ( int a ) =0;
};

class bar: public foo
{
public:
bar() {}
bar( int a ):foo(a) {}
virtual void init( int a ){}
};

void main()
{
bar a( 1 );
bar b;
b.init( 2 );
}

[...]

... however, as is obvious - my splitting of contructor
responsibilitie s is producing problems where there shouldn't be any and
I shall be changing it .

Thanks for being frank, sorry about the void main();


Don't be sorry, just get rid of it. C++ has no 'void main'...

V
Jul 23 '05 #4
pixelbeast wrote:
hi,
In the following code, I would like not to have to declare the
constructors in bar ( either the default or the (int a)
constructor ). When I remove them, bar has no idea of
construction with an int, so I get compile errors. I
currently have to declare them for every class inheriting
foo, and it would be much nicer just with the init. Is there
a nice way of doing this?
Unfortunately, no. I believe that the standards committee is
looking at ways of adding this to future versions of the
language.
class foo
{
public:
foo() {}
foo( int a ) {init(a);}
virtual void init ( int a ) {};
};

class bar: public foo
{
public:
bar() {}
bar( int a ):foo(a) {}
virtual void init( int a ){/*etc.*/}
};


Something you might not be aware of: when you construct
a bar from an int, eg:

bar b(1);

then it will call foo::init, and bar::init will never be
called. The virtual function mechanism doesn't kick in
until after the object has been constructed. In other
words, the call to 'init' in foo(int) does not try to
call init in any derived classes, because the derived
object has not yet been constructed.

Jul 23 '05 #5

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

Similar topics

5
6161
by: Martin Magnusson | last post by:
Hi! I have a class with a private member which is a pointer to an abstract class, which looks something like this: class Agent { public: void Step( Base* newB ); private:
23
5179
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
3
3494
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ... more protected fields }
3
1597
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client app with; D m_D = new D(tkn); public class A : MarshalByRefObject public A () <--------------------- public class B : A public B () <----------------------
12
1559
by: Chris | last post by:
Hi I am trying to create a base class with the Protected keyword Protected MustInherit Class MyBaseClas .. .. End Clas And I got an error saying something like the Protected keyword cannot be used
2
4739
by: norton | last post by:
Hello, May i know if there any ways to inherits a constructor from the base class? For Example i have a abstract class called User and a Customer class is inherits from User, and the base class user contains 3 constructor named 1. Public sub new() 2. Public SUb New(ByVal sName as string) 3. Public SUb New(ByVal sName as string, Byval sPassword as string)
1
2255
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the statement. To test this, I added messageboxes in all constructors indicating which constructor is called. There are four constructors which could be called: 1. Base class empty constructor 2. Base class 2-Par. constructor
26
5371
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
11
3047
by: Aflj | last post by:
This code won't compile (two compilers tried, gcc and VC++, both of recent versions, but I don't remember them exactly): class C1 { public: void M1(int i) {} }; class C2: public C1
2
1600
by: Josh Valino | last post by:
If I have a base class that has one constructor, is the only way that that constructor will get called be by having no constructors in my derived classes or explicitly calling base() in my derived constructors? Illustration: abstract class whatever { public whatever() { //code here I want to run
0
8968
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
9473
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
9334
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...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6750
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
6053
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
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.