inherit WITH operators

Discussion in 'C++' started by Raf256, Dec 18, 2005.

  1. Raf256

    Raf256 Guest

    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?

    --
    Rafa³ Maj
    Raf256, Dec 18, 2005
    #1
    1. Advertising

  2. 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::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
    Peter Jansson, Dec 18, 2005
    #2
    1. Advertising

  3. Raf256

    Raf256 Guest

    Peter Jansson <5eipf.40994$> 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
    Raf256, Dec 18, 2005
    #3
  4. Raf256

    Raf256 Guest

    Peter Jansson <5eipf.40994$> 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::eek:perator=; // <--------
    void SaveToDatabase();
    };


    --
    Rafa³ Maj
    Raf256, Dec 18, 2005
    #4
  5. Raf256

    Zara Guest

    On Sun, 18 Dec 2005 19:26:04 +0100, 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::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
    Zara, Dec 19, 2005
    #5
  6. Raf256

    Neil Cerutti Guest

    On 2005-12-18, 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::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.

    --
    Neil Cerutti
    Neil Cerutti, Dec 19, 2005
    #6
  7. Raf256

    Neil Cerutti Guest

    On 2005-12-19, Neil Cerutti <> 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
    Neil Cerutti, Dec 19, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. varun varun
    Replies:
    0
    Views:
    398
    varun varun
    Jul 3, 2003
  2. Frank

    inherit DataColumnCollection

    Frank, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    343
    Frank
    Jul 8, 2003
  3. DesignerX
    Replies:
    0
    Views:
    353
    DesignerX
    Aug 7, 2003
  4. Ben Mann

    ASP .Net PageBase Inherit

    Ben Mann, Oct 22, 2003, in forum: ASP .Net
    Replies:
    14
    Views:
    4,598
    John Soulis [MSFT]
    Oct 24, 2003
  5. Flare

    Inherit from soapException

    Flare, Oct 23, 2003, in forum: ASP .Net
    Replies:
    9
    Views:
    2,354
    Kevin Spencer
    Oct 24, 2003
Loading...

Share This Page