473,606 Members | 3,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoking a base class constructor

Suppose I have:

class A
{
A(int x, int y);
};

and
class B: public A
{
.... some declarations
};

then I try:
int s, t;
B bObject(s, t);

I get a compiler error (gcc) that no constructor of the form B(int&, int&)
exists. However, I would have expected that the constructor from class A
would be invoked (since B 'is a' A). Adding a B(int x, int y), and using
that to invoke A solves the problem.

Am I doing something wrong?

Thanks
Dominique
Jul 19 '05 #1
7 28979

"Dominique" <NODOTd.b.g.e.n .e.r.a.l.0.1.LO SEDOTS@cogecoDO Tca> wrote in message news:KI******** **********@read 1.cgocable.net. ..
I get a compiler error (gcc) that no constructor of the form B(int&, int&)
exists. However, I would have expected that the constructor from class A
would be invoked (since B 'is a' A). Adding a B(int x, int y), and using
that to invoke A solves the problem.

Am I doing something wrong?

Constructors don't get inheritted. The only constructors that exist for B
are the two implicitly generated ones:
B()
and B(const B&)

If you want one that takes two ints and passes them along as initailizers to
the A object you need to write:

B(int s, int t) : A(s,t) { }

Note that this will inhibit the creation of the B() constructor. If you need one, you
will have to provide it.
Jul 19 '05 #2
Ron Natalie wrote:
"Dominique" <NODOTd.b.g.e.n .e.r.a.l.0.1.LO SEDOTS@cogecoDO Tca> wrote in message news:KI******** **********@read 1.cgocable.net. ..

I get a compiler error (gcc) that no constructor of the form B(int&, int&)
exists. However, I would have expected that the constructor from class A
would be invoked (since B 'is a' A). Adding a B(int x, int y), and using
that to invoke A solves the problem.

Am I doing something wrong?


Constructors don't get inheritted. The only constructors that exist for B
are the two implicitly generated ones:
B()
and B(const B&)

If you want one that takes two ints and passes them along as initailizers to
the A object you need to write:

B(int s, int t) : A(s,t) { }

Note that this will inhibit the creation of the B() constructor. If you need one, you
will have to provide it.


That is not completely true, constructors do get inherited. The problem
with the code is that, since class A defines a non default cnstructor
A(int, int) the compiler does not generate a default constructor. Hence,
A() doesnot exist.

In B(int, int), since you are not explicitly calling A(int, int), the
compiler internally (while compiling) adds A() to the B(int, int)
constructor. But A() doesnot exists, hence you get the error message.

Good explanation of this phenomenon is present in the excellent book
"C++ object model" by Stanley lippman.

Jul 19 '05 #3

"amit gulati" <am********@cox .net> wrote in message news:bp******** **@news2.news.l arc.nasa.gov...
That is not completely true, constructors do get inherited.
No they don't. They have no name, they don't participate in lookup, so how
does the C++ notion of inheritance.
The problem
with the code is that, since class A defines a non default cnstructor
A(int, int) the compiler does not generate a default constructor. Hence,
A() doesnot exist.


This wasn't what he was asking. He wanted to know why he could construct
a B with two ints. The reason is that there is no B constructor that takes two
ints. Go back and read the original post.
Jul 19 '05 #4
amit gulati wrote:
That is not completely true, constructors do get inherited.
No, they don't.
The problem with the code is that, since class A defines a non default
cnstructor A(int, int) the compiler does not generate a default
constructor. Hence, A() doesnot exist.
Right.
In B(int, int),
B(int, int) doesn't exist.
since you are not explicitly calling A(int, int), the
compiler internally (while compiling) adds A() to the B(int, int)
constructor. But A() doesnot exists, hence you get the error message.
He didn't get an error message that A() doesn't exist, but one that
B(int, int) doesn't exist, which is true.
Just try out what happens if you add a default constructor to A.
According to your explanation, it should work then.
Good explanation of this phenomenon is present in the excellent book
"C++ object model" by Stanley lippman.


Never heared of that book. Seems that it belongs on the
not-to-be-recommended list.

Jul 19 '05 #5
amit gulati wrote:
Ron Natalie wrote:
"Dominique" <NODOTd.b.g.e.n .e.r.a.l.0.1.LO SEDOTS@cogecoDO Tca> wrote in
message news:KI******** **********@read 1.cgocable.net. ..

I get a compiler error (gcc) that no constructor of the form B(int&,
int&)
exists. However, I would have expected that the constructor from class A
would be invoked (since B 'is a' A). Adding a B(int x, int y), and using
that to invoke A solves the problem.

Am I doing something wrong?

Constructors don't get inheritted. The only constructors that exist
for B
are the two implicitly generated ones:
B()
and B(const B&)

What I mean by inherited is that in class B you can call A's
constructors, doesn't that mean that the constructors are inherited. I
never said that if you define A(int, int) then the compiler will
generate B(int, int).

Inheritance means that the properties (data and functions) of the base
class are available to the sub class as if they were it's own.

Hence anywhere in B I can call A(int, int), this shows that the
constructor A(int, int) is inherited.

I agree I did not read the problem that the person had completely. But
the fact is constructors are inherited.

If you want one that takes two ints and passes them along as
initailizers to
the A object you need to write:

B(int s, int t) : A(s,t) { }

Note that this will inhibit the creation of the B() constructor. If
you need one, you
will have to provide it.


That is not completely true, constructors do get inherited. The problem
with the code is that, since class A defines a non default cnstructor
A(int, int) the compiler does not generate a default constructor. Hence,
A() doesnot exist.

In B(int, int), since you are not explicitly calling A(int, int), the
compiler internally (while compiling) adds A() to the B(int, int)
constructor. But A() doesnot exists, hence you get the error message.

Good explanation of this phenomenon is present in the excellent book
"C++ object model" by Stanley lippman.


Jul 19 '05 #6

"amit gulati" <am********@cox .net> wrote in message news:bp******** **@news2.news.l arc.nasa.gov...
=
What I mean by inherited is that in class B you can call A's
constructors, doesn't that mean that the constructors are inherited.
Well, you're still wrong. You can't call constructors at all.
Inheritance means that the properties (data and functions) of the base
class are available to the sub class as if they were it's own.
Which does not happen for constructors.
Hence anywhere in B I can call A(int, int), this shows that the
constructor A(int, int) is inherited.


You can't call A(int,int) anywhere. You can't call constructors.

Jul 19 '05 #7
Ron Natalie wrote:
"amit gulati" <am********@cox .net> wrote in message news:bp******** **@news2.news.l arc.nasa.gov...
=
What I mean by inherited is that in class B you can call A's
constructor s, doesn't that mean that the constructors are inherited.

Well, you're still wrong. You can't call constructors at all.

Inheritance means that the properties (data and functions) of the base
class are available to the sub class as if they were it's own.

Which does not happen for constructors.

Hence anywhere in B I can call A(int, int), this shows that the
constructor A(int, int) is inherited.

You can't call A(int,int) anywhere. You can't call constructors.

OOps! I messed up again.
Refered to the C++ annotated reference manual page 263

"Constructo rs are not inherited"

Thanks for clearing my doubts.
Jul 19 '05 #8

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

Similar topics

0
4246
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message (with no responces associated) :
5
7329
by: mrstephengross | last post by:
Ok, I've got code that looks something like this: ================================================== template<typename T1, typename T2> class Base { public: explicit Base(const T1 & t1) { /* ... */ } };
3
3484
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
1586
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 () <----------------------
6
2483
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
12
3422
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to ensure the base class's static constructor is invoked before the derived class instance is constructed ?...
3
2234
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
26
5349
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
0
8036
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
7978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8461
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
8448
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...
0
8317
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5987
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
5470
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
3948
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
2454
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

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.