473,468 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

inherit WITH operators


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
6 2006
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
1
by: JKop | last post by:
First attempt: class Blah : public int { }; So I began working on the next best thing:
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
44
by: Frank Rizzo | last post by:
Any ideas?
9
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! Is it possible to alter the string type? Basicly what i want to do is to create my own string type. lets call it sqlString, when i use this it will always use replace on the string...
2
by: Christoph Heindl | last post by:
Hi, I'm currently struggling with a design decision. One of my classes (named Attributable) has a protected member of type std::list<StrongReference<Attribute> > Basically the list has...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
7
by: libsfan01 | last post by:
Hi all This js i wrote to switch display on and off of a given element does not work in ie but it does in ff and safari. how can i make it work with ie guys? <script...
2
by: GTalbot | last post by:
Hello fellow comp.infosystems.www.authoring.stylesheets colleagues, Imagine this situation: #grand-parent-abs-pos { height: 400px; position: absolute; width: 600px; }
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.