17

Must I need to redefine all the overloading operators with derived type if I require to use them in derived class?

The following code compiles fine:

class Point {

public:

    Point(int X = 0, int Y = 0):x(X), y(Y) {}

    virtual ~Point() {}

    Point operator +(Point &rhs) {
        return Point(x + rhs.x, y + rhs.y);
    }

protected:
    int x, y;
};

class Vector : public Point {

public:

    Vector(int X, int Y) : Point(X, Y) {}

    ~Vector() {}

    Vector operator +(Vector &rhs) {
        return Vector(x + rhs.x, y + rhs.y);
    }
};

int main()
{
    Vector v1(1, 2);
    Vector v2(3, 2);

    Vector v3 = v2 + v1;
}

But from what I've read,

C++ Primer 4th Ed. Section 15.5.3.

If a derived class wants to make all the overloaded versions available through its type, then it must either redefine all of them or none of them.

Does the part of the quote "none of them" make any sense here?

3 Answers 3

10

What it means is that if Point had more than one operator+(), and you only redefined one of them, then only that one would be accessible in the derived class; the other overloads would be hidden. If you declare no operator+() in the derived class, then all of the parent ones are available; if you declare any in the derived class, then none of the parent ones are available.

Make sense? This case is fine: the parent declares one, and you redefine that one. No problems. If the parent declared two, though, then your child class, which only declares one, would only have access to that one.

6
  • 3
    @Ernest Friedman-Hill: 1. Override is applicable only for virtual functions, This case is overloading 2. The members are inherited but hidden because of the derived class overloading.
    – Alok Save
    Apr 15, 2011 at 15:54
  • Yes, this is all correct. My language is quite imprecise and rather Java-colored; I'll touch it up. Apr 15, 2011 at 16:06
  • @Ernest Friedman-Hill: Apologies, I had already posted an answer before you reformatted or would have refrained.
    – Alok Save
    Apr 15, 2011 at 16:24
  • How could you have more than one member function operator+() for same class?
    – user963241
    Apr 15, 2011 at 17:24
  • 1
    @Dave18: You could have overloads for many other types than just const Point&. It probably makes no sense in this particular case, but some classes might find it useful to overload operator+ for string, int, double, etc...
    – Vitus
    Apr 15, 2011 at 17:52
9

Overloading operators in derived class from IBM.

A member function named f in a class A will hide all other members named f in the base classes of A, regardless of return types or arguments. The following example demonstrates this:

struct A {
  void f() { }
};

struct B : A {
  void f(int) { }
};

int main() {
  B obj_B;
  obj_B.f(3);
//  obj_B.f();
}

The compiler would not allow the function call obj_B.f() because the declaration of void B::f(int) has hidden A::f().

To overload, rather than hide, a function of a base class A in a derived class B, you introduce the name of the function into the scope of B with a using declaration. The following example is the same as the previous example except for the using declaration using A::f:

struct A {
  void f() { }
};

struct B : A {
  using A::f;
  void f(int) { }
};

int main() {
  B obj_B;
  obj_B.f(3);
  obj_B.f();
}

So if you do not overload all of them, then only the overloaded functions will be used.

6
  • 2
    Hey, do you think it's OK to quote this much of somebody else's content? Seems like a copyright issue to me. Apr 15, 2011 at 15:41
  • @Ernest: I didn't create the C++ language so I will have to reference something. See this similar meta question: meta.stackexchange.com/questions/46448/…
    – user195488
    Apr 15, 2011 at 15:59
  • +1 for the correct solution. You could have made this post much shorter, since you linked to the original content. Apr 15, 2011 at 17:19
  • 3
    @Andre: I am guarding against link rot.
    – user195488
    Apr 15, 2011 at 17:26
  • please make this quote shorter Apr 16, 2011 at 22:39
2

In C++, there is no overloading across scopes derived class scopes are not an exception to this general rule.

There is no overload resolution between Derived and Base class. An example:

class B
{
    public:
    int func1(int i)
    {
        cout<<"B::func1()";
        return i+1;
    }
};



class D : public B
{
    public:
    double func1(double d)
    {
        cout<<"D::func1()";
        return d+1.3;
    }
};

int main ()
{
    D *pd = new D;

    cout << pd->func1(2)  <<endl;
    cout << pd->func1(2.3)<<endl;

    return 0;
}

The output is:

D::func1()3.3
D::func1()3.6

This same rule applies for operator member functions as well, after all they are member functions too!

So in your code example if Point had more than one operator+(), and you redefined the same operator in Derived class then only that derived class operator will be accessible to objects of derived class because that version of the function hides the other Base class versions of operator+().
If you do not redefine the operator+() in the derived class, then none of the parent class versions of the operator+() are hidden and hence accessible through objects of Derived class.

Hence the statement:
If a derived class wants to make all the overloaded versions available through its type, then it must either redefine all of them or none of them.

Also, please note that overloading, overriding and function hiding are three terms that are loosely mis-used interchangeably sometimes but they all have separate meanings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.