inherit WITH operators

R

Raf256

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::eek:perator=;

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?
 
P

Peter Jansson

Raf256 said:
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::eek:perator=;

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
 
R

Raf256

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();
};
 
R

Raf256

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::eek:perator=; // <--------
void SaveToDatabase();
};
 
Z

Zara

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::eek:perator=;

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::eek:perator=(other);mDatabase=other.mDatabase;}
cStorableStr& operator=(const std::stringr& other)
{std::string::eek:perator=(other);mDatabase=default_mDatabase;}

void SaveToDatabase();
};

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

Regards

-- Zara
 
N

Neil Cerutti

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::eek:perator=;

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.
 
N

Neil Cerutti

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top