calling default instead of argument constructor, why?!
Code:
class String
{
protected:
enum {SZ = 80};
char str[SZ];
public:
String() //this is being called from Pstring's else
{str[0] = '\0';}
String(char s[])
{strcpy(str, s);}
void display() const
{cout << str;}
operator char*()
{return str;}
};
class Pstring: public String
{
public:
Pstring(char s[]);
};
Pstring::Pstring(char s[])
{
if (strlen(s) > SZ-1)
{
//do something
}
else String(s); // this is calling String() and not String(char s[])!!
}
int main()
{
Pstring test = "testing";
test.display();
return 0;
}
When I initialize test, it goes into Pstring's else statement which calls String(s), but instead of calling String(char s[]) it's calling the no argument constructor String() even though I'm sending it the argument (s)!! Why??