By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

inherit WITH operators

P: n/a

Hi,
I would extend class only a bit. In example: I want to write my own class,
that works all like std::string, only it also have member int mDatabase;
and it have method SaveToDatabase();

I could do like:

class cStorableStr : std::string {
private:
int mDatabase;
public:
std::string::operator=;

void SaveToDatabase();
};

that inherits all methods from std::string, and operator(s) =.

But what if I would like to just unherit ALL. ALL operators, or ALL
members+operators. Is there a syntax allowing that?

Like:
public: std::string::*;

If not, perhaps it would be nice to have it?

--
Rafał Maj
Dec 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Raf256 wrote:
Hi,
I would extend class only a bit. In example: I want to write my own class,
that works all like std::string, only it also have member int mDatabase;
and it have method SaveToDatabase();

I could do like:

class cStorableStr : std::string {
private:
int mDatabase;
public:
std::string::operator=;

void SaveToDatabase();
};

that inherits all methods from std::string, and operator(s) =.

But what if I would like to just unherit ALL. ALL operators, or ALL
members+operators. Is there a syntax allowing that?

Like:
public: std::string::*;

If not, perhaps it would be nice to have it?


Hi,
If you design it like the following, then don't you inherit everything
than can be inherited, plus the extra member and member method?

class cStorableStr: public std::string
{
private:
int mDatabase;
public:
void SaveToDatabase();
};

Regards,
Peter Jansson
Dec 18 '05 #2

P: n/a
Peter Jansson <5e*******************@newsb.telia.net> Sunday 18 of December
2005 19:52
If you design it like the following, then don't you inherit everything
than can be inherited, plus the extra member and member method?

class cStorableStr: public std::string
{
private:
int mDatabase;
public:
void SaveToDatabase();
};


But that do not inherit the operators.

Btw, in example above I forget "using", it should go:

class cStorableStr: public std::string
{
private:
int mDatabase;
public:
using std::string=; // <--------
void SaveToDatabase();
};
--
Rafał Maj
Dec 18 '05 #3

P: n/a
Peter Jansson <5e*******************@newsb.telia.net> Sunday 18 of December
2005 19:52
If you design it like the following, then don't you inherit everything
than can be inherited, plus the extra member and member method?

class cStorableStr: public std::string
{
private:
int mDatabase;
public:
void SaveToDatabase();
};


But that do not inherit the operators.

Btw, in example above I forget "using", it should go:

class cStorableStr: public std::string
{
private:
int mDatabase;
public:
using std::string::operator=; // <--------
void SaveToDatabase();
};
--
Rafał Maj
Dec 18 '05 #4

P: n/a
On Sun, 18 Dec 2005 19:26:04 +0100, Raf256 <sp**@raf256.com.invalid>
wrote:

Hi,
I would extend class only a bit. In example: I want to write my own class,
that works all like std::string, only it also have member int mDatabase;
and it have method SaveToDatabase();

I could do like:

class cStorableStr : std::string {
private:
int mDatabase;
public:
std::string::operator=;

void SaveToDatabase();
};

that inherits all methods from std::string, and operator(s) =.

But what if I would like to just unherit ALL. ALL operators, or ALL
members+operators. Is there a syntax allowing that?

Like:
public: std::string::*;

If not, perhaps it would be nice to have it?

You should inherit publicly from string, and then write the
non-inheritable member functions: default constructor, copy
constructor and copy assignment, and supply the assignment of string
and constructor from string:

class cStorableStr : std::string {
private:
int mDatabase;
public:
enum {default_mDatabase=;}0;
cStorableStr():mDatabase(default_mDatabase) {}
cStorableStr(const cStorableStr& other)
:std::string(other),mDatabase(other.mDatabase) {}
cStorableStr& operator=(const cStorableStr& other)
{std::string::operator=(other);mDatabase=other.mDa tabase;}
cStorableStr& operator=(const std::stringr& other)
{std::string::operator=(other);mDatabase=default_m Database;}

void SaveToDatabase();
};

Be careful when using this class, as std::basic_string does not have a
virtual destructor.

Regards

-- Zara
Dec 19 '05 #5

P: n/a
On 2005-12-18, Raf256 <sp**@raf256.com.invalid> wrote:
Hi,
I would extend class only a bit. In example: I want to write my
own class, that works all like std::string, only it also have
member int mDatabase; and it have method SaveToDatabase();

I could do like:

class cStorableStr : std::string {
private:
int mDatabase;
public:
std::string::operator=;

void SaveToDatabase();
};

that inherits all methods from std::string, and operator(s) =.

But what if I would like to just unherit ALL. ALL operators, or
ALL members+operators. Is there a syntax allowing that?
Unfortunately, no.

In general, the variant of OOP that C++ supports requires design
of class hierarchies from base classes on up. You cannot usually
take an arbitrary class, which wasn't intended to be a base
class, and extend it using inheritance.
Like:
public: std::string::*;

If not, perhaps it would be nice to have it?


You need to use aggregation, not public inheritance. Then provide
inline functions that forward to the contained object. This
provides exactly what you're asking for, though not a convenient
short-hand for it.

--
Neil Cerutti
Dec 19 '05 #6

P: n/a
On 2005-12-19, Neil Cerutti <le*******@email.com> wrote:
You need to use aggregation, not public inheritance. Then
provide inline functions that forward to the contained object.
This provides exactly what you're asking for, though not a
convenient short-hand for it.


One other option is to provide functions that operate on
std::strings to provide the extra functionality you need.

--
Neil Cerutti
Dec 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.