I wondering why in c++ can't use parent class constructor for an specific signature, in case that derived class miss that?
For example in below sample, I can't initialize dd
object with std::string
.
#include <iostream>
class Base
{
int num;
std::string s;
public:
Base(int _num){ num = _num;}
Base(std::string _s){ s = _s;}
};
class Derived : public Base {
public:
Derived(int _num):Base(_num){}
};
int main()
{
Base b(50);
Derived d(50);
Base bb("hell");
Derived dd("hell"); // <<== Error
return 0;
}
With Inheritance I expect to extend a class and not losing previous functionality but here I feel losing some.
In a more practical example, I create my version of std::string
but It doesn't behave like a std::string
in some cases :
#include <string>
#include <iostream>
class MyString: public std::string {
public:
void NewFeature(){/* new feature implementation*/}
};
int main()
{
MyString s("initialization"); // <<== Error: I expect to initialize with "..."
cout<<s; // <<== Error: I expect to print it like this.
return 0;
}
Can somebody give some explanation ?
– Emadpres Dec 07 '14 at 15:22