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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:59:43+00:00 2026-05-28T00:59:43+00:00

What shall I do in this example to trigger the right operator: #include<iostream> using

  • 0

What shall I do in this example to trigger the right operator:

#include<iostream>
using namespace std;

class A
{
public:
    virtual void operator/(A& a){cout << "class A";}
};
class B : public A
{
public:
    void operator/(B& b){cout << "class B";}
};

int main()
{
    A* a = new B;
    A* b = new B;
    *a / *b;
    return 0;
}

The output is "class A" but it should be "class B".
How can I solve this? Thanks.

EDIT: According to answers… should I do this for every derived class:

class A
{
public:
    virtual void operator/(A& a){cout << "class A";}
};
class B : public A
{
public:
    virtual void operator/(A& a) override {cout << "class B";}
};
class C : public B
{
public:
    virtual void operator/(A& a) override {cout << "class C";}
};
class D: public C
{
public:
    void operator/(A& a) override {cout << "class D";}
};
int main()
{
    A* a = new D;
    A* b = new D;
    *a / *b;
    //...
}
  • 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-28T00:59:44+00:00Added an answer on May 28, 2026 at 12:59 am

    (First, I’m going to try to explain what’s happening at the moment, but at the end I’ll try to write a ‘correct’ program.)

    I’m going to use x and y instead of a and b – it’s bit less confusing.

    A* x = new C;
    A* y = new B;
    *x / *y;
    

    The static type of x is A – i.e. that’s what the type of the variable is. The dynamic type, the real type of the object pointed at by the variable, is C.

    The static type of y is A – i.e. that’s what the type of the variable is. The dynamic type, the real type of the object pointed at by the variable, is B.

    So there are four types that seem to be involved here. But actually, the dynamic type of y is never relevant, as we will see. So that leaves (at most) three relevant types. To understand this, let’s rewrite the expression as follows:

    x -> operator/ (*y);
    

    First, the compiler looks at the static type of x. In this case it is A. Then it looks for a method in the static type with the appropriate signature. What does ‘appropriate signature’ mean? The answer is that method lookup ignores the dynamic type of variables. The static type of y is A, and hence operator/(A&) is selected. (We know y is really a B, but that’s ignored. )Finally, the runtime will look at the dynamic type of x (ignoring y). The dynamic type is C. Because the operator/(A&) was virtual in A, then the runtime will actually call the C::operator/(A&) instead.

    In short, the dynamic type of y is not relevant. The methods operator/(B&) or operator/(C&) will never be called. Those methods should be deleted.

    There’s a fundamental asymmetry here. In *y / *x, the dynamic type of y would be relevant, but not the dynamic type of x.

    How to fix it

    Before ‘fixing’ the problem, we need to be sure we know what the question is. Hence, I’ll clarify my interpretation first. If this is the wrong interpretation, apologies in advance.

    I’m going to assume that the questioner intends that the dynamic type of x will always be the same as the dynamic type of y, and that if they are different then there should be an error message. For example, this code is expected to work:

    A * x = new A;
    A * y = new A;
    *x / *y; // divide an instance of A by another instance of A
    

    and

    A * x = new B;
    A * y = new B;
    *x / *y; // divide an instance of B by another instance of B
    

    and

    A * x = new C;
    A * y = new C;
    *x / *y; // divide an instance of C by another instance of C
    

    but not

    A * x = new B;
    A * y = new C;
    *x / *y; // should give an error
    

    or

    A * x = new C;
    A * y = new B;
    *x / *y; // should give an error
    

    Here is a complete program that does this:

    #include <iostream>
    #include <typeinfo>
    #include <cassert>
    using namespace std;
    class A
    {
    public:
        virtual void operator/(A& z) {
                assert(typeid(z) == typeid(*this));
                cout << "class A" << endl;
        }
    };
    class B : public A
    {
    public:
        virtual void operator/(A& z) {
                assert(typeid(z) == typeid(*this));
                cout << "class B" << endl;
                B& b = dynamic_cast<B&>(z);
        }
    };
    class C : public B
    {
    public:
        virtual void operator/(A& z) {
                assert(typeid(z) == typeid(*this));
                cout << "class C" << endl;
                C& c = dynamic_cast<C&>(z);
        }
    };
    int main()
    {
        A* x = new C;
        A* y = new C;
        *x / *y;
    }
    

    The operators in B and C do two things:

    • test that the type of the second operand is as expected. i.e. confirm that both side of the / have the same type.
    • cast the A& to the appropriate type, allowing the division code to have full access to both operands
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: A validation method contains this check to see if an NSError object shall
I want to develop a application. Say for example on Phone7/iPhone/android, this application shall
I need some help adding conditions to a linq query. Take this small example
This works (prints, for example, 3 arguments): to run argv do shell script echo
Assume a Cassandra cluster with hosts A and B . This cluster shall now
This is a idea for a security. Our employees shall have access to some
I have this small class called City that simply holds some information about a
I was reading through K&R and i came across this example about uncertainty in
I want to invoke this javascript. //<BUTTON id=TestID class=My_Test button small type=submit><SPAN class=account>By</SPAN></BUTTON> How
In an example like this : $c = true; // Let's not forget to

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.