473,405 Members | 2,262 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,405 software developers and data experts.

Call base class constructor?

I am reading a text that says:

"The derived-class constructor initializer names its base-class followed
by a (possibly empty) list of arguments. These arguments are the initial
values to use in constructing the base-class part; the serve to select
the base-class constructor to run in order to initialize the base".

I have then made this example:

#include <string>

class BigBob {
public:
BigBob (std::string str) {
n = str;
}

std::string getName() {
return n;
}

private:
std::string n;

};

class SmallBob: public BigBob {
public:
SmallBob() { BigBob}; //naming base class followed by no
//args
private:
};

I have followed the instructions to name the base-class in the
derived-class constructor initializer followed by no arguments. But it
gives a lot of errors. What am I misunderstanding in the guide?
May 9 '07 #1
5 3880
* desktop:
I am reading a text that says:

"The derived-class constructor initializer names its base-class followed
by a (possibly empty) list of arguments. These arguments are the initial
values to use in constructing the base-class part; the serve to select
the base-class constructor to run in order to initialize the base".

I have then made this example:

#include <string>

class BigBob {
public:
BigBob (std::string str) {
n = str;
}

std::string getName() {
return n;
}

private:
std::string n;

};

class SmallBob: public BigBob {
public:
SmallBob() { BigBob}; //naming base class followed by no
//args
private:
};

I have followed the instructions to name the base-class in the
derived-class constructor initializer followed by no arguments. But it
gives a lot of errors. What am I misunderstanding in the guide?
See section 2.1.12 of <url:
http://home.no.net/dubjai/win32cpptut/html/w32cpptut_02.html>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 9 '07 #2
desktop wrote:
I am reading a text that says:

"The derived-class constructor initializer names its base-class
followed by a (possibly empty) list of arguments. These arguments are
the initial values to use in constructing the base-class part; the
serve to select the base-class constructor to run in order to
initialize the base".
This text doesn't actually specify HOW the initialiser "names its
base-class". I am not sure how you can be expected to "follow" it.
I have then made this example:

#include <string>

class BigBob {
public:
BigBob (std::string str) {
n = str;
}

std::string getName() {
return n;
}

private:
std::string n;

};

class SmallBob: public BigBob {
public:
SmallBob() { BigBob}; //naming base class followed by no
//args
private:
};

I have followed the instructions to name the base-class in the
derived-class constructor initializer followed by no arguments. But it
gives a lot of errors. What am I misunderstanding in the guide?
There is no real "guide". You're expected to do

SmallBob() : BigBob("big bob") {}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 9 '07 #3
desktop wrote:
I am reading a text that says:

"The derived-class constructor initializer names its base-class followed
by a (possibly empty) list of arguments. These arguments are the initial
values to use in constructing the base-class part; the serve to select
the base-class constructor to run in order to initialize the base".

I have then made this example:

#include <string>

class BigBob {
public:
BigBob (std::string str) {
n = str;
}

std::string getName() {
return n;
}

private:
std::string n;

};

class SmallBob: public BigBob {
public:
SmallBob() { BigBob}; //naming base class followed by no
//args
A conforming version of this constructor would be:

SmallBob() : BigBob( std::string("") ) { }

{ BigBob};

is syntactically ill formed. The compiler will Not know what to do with it.

BTW - unless you have good reason to do otherwise, declare functions
that take a std::string parameter to be const references like :

BigBob (const std::string & str)
>

private:
};

I have followed the instructions to name the base-class in the
derived-class constructor initializer followed by no arguments. But it
gives a lot of errors. What am I misunderstanding in the guide?
May 9 '07 #4
Victor Bazarov wrote:
desktop wrote:
>I am reading a text that says:

"The derived-class constructor initializer names its base-class
followed by a (possibly empty) list of arguments. These arguments are
the initial values to use in constructing the base-class part; the
serve to select the base-class constructor to run in order to
initialize the base".

This text doesn't actually specify HOW the initialiser "names its
base-class". I am not sure how you can be expected to "follow" it.
>I have then made this example:

#include <string>

class BigBob {
public:
BigBob (std::string str) {
n = str;
}

std::string getName() {
return n;
}

private:
std::string n;

};

class SmallBob: public BigBob {
public:
SmallBob() { BigBob}; //naming base class followed by no
//args
private:
};

I have followed the instructions to name the base-class in the
derived-class constructor initializer followed by no arguments. But it
gives a lot of errors. What am I misunderstanding in the guide?

There is no real "guide". You're expected to do

SmallBob() : BigBob("big bob") {}

V
Do you not mean:

SmallBob(std::string str) : BigBob(str){}

this does not work, but I guess something like that is necessary if I
want to make an instance of SmallBob like:

SmallBob("smallbob");
May 9 '07 #5
desktop wrote:
Victor Bazarov wrote:
>desktop wrote:
>>I am reading a text that says:

"The derived-class constructor initializer names its base-class
followed by a (possibly empty) list of arguments. These arguments
are the initial values to use in constructing the base-class part;
the serve to select the base-class constructor to run in order to
initialize the base".

This text doesn't actually specify HOW the initialiser "names its
base-class". I am not sure how you can be expected to "follow" it.
>>I have then made this example:

#include <string>

class BigBob {
public:
BigBob (std::string str) {
n = str;
}

std::string getName() {
return n;
}

private:
std::string n;

};

class SmallBob: public BigBob {
public:
SmallBob() { BigBob}; //naming base class followed by no
//args
private:
};

I have followed the instructions to name the base-class in the
derived-class constructor initializer followed by no arguments. But
it gives a lot of errors. What am I misunderstanding in the guide?

There is no real "guide". You're expected to do

SmallBob() : BigBob("big bob") {}

V

Do you not mean:

SmallBob(std::string str) : BigBob(str){}
No, I don't mean that. Since you specified that 'SmallBob's c-tor
does not take any arguments, why change now?
this does not work,
Read FAQ 5.8.
but I guess something like that is necessary if I
want to make an instance of SmallBob like:

SmallBob("smallbob");
If you want to make a usable instance you might want to give it a name:

SmallBob mybob("smallbob");

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

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

Similar topics

23
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) .
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
4
by: Greg | last post by:
Is it possible to call a constructor from within a constructor I mean Class A { public A(string getModifiedVal) { .........
6
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;
6
by: marcwentink | last post by:
Say I have class A that inherits from class B, and I call class A his constructor. Then somehow class B his constructor is called also. How does this work? Is a constructor under the hood a sort of...
1
by: mwebel | last post by:
Hi ikind of remember this being easy but cant find it on google: i have a base class and a derived class. The base class has two constructors normal and int overloaded one. thing is i want the...
5
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
11
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by...
11
by: dolphin | last post by:
Hi All! I have a question that how to call a function just using a string. For example There is a .cpp file named a.cpp.There are some functions::fun1() fun2() fun3(). I have another fucntion...
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: 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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.