[Tuxcpprogramming] Not everything inherited with inheritance??
Jack Lloyd
lloyd at acm.jhu.edu
Fri Jul 20 03:14:47 UTC 2001
> It would seem that when you inherit a class, not
> everything gets passed on. Consider the following
> example:
No. Specifically, constructor are not passed on by default, you have to
call them yourself.
> When I try to compile it, it complains with:
>
> tst_main.cpp: In function `int main(int, char **)':
> tst_main.cpp:16: conversion from `const char[7]' to non-scalar type
> `tstTy' requested
>
> So it seems that whereas the string type can happily
> convert standard character strings to string type,
> tstTy, which is inherited from string, can't!!!
>
> Why is this so???
Because you did not provide an operator=, the compiler used the default
definition of one, which:
1) converts it's argument to the appropriate class (tstTy in this case)
2) does a copy of each element of the first object to the other one
The problem being there is now way to convert a const char* into a tstTy
in (1). So the compiler gives up.
> And what can I do about it?
Create a constructor to handle the conversion from a const char* to a
string.
BTW, are you sure you want to be doing this? STL containers (including
string) are not really meant for derivation.
Jack
More information about the tuxCPProgramming
mailing list