Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:55:36+00:00 2026-06-12T10:55:36+00:00

Suppose I have a base class and two classes derived from it: class Base

  • 0

Suppose I have a base class and two classes derived from it:

class Base
{
protected:
    double value;
public:
    virtual ~Base();

    Base(double value) : value(value) {}
    Base(const Base& B) { value=B.value; }

    Base operator+ (const Base& B) const { 
        return Base(value+B.value); 
    }

};

class final Derived1 : public Base {
public:
    Derived1(double value) : Base(value) {}
};

class final Derived2 : public Base {
public:
    Derived2(double value) : Base(value) {}
};

I want to accomplish the following:

int main(int argc, char *argv[])
{
    Derived1 a = Derived1(4.0);
    Derived2 b = Derived2(3.0);

    a+a; // this should return a Derived1 object
    b+b; // this should return a Derived2 object

    a+b; // this should FAIL AT COMPILE TIME

    return 0;
}

In other words, I want to guarantee that the inherited operator+ only operates on objects of the same type as the calling instance.

How do I do this cleanly? I found myself re-defining the operator for each class:

class final Derived1 : public Base {
    ...
    Derived1 operator+ (const Derived1& D1) const {
        return Derived1(value+D1.value);
    }
    ...
};

class final Derived2 : public Base {
    ...            
    Derived2 operator+ (const Derived2& D1) const {
        return Derived2(value+D1.value);
    }
    ...
};

But that’s just a pain. Moreover, it doesn’t seem like proper code re-use to me.

What is the proper technique to use here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:55:37+00:00Added an answer on June 12, 2026 at 10:55 am

    If you can make sure Derived1 and Derived2 are leaf classes (i.e. no other class can derive from them) you can do this with the curiously recurring template pattern:

    template <typename T>
    class BaseWithAddition : public Base {
        T operator+(T const& rhs) const {
            return T(value + rhs.value);
        }
    };
    
    class final Derived1 : public BaseWithAddition<Derived1> {
        // blah blah
    };
    
    class final Derived2 : public BaseWithAddition<Derived2> {
        // blah blah
    };
    

    (final is a C++11 feature that prevents further derivation.)

    If you allow derivation from Derived1 and Derived2 then you get trouble:

    class Derived3 : public Derived1 {};
    Derived3 d3;
    Derived1 d1;
    Derived1& d3_disguised = d3;
    d1 + d3_disguised; // oooops, this is allowed
    

    There’s no way to prevent this at compile-time. And even if you want to allow it, it’s not easy to get decent semantics for this operation without multiple dispatch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have two classes: class Base { private: int x; public: void f();
I have a base class Element and derived two other classes from it, Solid
The scenario Suppose I have the following two model classes: public class ProductColor {
Suppose I have two classes in a Rails application: class Subject < ActiveRecord::Base def
Suppose I have two classes a base class and an inherited class as follows:
Suppose we have the two classes given below: public class baseclass implements interface {}
Suppose I have two versions of operator-> (overloaded on const) in a base class.
Suppose I have these two classes class base_size { public: int size() { return
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose we have declared these two classes: public class Animal { //..... } public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.