473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

STL string inheritance

Hello community,

i'm fairly new to using the STL but i've been experimenting a bit with it.
I tried to derive a new class say MyString from string like so:

class MyString: public string{
public:
MyString(){}
};

when i then try to do:

MyString MyStringInstance;
MyStringInstance = "BlaBlaBla";

it moans:
error C2679: binary '=' : no operator found which takes a right-hand operand
of type 'const char [9]' (or there is no acceptable conversion)

MyStringInstance += "BlaBlaBla";

and

string MyStringInstance;
MyStringInstance = "BlaBlaBla";

works fine though.

Why is the = operator not available in MyString ??

Thanks for any help, Mike :)

P.S. I'm using MS Visual C++ .NET


Jul 22 '05 #1
19 3162

"Mike Tyka" <m.****@gnx.net> wrote in message news:I4********@bath.ac.uk...
Hello community,

i'm fairly new to using the STL but i've been experimenting a bit with it.
I tried to derive a new class say MyString from string like so:

class MyString: public string{
public:
MyString(){}
};

when i then try to do:

MyString MyStringInstance;
MyStringInstance = "BlaBlaBla";

it moans:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const char [9]' (or there is no acceptable conversion)

MyStringInstance += "BlaBlaBla";

and

string MyStringInstance;
MyStringInstance = "BlaBlaBla";

works fine though.

Why is the = operator not available in MyString ??

Thanks for any help, Mike :)

P.S. I'm using MS Visual C++ .NET


operator=() is not directly inherited by the derived class, it is the
standard C++ behaviour (IMHO)

Fabio
Jul 22 '05 #2

"Fabio" <fa*************@libero.it> skrev i en meddelelse
news:2s*************@uni-berlin.de...

"Mike Tyka" <m.****@gnx.net> wrote in message

news:I4********@bath.ac.uk...
Hello community,

i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so:

class MyString: public string{
public:
MyString(){}
};

when i then try to do:

MyString MyStringInstance;
MyStringInstance = "BlaBlaBla";

it moans:
error C2679: binary '=' : no operator found which takes a right-hand

operand
of type 'const char [9]' (or there is no acceptable conversion)

MyStringInstance += "BlaBlaBla";

and

string MyStringInstance;
MyStringInstance = "BlaBlaBla";

works fine though.

Why is the = operator not available in MyString ??

Thanks for any help, Mike :)

P.S. I'm using MS Visual C++ .NET


operator=() is not directly inherited by the derived class, it is the
standard C++ behaviour (IMHO)

Fabio


Rubbish! Of course it is.
/Peter
Jul 22 '05 #3
Peter Koch Larsen wrote in news:K7f7d.54262$Vf.2607123
@news000.worldonline.dk in comp.lang.c++:
operator=() is not directly inherited by the derived class, it is the
standard C++ behaviour (IMHO)

Fabio


Rubbish! Of course it is.


Not so, Fabio is correct (*), operator = if not declared by the user
is generated by the compiler (uses member/base-wise assignment),
hence the compiler generated operator = hides the base class
operator =.

*) This depends on the interpretation of "directly inherited"
of course :).

The easiest way to declare operator = is:

#include <iostream>
#include <string>

class MyString: public std::string
{
public:

MyString(){}

using std::string::operator =;
};

Alas this won't work for constructors.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
"Mike Tyka" <m.****@gnx.net> wrote in message news:I4********@bath.ac.uk...
Hello community,

i'm fairly new to using the STL but i've been experimenting a bit with it.
I tried to derive a new class say MyString from string like so:

class MyString: public string{
public:
MyString(){}
};

<snip>

I always feel the urge to say this, so I'm saying it. First tip about the
standard container classes: they were not designed for inheritance. The
container classes have no virtual member functions (even the important
destructor) and no standard protected members. If MyString is just going to
hold a bunch of utility functions for std::string, then just use non-member
functions (possibly in a namespace) instead.

--
David Hilsee
Jul 22 '05 #5
David Hilsee wrote:
"Mike Tyka" <m.****@gnx.net> wrote in message news:I4********@bath.ac.uk...
Hello community,

i'm fairly new to using the STL but i've been experimenting a bit with it.
I tried to derive a new class say MyString from string like so:

class MyString: public string{
public:
MyString(){}
};


<snip>

I always feel the urge to say this, so I'm saying it. First tip about the
standard container classes: they were not designed for inheritance. The
container classes have no virtual member functions (even the important
destructor) and no standard protected members. If MyString is just going to
hold a bunch of utility functions for std::string, then just use non-member
functions (possibly in a namespace) instead.

Or you could think of 'aggregation' relationships a.k.a member
relationships.

class MyString {
public:

MyString(const string & ref) : str(ref);
// Methods that operate on the member variable 'str'.
// ......................

private:

string str;
};

--
Karthik.
http://akktech.blogspot.com .
Jul 22 '05 #6

"Rob Williscroft" <rt*@freenet.co.uk> skrev i en meddelelse
news:Xn**********************************@130.133. 1.4...
Peter Koch Larsen wrote in news:K7f7d.54262$Vf.2607123
@news000.worldonline.dk in comp.lang.c++:
operator=() is not directly inherited by the derived class, it is the
standard C++ behaviour (IMHO)

Fabio


Rubbish! Of course it is.


Not so, Fabio is correct (*), operator = if not declared by the user
is generated by the compiler (uses member/base-wise assignment),
hence the compiler generated operator = hides the base class
operator =.

*) This depends on the interpretation of "directly inherited"
of course :).


I believe we are in agreement here. To sum it up: operator= behaves exactly
like any other member function or operator, but since the compiler generates
its own operator= you can not use it without some trickery.

/Peter

[snip]
Jul 22 '05 #7
Karthik Kumar <ka*******************@yahoo.com> wrote in message news:<415e46b5$1@darkstar>...
David Hilsee wrote:
"Mike Tyka" <m.****@gnx.net> wrote in message news:I4********@bath.ac.uk...
Hello community,

i'm fairly new to using the STL but i've been experimenting a bit with it.
I tried to derive a new class say MyString from string like so:

class MyString: public string{
public:
MyString(){}
};


<snip>

I always feel the urge to say this, so I'm saying it. First tip about the
standard container classes: they were not designed for inheritance. The
container classes have no virtual member functions (even the important
destructor) and no standard protected members. If MyString is just going to
hold a bunch of utility functions for std::string, then just use non-member
functions (possibly in a namespace) instead.

Or you could think of 'aggregation' relationships a.k.a member
relationships.

class MyString {
public:

MyString(const string & ref) : str(ref);
// Methods that operate on the member variable 'str'.
// ......................

private:

string str;
};


Then you have to explicitly forward any parts of std::string's
interface that you want MyString to have. If you want a restricted
interface (like the standard sequence adapters) this might be the
right solution.

But if you want all the functionality of a std container plus some
more of your own, and your OO design[*] calls for a new type to
provide this functionality, then inheriting from the container might
be the simplest solution.

The only thing you absolutely MUST not do is delete the derived
objects polymorphically because the base class doesn't have a virtual
destructor. But as long as you understand that, then there's no
problem. As a practical matter, you won't be able to do anything
polymorphically because the std containers don't have any virtual
functions. But in this case polymorphism isn't the goal - a new class
with the container's functionality plus more of your own is.
[*] I'm assuming that if you're considering inheritance, you're
thinking OO.

GJD
Jul 22 '05 #8
"Gavin Deane" <de*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
Karthik Kumar <ka*******************@yahoo.com> wrote in message news:<415e46b5$1@darkstar>...
David Hilsee wrote:
"Mike Tyka" <m.****@gnx.net> wrote in message news:I4********@bath.ac.uk...
>Hello community,
>
>i'm fairly new to using the STL but i've been experimenting a bit with it.>I tried to derive a new class say MyString from string like so:
>
>class MyString: public string{
> public:
> MyString(){}
>};

<snip>

I always feel the urge to say this, so I'm saying it. First tip about the standard container classes: they were not designed for inheritance. The container classes have no virtual member functions (even the important
destructor) and no standard protected members. If MyString is just going to hold a bunch of utility functions for std::string, then just use non-member functions (possibly in a namespace) instead.

Or you could think of 'aggregation' relationships a.k.a member
relationships.

class MyString {
public:

MyString(const string & ref) : str(ref);
// Methods that operate on the member variable 'str'.
// ......................

private:

string str;
};


Then you have to explicitly forward any parts of std::string's
interface that you want MyString to have. If you want a restricted
interface (like the standard sequence adapters) this might be the
right solution.

But if you want all the functionality of a std container plus some
more of your own, and your OO design[*] calls for a new type to
provide this functionality, then inheriting from the container might
be the simplest solution.


I have not yet seen any "OO design" that irrefutably called for inheritance
from a standard container. If utility functions for std::string are
desired, they do not need to be written in a derived class. In fact, it is
better if they are not, because they are more easily reused in programs that
use std::string and not MyString. Most of the time, this is what is
desired. If the intention is to provide a string class that could later
have its std::string representation replaced, then the std::string should
probably be a private member.
The only thing you absolutely MUST not do is delete the derived
objects polymorphically because the base class doesn't have a virtual
destructor. But as long as you understand that, then there's no
problem. As a practical matter, you won't be able to do anything
polymorphically because the std containers don't have any virtual
functions. But in this case polymorphism isn't the goal - a new class
with the container's functionality plus more of your own is.


I'm not sure if that's best described as a goal or as a solution to a
problem, and a solution that usually has better alternatives at that.

If you honestly need a class that publicly exposes std::string's interface
and has more functionality thrown in, then I think the following is a little
more "honest":

class MyString {
public:
// constructors, etc
std::string s;
// additional stuff here
};

To access the string, simply write foo.s.blah() instead of foo.blah(). This
avoids forwarding functions at the cost of two extra keystrokes. It also
avoids the "delete pString" issue. Most people would say that it doesn't
feel right, but it's about the same as public inheritance from the
std::string.

--
David Hilsee
Jul 22 '05 #9

"David Hilsee" <da*************@yahoo.com> wrote in message
news:0a********************@comcast.com...
"Gavin Deane" <de*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
Karthik Kumar <ka*******************@yahoo.com> wrote in message news:<415e46b5$1@darkstar>...
David Hilsee wrote:
> "Mike Tyka" <m.****@gnx.net> wrote in message news:I4********@bath.ac.uk... >
>>Hello community,
>>
>>i'm fairly new to using the STL but i've been experimenting a bit with it.
>>I tried to derive a new class say MyString from string like so:
>>
>>class MyString: public string{
>> public:
>> MyString(){}
>>};
>
> <snip>
>
> I always feel the urge to say this, so I'm saying it. First tip
about
the > standard container classes: they were not designed for inheritance. The > container classes have no virtual member functions (even the
important > destructor) and no standard protected members. If MyString is just
going to > hold a bunch of utility functions for std::string, then just use non-member > functions (possibly in a namespace) instead.
>
Or you could think of 'aggregation' relationships a.k.a member
relationships.

class MyString {
public:

MyString(const string & ref) : str(ref);
// Methods that operate on the member variable 'str'.
// ......................

private:

string str;
};


Then you have to explicitly forward any parts of std::string's
interface that you want MyString to have. If you want a restricted
interface (like the standard sequence adapters) this might be the
right solution.

But if you want all the functionality of a std container plus some
more of your own, and your OO design[*] calls for a new type to
provide this functionality, then inheriting from the container might
be the simplest solution.


I have not yet seen any "OO design" that irrefutably called for

inheritance from a standard container. If utility functions for std::string are
desired, they do not need to be written in a derived class. In fact, it is better if they are not, because they are more easily reused in programs that use std::string and not MyString. Most of the time, this is what is
desired. If the intention is to provide a string class that could later
have its std::string representation replaced, then the std::string should
probably be a private member.
The only thing you absolutely MUST not do is delete the derived
objects polymorphically because the base class doesn't have a virtual
destructor. But as long as you understand that, then there's no
problem. As a practical matter, you won't be able to do anything
polymorphically because the std containers don't have any virtual
functions. But in this case polymorphism isn't the goal - a new class
with the container's functionality plus more of your own is.
I'm not sure if that's best described as a goal or as a solution to a
problem, and a solution that usually has better alternatives at that.

If you honestly need a class that publicly exposes std::string's interface
and has more functionality thrown in, then I think the following is a

little more "honest":

class MyString {
public:
// constructors, etc
std::string s;
// additional stuff here
};

To access the string, simply write foo.s.blah() instead of foo.blah(). This avoids forwarding functions at the cost of two extra keystrokes. It also
avoids the "delete pString" issue. Most people would say that it doesn't
feel right, but it's about the same as public inheritance from the
std::string.

--
David Hilsee


Thanks a lot for all your answers, i think i'm slowly getting my head round
this stuff.. I kept the example generic for simplicity's sake.
I want to create a data structure and a correcponding mask data structure,
both of
which have all of the properties of a string, and it would be useful to have
all it's functionality
(like cutting, concatenating etc etc). At the same time both data structures
will want to have
additional functionality, specialised to the type of data they're holding,
but no more additional
data - so the lack of virtual destructors etc shouldnt matter !?Hence the
inherited approach seems
sensible in this case ?

Am i thinking along the right lines here ?

Mike :)


Jul 22 '05 #10
"Mike Tyka" <m.****@gnx.net> wrote in message news:I5********@bath.ac.uk...
<snip>
Thanks a lot for all your answers, i think i'm slowly getting my head round this stuff.. I kept the example generic for simplicity's sake.
I want to create a data structure and a correcponding mask data structure,
both of
which have all of the properties of a string, and it would be useful to have all it's functionality
(like cutting, concatenating etc etc). At the same time both data structures will want to have
additional functionality, specialised to the type of data they're holding,
but no more additional
data - so the lack of virtual destructors etc shouldnt matter !?Hence the
inherited approach seems
sensible in this case ?

Am i thinking along the right lines here ?


Even if the derived class contains no additional data, it's still undefined
behavior if you pass delete a std::string pointer when it is a pointer to an
instance of the derived class. Of course, you can avoid doing that in your
programs, and it's hard to accidentally do it simply because std::string has
no virtual member functions.

I tend to gripe about public inheritance of standard containers because it
is using inheritance to expose an implementation detail as a "nameless
public member". Many people choose to do that instead of having a public
member because inheritance is generally considered "better" than public
members. Unfortunately, in this case, it's roughly the same thing. It's a
general problem when inheritance is used for code reuse purposes (discussed
in the FAQ a bit in the Smalltalk/C++ comparison -- 30.4). However, for
most smaller programs, it doesn't really matter.

--
David Hilsee
Jul 22 '05 #11
"David Hilsee" <da*************@yahoo.com> wrote in message news:<0a********************@comcast.com>...
"Gavin Deane" <de*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
Karthik Kumar <ka*******************@yahoo.com> wrote in message news:<415e46b5$1@darkstar>...
David Hilsee wrote:
> "Mike Tyka" <m.****@gnx.net> wrote in message news:I4********@bath.ac.uk...


<snip deriving MyString from std::string>
> I always feel the urge to say this, so I'm saying it. First tip about the > standard container classes: they were not designed for inheritance. The > container classes have no virtual member functions (even the important
> destructor) and no standard protected members. If MyString is just going to > hold a bunch of utility functions for std::string, then just use non-member > functions (possibly in a namespace) instead.
>
Or you could think of 'aggregation' relationships a.k.a member
relationships.

class MyString {
public:
MyString(const string & ref) : str(ref);
// Methods that operate on the member variable 'str'.
// ......................
private:
string str;
};


Then you have to explicitly forward any parts of std::string's
interface that you want MyString to have. If you want a restricted
interface (like the standard sequence adapters) this might be the
right solution.

But if you want all the functionality of a std container plus some
more of your own, and your OO design[*] calls for a new type to
provide this functionality, then inheriting from the container might
be the simplest solution.


I have not yet seen any "OO design" that irrefutably called for inheritance
from a standard container.


Very few design decisions are irrefutable. There are almost always
alternatives.

There is some blurring going on here between strings, where the OP
started, and the standard containers, which came up during the
discussion, and are much more general purpose than std::string.

I have never needed to create my own string type. Indeed I have
suffered in situations where too many different string types coexist.
But I have on a number of occasions wanted to add functionality to a
continer for a specific purpose.
If utility functions for std::string are
desired, they do not need to be written in a derived class. In fact, it is
better if they are not, because they are more easily reused in programs that
use std::string and not MyString. Most of the time, this is what is
desired.
Again, thinking containers rather than strings ...
I absolutely agree with that, but I'm not talking about utility
functions that could meaningfully apply to any container of the right
type.

As an example, I wrote a piece of software that communicated with some
electronics by reading and writing sequences of bytes over a serial
port. I wanted the messages in a std::vector<char> because I needed to
do vector-related things like iterating, push_backing etc. But I also
needed functionality very specific to my messages - parsing, building
up parts of the message from field specifier objects I had, for
example.

I could have done it all with free functions that took a vector<char>
and operated on it via its public interface only. But that seemed
wrong because it would make absolutley no sense to try and parse
something that happens to be a vector<char> but isn't one of my
messages.

What I really wanted was something like
typedef std::vector<char> my_message_type;
where typedef really does define a new and distinct type. But the
language doesn't provide that.
If the intention is to provide a string class that could later
have its std::string representation replaced, then the std::string should
probably be a private member.
Agreed. But that wasn't my intention either.
The only thing you absolutely MUST not do is delete the derived
objects polymorphically because the base class doesn't have a virtual
destructor. But as long as you understand that, then there's no
problem. As a practical matter, you won't be able to do anything
polymorphically because the std containers don't have any virtual
functions. But in this case polymorphism isn't the goal - a new class
with the container's functionality plus more of your own is.


I'm not sure if that's best described as a goal or as a solution to a
problem, and a solution that usually has better alternatives at that.


Well in the project I described above, the messages were the whole
point of the work. A type that could represent the messages was the
fundamental design goal.

In other similar situations I've tried alternatives and never come
across one that's as clean as inheritance.
If you honestly need a class that publicly exposes std::string's interface
and has more functionality thrown in, then I think the following is a little
more "honest":

class MyString {
public:
// constructors, etc
std::string s;
// additional stuff here
};

To access the string, simply write foo.s.blah() instead of foo.blah(). This
avoids forwarding functions at the cost of two extra keystrokes. It also
avoids the "delete pString" issue. Most people would say that it doesn't
feel right, but it's about the same as public inheritance from the
std::string.


I've never tried this idea, but to me personally it definitely doesn't
feel right, whereas inheritance does.
Jul 22 '05 #12
"Gavin Deane" <de*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
"David Hilsee" <da*************@yahoo.com> wrote in message

news:<0a********************@comcast.com>...
<snip>
If utility functions for std::string are
desired, they do not need to be written in a derived class. In fact, it is better if they are not, because they are more easily reused in programs that use std::string and not MyString. Most of the time, this is what is
desired.


Again, thinking containers rather than strings ...
I absolutely agree with that, but I'm not talking about utility
functions that could meaningfully apply to any container of the right
type.

As an example, I wrote a piece of software that communicated with some
electronics by reading and writing sequences of bytes over a serial
port. I wanted the messages in a std::vector<char> because I needed to
do vector-related things like iterating, push_backing etc. But I also
needed functionality very specific to my messages - parsing, building
up parts of the message from field specifier objects I had, for
example.

I could have done it all with free functions that took a vector<char>
and operated on it via its public interface only. But that seemed
wrong because it would make absolutley no sense to try and parse
something that happens to be a vector<char> but isn't one of my
messages.

What I really wanted was something like
typedef std::vector<char> my_message_type;
where typedef really does define a new and distinct type. But the
language doesn't provide that.


I don't think the distinct type is necessary. The std::vector<char> base
would be publicly exposed for anyone to modify in any way they please. Its
contents could easily (and accidentally) be replaced with an arbitrary
sequence of characters that isn't one of your messages, and there's nothing
the derived class can do about that. The distinct type doesn't offer any
significant benefit that I can see. I'm not really sure what you mean by
"doesn't make sense". Maybe you meant "I'm never going to do that"? If
there is an invalid sequence of characters that "doesn't make sense" for
your messages, then I would revisit the private member solution.

<snip>
If you honestly need a class that publicly exposes std::string's interface and has more functionality thrown in, then I think the following is a little more "honest":

class MyString {
public:
// constructors, etc
std::string s;
// additional stuff here
};

To access the string, simply write foo.s.blah() instead of foo.blah(). This avoids forwarding functions at the cost of two extra keystrokes. It also avoids the "delete pString" issue. Most people would say that it doesn't feel right, but it's about the same as public inheritance from the
std::string.


I've never tried this idea, but to me personally it definitely doesn't
feel right, whereas inheritance does.


Many people would say that, and I think that might have something to do with
all of the "OO hype" that people hear from time to time. I'd argue that
they're about the same.

--
David Hilsee
Jul 22 '05 #13
"David Hilsee" <da*************@yahoo.com> wrote in message news:<3p********************@comcast.com>...
"Gavin Deane" <de*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
"David Hilsee" <da*************@yahoo.com> wrote in message news:<0a********************@comcast.com>...
<snip>
If utility functions for std::string are
desired, they do not need to be written in a derived class. In fact, it is better if they are not, because they are more easily reused in programs that use std::string and not MyString. Most of the time, this is what is
desired.


Again, thinking containers rather than strings ...
I absolutely agree with that, but I'm not talking about utility
functions that could meaningfully apply to any container of the right
type.

As an example, I wrote a piece of software that communicated with some
electronics by reading and writing sequences of bytes over a serial
port. I wanted the messages in a std::vector<char> because I needed to
do vector-related things like iterating, push_backing etc. But I also
needed functionality very specific to my messages - parsing, building
up parts of the message from field specifier objects I had, for
example.

I could have done it all with free functions that took a vector<char>
and operated on it via its public interface only. But that seemed
wrong because it would make absolutley no sense to try and parse
something that happens to be a vector<char> but isn't one of my
messages.

What I really wanted was something like
typedef std::vector<char> my_message_type;
where typedef really does define a new and distinct type. But the
language doesn't provide that.


I don't think the distinct type is necessary.


It makes the code more closely model the problem, making the intent
clearer.

some_return_type parse_message(const std::vector<char>& msg, ...) {
.... }

doesn't make sense where

some_return_type parse_message(const my_message_type& msg, ...) { ...
}

does because the function isn't for parsing a vector<char>, it's for
parsing a my_message_type. Again, my ideal solution would be a strong
typedef but we don't have those.
The std::vector<char> base
would be publicly exposed for anyone to modify in any way they please. Its
contents could easily (and accidentally) be replaced with an arbitrary
sequence of characters that isn't one of your messages, and there's nothing
the derived class can do about that.
The same risk exists with a public member or using a vector directly.
So here inheritance doesn't add anything, but it doesn't lose anything
either.
The distinct type doesn't offer any
significant benefit that I can see. I'm not really sure what you mean by
"doesn't make sense". Maybe you meant "I'm never going to do that"? If
there is an invalid sequence of characters that "doesn't make sense" for
your messages, then I would revisit the private member solution.


Whether the sequence of characters in the message object makes sense
has nothing to do with why I want a distinct type. There may (now or
in the future) be other vector<char> objects around that are used for
completely different things. It seems preferable to me to be able to
write a parse function that can only accept my messages (which may or
may not turn out to be full of garbage). Without a distinct type, I'd
be able to call the parse function for a vector<char> that wasn't a
message, which would make no more sense that calling it for an int.

<snip>
Jul 22 '05 #14
"Mike Tyka" <m.****@gnx.net> wrote:
Thanks a lot for all your answers, i think i'm slowly getting my head round
this stuff.. I kept the example generic for simplicity's sake.
I want to create a data structure and a correcponding mask data structure,
both of
which have all of the properties of a string, and it would be useful to have
all it's functionality
(like cutting, concatenating etc etc). At the same time both data structures
will want to have
additional functionality, specialised to the type of data they're holding,
but no more additional
data - so the lack of virtual destructors etc shouldnt matter !?Hence the
inherited approach seems
sensible in this case ?
Is it the case that any sequence of characters that can be held in a
std::string, can also be held (and make sense) in one of your
specialized types?

If yes, then your specialized type isn't adding anything new, just use a
std::string. If no, then publicly inheriting from std::string is
dangerous because it exposes your type to misuse.

Either use private inheritance, or containment in the latter case.

Am i thinking along the right lines here ?


Ask yourself, "what are the invariants that exist for my specialized
type that are different than those that exist for std::string?" Then you
will be thinking along the right lines.
Jul 22 '05 #15
"Gavin Deane" <de*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
<snip>
Whether the sequence of characters in the message object makes sense
has nothing to do with why I want a distinct type. There may (now or
in the future) be other vector<char> objects around that are used for
completely different things. It seems preferable to me to be able to
write a parse function that can only accept my messages (which may or
may not turn out to be full of garbage). Without a distinct type, I'd
be able to call the parse function for a vector<char> that wasn't a
message, which would make no more sense that calling it for an int.


My point is that even with the new type, after a simple assignment, your
functions can be called for a std::vector<char> that wasn't a message. It
offers about as much protection as the typedef, so I didn't see any benefit
of a separate class over a typedef. It might guard against cases where the
programmer _accidentally_ passed your function the wrong std::vector<char>,
but I can't imagine that happening much, in practice. If your functions
work for any std::vector<char> (as they should, given the public
inheritance), then I'd just use a typedef to express the intent that it
should only be used for messages.

--
David Hilsee
Jul 22 '05 #16
> But if you want all the functionality of a std container plus some
more of your own, and your OO design[*] calls for a new type to
provide this functionality, then inheriting from the container might
be the simplest solution.


These templates are not designed so that they can be used as base
classes for polymorphism.
Their intended use is for value objects.
http://c2.com/cgi/wiki?ValueObject
Jul 22 '05 #17
> [...]
The only thing you absolutely MUST not do is delete the derived
objects polymorphically because the base class doesn't have a virtual
destructor. But as long as you understand that, then there's no
problem. As a practical matter, you won't be able to do anything
polymorphically because the std containers don't have any virtual
functions. [...]


Should the standard collections be treated as final classes like it
can be specified with a key word in Java?
Jul 22 '05 #18
Ma************@web.de (Markus Elfring) wrote in message news:<40**************************@posting.google. com>...
But if you want all the functionality of a std container plus some
more of your own, and your OO design[*] calls for a new type to
provide this functionality, then inheriting from the container might
be the simplest solution.


These templates are not designed so that they can be used as base
classes for polymorphism.
Their intended use is for value objects.
http://c2.com/cgi/wiki?ValueObject


Where do I mention polymorphism in the passage you quoted? If you read
the rest of the discussion you'll see that I wasn't talking about
using inheritance to get polymorhic behaviour at all.

In fact, in part my post that you snipped, I specifically said that
you wouldn't be able to use a classes derived from containers
polymorhically because containers have no virtual functions.
Jul 22 '05 #19

"Fabio" <fa*************@libero.it> wrote in message news:2s*************@uni-berlin.de...

operator=() is not directly inherited by the derived class, it is the
standard C++ behaviour (IMHO)

Standard behavior. The reason is simple. If you don't declare an operator=,
the compiler implicitly generates one for you. That hides any base class operator=.
Of course, if you do declare an operator=, it hides a base class operator=
so the effect is the same.

Jul 22 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.