[LC++]Re: [Tuxcpprogramming] Not everything inherited with inheritance??

Jack Lloyd lloyd at acm.jhu.edu
Sat Jul 21 02:05:47 UTC 2001

> Okay.  Thanks for this.  Is it only the constructors which are not
> passed on?

Also non-virtual destructors, and possibly some things like operator=? I
really can't remember.

> And now it seems to work okay.  Is there anything else I should add?

Possibly operator=, but probably the default will work.

> I thought conversion was handled by something like
>     operator char*() const {......}
> rather than via the constructor???

It is usually (along with operator=). What I said before was wrong. When
you construct and assign an object at the same time, it calls the
constructor with whatever is on the right of the '=' instead of calling an
empty constructor and then calling operator= with whatever was on the
right. But since no such constructor existed bad things happened. That's
what was actually going on (how much you want to bet I'm wrong this time
too ^_^).

> Does C++ use constructor argument for conversion as well??

No, I was totally wrong about that (see above).

> What I want is basically a string with extra functionality.  In
> particular I want it to have an extra member function associated with
> it called "getToken(string& str)" which will extract tokens from the
> string, tokens being separated by whitespace.
> Because what I want _is_ actually a string --- ie I want to be able to
> do everything else you can do with a string --- it makes sense (I
> thought) to derive it from "string".  The only difference is that it
> has this extra functionality.
> Is my reasoning faulty here?  Is there a better way of doing what I
> want?

For the most part, this makes perfectly good sense, from an OO
perspective. From a C++ perspective, it's probably cleaner and safer to
create a new class which contains a string as a member. If you provide
suitable conversions (constructor, operator std::string, and operator=)
you shouldn't be able to see the difference.

Also, you may want to consider a prodedural interface:

string getToken(string& from)

which pulls the next token out of from and returns it. That way code
doesn't have to be converted to use the new class; normal strings as used
by lots of pre-existing code will work seamlessly with it.


More information about the tuxCPProgramming mailing list