Call base class constructor?

D

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?
 
A

Alf P. Steinbach

* 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>.
 
V

Victor Bazarov

desktop said:
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
 
G

Gianni Mariani

desktop said:
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)
 
D

desktop

Victor said:
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.


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");
 
V

Victor Bazarov

desktop said:
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
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top