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

  • Home
  • SEARCH
  • 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 6872889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:59:12+00:00 2026-05-27T03:59:12+00:00

My problem is this, I have a base class from which classes are derived.

  • 0

My problem is this, I have a base class from which classes are derived. I have an overloaded += operator in the base class but I only want to be able to add derived classes of the same type. I do this by returning the lhs and producing a warning message if they are not the same type.

class base{
   int variable;
   public:
   base(int v);
   base & operator+=(const base & rhs);

class a: public base{
}

class b: public base{
}

base & operator+=(const base & rhs){
if(type_same(const & rhs){
    variable+=rhs.variable; }
return *this
}

a first(1);
a second(5);
b third(2);

first+=second // first.variable now =6
second+=third // warning produced second.variable still = 5

I’ve never used dynamic cast before so my question is is it possible/advisable to a check using it and typeid to find out whether the types passed are the same. My alternative is a string constant name for each derived class.

  • 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-05-27T03:59:13+00:00Added an answer on May 27, 2026 at 3:59 am

    My take: you could employ CRTP

    The below code will issue the type mismatch error at compile time, as suggested by others.

    template <typename Derived>
    class base{
       int variable;
     public:
       base(int v) { variable = v; }
       Derived& operator+=(const Derived& rhs);
    };
    
    struct a: base<a> {
        a(int v): base<a>(v) {}
    };
    
    struct b: base<b> {
        b(int v): base<b>(v) {}
    };
    
    template <typename Derived> 
    Derived& base<Derived>::operator+=(const Derived& rhs) 
    {
        variable += rhs.variable; 
        return static_cast<Derived&>(*this);
    }
    
    int main(int argc, const char *argv[])
    {
        a a1(1), a2(2);
        b b1(1), b2(2);
    
        a1 += a2;
        b1 += a2; // compile time error :)
    
        return 0;
    }
    

    Even if you really wanted to type the operator arguments as base& you’d at least gain the benefit of using static_cast<> because the conversion is exact and well defined at runtime. I can expand the idea in a few

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

Sidebar

Related Questions

I have a base class Element and derived two other classes from it, Solid
I have a stateless, abstract base class from which various concrete classes inherit. Some
I have a base class and a dozen derived classes. All but one derived
I want to get your opinion on this. I have a class which is
I have two classes: a base class, Foo::Base and a derived class, Foo::Base::Sub .
I have an abstract virtual base class Foo from which I derive many other
I have an abstract base class called Curve . There are three classes that
I have an abstract base class which acts as an interface. I have two
Suppose I have an abstract base class, that just defines a container on which
I have the following generic classes: class Base<T> where T : ... { ...

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.