Operator= can't be inherited.

Discussion in 'C++' started by Nephi Immortal, Feb 10, 2011.

  1. Please explain why operator= cannot be inherited into class B from
    class A. Is it because the left reference before operator= is type A,
    but not type B? Then, do I have to define a copy of operator= and
    replace left reference to type B in class B body?
    It is annoying to redefine operator= every time I create several new
    classes that are derived from class A.

    class A {
    public:
    A() : m_data( 0 ) {}
    ~A() {}

    A &operator=( int byte ) {
    m_data = byte;
    return *this;
    }

    int m_data;
    };

    class B : public A {
    public:
    B() : A() {}
    ~B() {}

    };

    int main () {
    A a;
    a = 5; // ok

    B b;
    b = 7; // error

    return 0;
    }
    Nephi Immortal, Feb 10, 2011
    #1
    1. Advertising

  2. Nephi Immortal

    Öö Tiib Guest

    On Feb 10, 7:06 am, Nephi Immortal <> wrote:
    >         Please explain why operator= cannot be inherited into class B from
    > class A.  Is it because the left reference before operator= is type A,
    > but not type B?  Then, do I have to define a copy of operator= and
    > replace left reference to type B in class B body?
    >         It is annoying to redefine operator= every time I create several new
    > classes that are derived from class A.


    That is because operator=() is special. When you do not declare copy-
    assignment for a class then compiler declares one for you. Therefore
    there will be always one operator=() present (copy-assignment) for a
    class.

    That always present T& T::eek:perator=(T const&) will hide all the B&
    B::eek:perator=(Something) declared by base classes. C++ may not find
    them because they are hidden.
    Öö Tiib, Feb 10, 2011
    #2
    1. Advertising

  3. Nephi Immortal

    Paul Guest

    "Nephi Immortal" <> wrote in message
    news:...
    > Please explain why operator= cannot be inherited into class B from
    > class A.

    Inheritance works the other way.
    A doesn't inherit from B, B inherits from A.

    > Is it because the left reference before operator= is type A,
    > but not type B? Then, do I have to define a copy of operator= and
    > replace left reference to type B in class B body?
    > It is annoying to redefine operator= every time I create several new
    > classes that are derived from class A.


    You also need to overload these operators to allow for:
    bObject1 = bObject2;
    if you want this behaviour.
    With your code your "right hand side" operand can only be an int type.

    It looks like you'd be better off with using/setter methods.
    If you do this you should also consider making these virtual so you have the
    following behaviour:
    A* a_ptr;

    if( whatever) a_ptr=new(B);
    else a_ptr=new(C);

    int x = a_ptr->get_mData();
    /*If you made get_mData virtual in A, and implemented it in each derived
    dclass,then the correct function will will be called whether it's a B or C
    type*/

    The above demonstrates the importance of understanding the direction of
    inheritance.
    The same can be done with operator=() functions but complications arise as
    the return type differs with inheritance. You need to write an operator=
    function for each type, if you want that kind of behaviour. There is no easy
    solution.


    HTH
    Paul, Feb 10, 2011
    #3
  4. Nephi Immortal

    James Kanze Guest

    On Feb 10, 5:06 am, Nephi Immortal <> wrote:
    > Please explain why operator= cannot be inherited into class
    > B from class A.


    For the same reason other functions aren't be inherited if you
    define a function with the same name in the derived class.

    > Is it because the left reference before operator= is type A,
    > but not type B? Then, do I have to define a copy of operator=
    > and replace left reference to type B in class B body?


    It's more because every class has an operator=; if you don't
    define one, the compiler will do it for you.

    > It is annoying to redefine operator= every time I create
    > several new classes that are derived from class A.


    I'm not sure what you're trying to accomplish, but assignment
    and inheritance don't generally work well together to begin
    with.

    --
    James Kanze
    James Kanze, Feb 10, 2011
    #4
  5. On Feb 10, 7:09 am, James Kanze <> wrote:
    > On Feb 10, 5:06 am, Nephi Immortal <> wrote:
    >
    > > Please explain why operator= cannot be inherited into class
    > > B from class A.

    >
    > For the same reason other functions aren't be inherited if you
    > define a function with the same name in the derived class.
    >
    > > Is it because the left reference before operator= is type A,
    > > but not type B?  Then, do I have to define a copy of operator=
    > > and replace left reference to type B in class B body?

    >
    > It's more because every class has an operator=; if you don't
    > define one, the compiler will do it for you.
    >
    > > It is annoying to redefine operator= every time I create
    > > several new classes that are derived from class A.

    >
    > I'm not sure what you're trying to accomplish, but assignment
    > and inheritance don't generally work well together to begin
    > with.


    Ok. I understand, but I want to ask why operator= can’t be
    inherited.

    For example, Object &operator=( const Object & ) cannot be inherited
    and it is provided to all classes by C++ Compiler unless you define
    your own operator= explicitly.

    Now, I am asking. What about Object &operator=( const int )? It is
    not provided by C++ Compiler and it should be inherited.

    Also, and what about operator int() as cast operator? Can it be
    inherited?

    And what about other operators such as operator[], operator++, and
    operator+=?
    Nephi Immortal, Feb 11, 2011
    #5
  6. Nephi Immortal

    James Kanze Guest

    On Feb 11, 3:42 am, Nephi Immortal <> wrote:
    > On Feb 10, 7:09 am, James Kanze <> wrote:
    > > On Feb 10, 5:06 am, Nephi Immortal <> wrote:


    > > > Please explain why operator= cannot be inherited into class
    > > > B from class A.


    > > For the same reason other functions aren't be inherited if you
    > > define a function with the same name in the derived class.


    > > > Is it because the left reference before operator= is type A,
    > > > but not type B? Then, do I have to define a copy of operator=
    > > > and replace left reference to type B in class B body?


    > > It's more because every class has an operator=; if you don't
    > > define one, the compiler will do it for you.


    > > > It is annoying to redefine operator= every time I create
    > > > several new classes that are derived from class A.


    > > I'm not sure what you're trying to accomplish, but assignment
    > > and inheritance don't generally work well together to begin
    > > with.


    > Ok. I understand, but I want to ask why operator= can’t be
    > inherited.


    For the same reason no function is inherited if you declare
    a function of the same name in the derived class. operator=
    follows exactly the same rules as every other function.

    Consider:

    struct Base
    {
    void f(int);
    void f(std::string const&);
    };

    struct Derived : Base
    {
    void f(std::string const&);
    };

    int
    main()
    {
    Derived d;
    d.f(1);
    return 0;
    }

    Doesn't compile either. (Worse, d.f(0) compiles, but crashes at
    runtime, because it tries to convert the null pointer constant
    into a string, in order to call d.f(std::string const&).)

    See http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.9..

    > For example, Object &operator=( const Object & ) cannot be inherited
    > and it is provided to all classes by C++ Compiler unless you define
    > your own operator= explicitly.


    > Now, I am asking. What about Object &operator=( const int )? It is
    > not provided by C++ Compiler and it should be inherited.


    Declaring any function with a given names in a derived class
    blocks the inheritance of all functions of that name. This is
    a basic principle of C++ name lookup: once the compiler finds
    a name, it stops.

    > Also, and what about operator int() as cast operator? Can it be
    > inherited?


    > And what about other operators such as operator[], operator++, and
    > operator+=?


    Operators follow exactly the same rules as any other function.
    With no exceptions.

    --
    James Kanze
    James Kanze, Feb 11, 2011
    #6
    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. Alicia
    Replies:
    3
    Views:
    940
    jjr2004a
    Nov 24, 2004
  2. SpOiLeR
    Replies:
    5
    Views:
    379
    Samee Zahur
    May 8, 2005
  3. vj
    Replies:
    6
    Views:
    1,651
  4. looping
    Replies:
    2
    Views:
    284
    James Stroud
    Mar 5, 2007
  5. 7stud --
    Replies:
    11
    Views:
    394
    7stud --
    Nov 9, 2007
Loading...

Share This Page