I am working my way through Object Oriented programming in C++ from Robert Lafore and I am stuck with one exercise where he calls a base class constructor from a derived class.
I try to use a char array constructor in the baseclass to initialize the variable sstr. When calling it from a derivedclass like this:
derivedclass(char s[], j) : baselcass(s), i(j) {}
sstr gets initizialized but when calling it like this:
1 2 3 4 5
derivedclass(char s[], j)
{
baselcass(s);
i = j;
}
I receive errors with g++. Can anyone explain why and how to circumvent this issue? I would like to be able to call the baseclass constructor from the body of the derived constructor so I can make some adjustments to the char array.
For a complete code listing see below (and any other constructive remarks are very welcome)
The syntax to call the base constructor is using initialization lists ( the : baseclass ( ) thing )
Once you get in the body of the constructor your object has already been constructed so you can't call the base constructor.
If you use C++ strings instead of char arrays you'll have much less trouble and btw, array parameters are pointers
thanks for the clarification. The idea of the exercise was to make your own string class with buffer protection (in a crude way). There was a base class called String and a derived class called Pstring (protected string). The derived class should check the length of the input and then cut of any remaining data. Then it creates an object of type String.
To be honest, your base class has the responsibility to safely construct itself from its constructor parameters. It seem like a strange design decision to subordinate that task to a sub-class.
class devcl : public baseclass
{
private:
int i;
public:
devcl() : baseclass(), i(0)
{}
devcl(char s[], int j)
{
i = j;
//baseclass(s); //*****I wouldn't have thought that this would cause a compiler error*****
//These next two line together is just the same as the one above
char t[SZ];
baseclass(strcpy(t, s));
}
void showderived()
{
showstr();
cout << "\nI has the value of: " << i << endl;
}
};
The c++ statement format of a typename followed by some or no values in parenthesis
creates a temporary variable of that type (it is unamed because you have not given it an identifier);
For example: std::string ("helo world");
So your baseclass(s) was creating a baseclass variable - not initializing the baseclass part of the derived class as you hoped.
As already pointed out - baseclass initialization is done in the initialization list ( in that list baseclass(s) means initialise the basclass not create a baseclass variable).
@Galik: you are absolutely right about the responsibility of the base class when it comes to safely constructing parameters. This was however just an exercise from the book and well, it served its purpose. It is somewhat clearer now what you can and can't do and what happens when. Thanks for pointing me to the definition of the strncpy, that third parameter is the clue!
@guestgalkan: thanks, I think I understand why things went wrong now....