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 7816075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:44:13+00:00 2026-06-02T05:44:13+00:00

#include <iostream> class aa { public: aa(){} aa(aa& obj) { aa1 = obj.aa1; }

  • 0
#include <iostream>

class aa
{
public:
    aa(){}

    aa(aa& obj)
    {
        aa1 = obj.aa1;
    }

    virtual aa operator =(aa& obj)
    {
        aa1 = obj.aa1;
        return (*this);
    }

    virtual aa operator +(aa& obj)
    {
        aa1 += obj.aa1;
        return (*this);
    }

    int aa1;
};

class bb: public aa
{
public:
    bb():aa(){}

    bb(bb& obj)
    {
        bb1 = obj.bb1;
    }

    aa operator =(aa& obj)
    {
        aa::operator =(obj);
        bb b1 = dynamic_cast<bb&>(obj);
        bb1 = b1.bb1;       
        return (*this);
    }

    aa operator +(aa& obj)
    {
        aa::operator +(obj);
        bb b1 = dynamic_cast<bb&>(obj);
        bb1 += b1.bb1;
        return (*this);
    }

    int bb1;
};


int main()
{
    bb b1;
    bb b2;

    b1.bb1 = 1;
    b1.aa1 = 1;

    b2.bb1 = 2;
    b2.aa1 = 2;

    aa &a1 = b1;
    aa &a2 = b2;

    a1 = a2;
    b1 = dynamic_cast<bb&>(a1);
    b2 = dynamic_cast<bb&>(a2);

    std::cout<<b1.aa1<<";"<<b1.bb1;

    bb b3;
    b3.bb1 = 3;
    b3.aa1 = 3;

    aa &a3 = b3;

    aa &a4 = a2 + a3;
    b3 = dynamic_cast<bb&>(a4);

    return 0;
}

Output:
2;2 and then it crashes at line b3 = dynamic_cast<bb&>(a4); giving the error std::bad_cast at memory location 0x0012fdbc..

The reason I have found is the result of expression of a2+a3 is coming as object of type aa, and in the next statement we are trying to cast it to derived type object which is not valid, hence leading to exception. So my query is can we achieve the above intention i.e. aa &a4 = a2 + a3; with some changes in the above functions?

  • 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-02T05:44:14+00:00Added an answer on June 2, 2026 at 5:44 am

    C++ has a single dispatch (based on virtual function).
    Polymorphic binary operators fall inevitably into the “dual dispatch” case, and hence cannot be implemented polymorphicaly by means of just simple virtual function.

    Also, since you are returning a value, every sub-classes information is lost.

    A more proper way to handle this kind of situation is to define a non-polymorphic handle that implements the operations and holds polymorphic types, delegating to them the operation execution.

    Like

    class handle
    {
    public:
        class aa;
        class bb;
    
        class root
        {
        public:
            virtual ~root() {}         //< required being this polymorphic
            virtual root* clone()=0;
    
            virtual handle add_invoke(const root& r) const=0; //resolve the 2nd argument
            virtual handle add(const aa& a) const=0;    //resolve the 1st argument
            virtual handle add(const bb& a) const=0;    //resolve the 1st argument
        };
    
        class aa: public root
        {
        public:
            aa(...) { /*set vith a value */ }
            aa(const aa& a) { /* copy */ }
            virtual root* clone() { return new aa(*this); }
    
            virtual handle add_invoke(const root& r) const 
            { return r.add(*this); }  //will call add(const aa&);
    
            virtual handle add(const aa& a) const
            { return handle(new aa(.../*new value for aa with (aa,aa)*/)); }
            virtual handle add(const bb& a) const
            { return handle(new bb(.../*new value for bb with(aa,bb)*/)); }
        };
    
        class bb: public root
        {
        public:
            bb(...) { /*set vith a value */ }
            bb(const bb& b) { /* copy */ }
            virtual root* clone() { return new bb(*this); }
    
            virtual handle add_invoke(const root& r) const
            { return r.add(*this); }  //will call add(const bb&);
    
            virtual handle add(const aa& a) const
            { return handle(new bb(.../*new value for aa with (bb,aa)*/)); }
            virtual handle add(const bb& a) const
            { return handle(new bb(.../*new value for bb with (bb,bb)*/)); }
        };
    
        handle() :ps() {}
        //support both copy (by clone) and move (by stole)
        handle(const handle& s) :ps(s.ps? s.ps->clone(): nullptr) {}
        handle(handle&& s) :ps(s.ps) { s.ps=nullptr; };
        //assign by value, to force temporary assign
        handle& operator=(handle h) { delete ps; ps=h.ps; h.ps=0; return *this; }
        //cleanup
        ~handle() { delete ps; }
    
        //the operator+
        friend handle operator+(const handle& a, const handle& b)
        { 
            return (b.ps && a.ps)? b.ps->add_invoke(*a.ps): handle(); 
            //Note: manage also the a+b with one of `a` or `b` as null, if it make sense
        }
    
    private:
        handle(root* p) :ps(p) {}
    
        root* ps;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: #include <iostream> class foo { public: foo(int yy){y = yy;}
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Suppose you have the following situation #include <iostream> class Animal { public: virtual void
Look at this code please: #include <iostream> using namespace std; class Ratio { public:
Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
#include <iostream> #include <fstream> using namespace std; class binaryOperators { public: int i; binaryOperators
#include <iostream> #include <vector> #include <cassert> class a_class { public: int num_IN; a_class():num_IN(0){} a_class(a_class
#include<iostream> #include<vector> #include<algorithm> class Integer { public: int m; Integer(int a):m(a){}; }; class CompareParts

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.