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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:27:36+00:00 2026-05-13T13:27:36+00:00

i have a problem with function overloading. I will show you with some simple

  • 0

i have a problem with function overloading. I will show you with some simple example:

class A {};
class B : public A{};

void somefunction(A&, A&);
void somefunction(B&, B&);

void someotherfunction() {
...
A& a1 = ...
A& a2 = ...

...
}

Both a1 and a2 are instances of B but

somefunction(a1,a2);

calls

void somefunction(A&, A&);

What did i do wrong? I mean polymorphism and overloading are for stuff like that, arent they?

edit: Ok now i know it does not work (thanks for your answers).

Any solution how to do this? Without casting.

edit2: Ok left it as it is, with type casting, since something i would like to have is not possible. Thanks all for your help.

  • 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-13T13:27:36+00:00Added an answer on May 13, 2026 at 1:27 pm

    Cast them statically so that the compiler knows which one to pick:

    void somefunction((B&)a1, (B&)a2);
    

    The reason why you are having this problem is with the program design, not the language. Compiler picks which which function is used based on the types that are passed in. C# will behave in exactly the same way (pretty sure Java will too).

    It seems to me that you are implementing polymorphism in the wrong place. somefunction really belongs inside class a and should be virtual. Then whenever it’s called on the instance of a at runtime the override in the right class will be called.

    So, really it should be something like this:

    class a {
    public:
      virtual somefunction(a& a2) {
        //do stuff
      }
    }
    
    class b : public a {
      virtual somefunction(a& a2) {
        b& b2 = (b&)a2;
        //do stuff
      }
    }
    
    
    class c : public b {
      virtual somefunction(a& a2) {
        c& c2 = (c&)a2;
        //do stuff
      }
    }
    

    The above solution uses minimal casting inside the virtual function and assumes that the two instance of the same type. This means that b.somefunction(a()) will have undefined behaviour.

    A better solution is to rely on C++ RTTI and use dynamic_cast, which will return NULL if the downcast is not possible.

    This problem is known as double dispatch problem and is described in the wikipedia article pretty much as you described it. Furthermore, the only solution that wikipedia gives for multiple dispatch is to use dynamic_cast.

    EDIT OK, this has been bugging me, here is the solution for full double dispatch between a base class and two subclasses. It aint pretty and uses a bit of C++ trickery like friend classes (for better encapsulation actually, rather than the reverse) and forward declarations.

    class b;
    class c;
    class a {
    protected:
        virtual void somefunction(a& a2); //do stuff here 
        virtual void somefunction(b& b2); //delegate to b
        virtual void somefunction(c& c2); //delegate to c
    public:
        virtual void doFunc(a& a2) {
            a2.somefunction(*this);
        }
        friend class b;
        friend class c;
    };
    
    class b : public a {
    protected:
        virtual void somefunction(a& a2); //do stuff here 
        virtual void somefunction(b& b2); //do stuff here
        virtual void somefunction(c& c2); //delegate to c
    public:
        virtual void doFunc(a& a2) {
            a2.somefunction(*this);
        }
        friend class a;
    };
    
    
    class c : public b {
    protected:
        virtual void somefunction(a& a2); //do stuff here 
        virtual void somefunction(b& b2); //do stuff here
        virtual void somefunction(c& c2); //delegate to c
    public:
        virtual void doFunc(a& a2) {
            a2.somefunction(*this);
        }
        friend class a;
        friend class b;
    
    };
    //class a
    void a::somefunction(a& a2)  {
        printf("Doing a<->a");
    }
    void a::somefunction(b& b2)  {
        b2.somefunction(*this);
    }
    void a::somefunction(c& c2)  {
        c2.somefunction(*this);
    }
    //class b
    void b::somefunction(a& a2)  {
        printf("Doing b<->a");
    }
    void b::somefunction(b& b2)  {
        printf("Doing b<->b");
    }
    void b::somefunction(c& c2)  {
        c2.somefunction(*this);
    }
    //class c
    void c::somefunction(a& a2)  {
        printf("Doing c<->a");
    }
    void c::somefunction(b& b2)  {
        printf("Doing c<->b");
    }
    void c::somefunction(c& c2)  {
        printf("Doing c<->c");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 421k
  • Answers 421k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer arg(1) won't be a username or UID and not work… May 15, 2026 at 10:58 am
  • Editorial Team
    Editorial Team added an answer We always run our backups overnight, so the last backup… May 15, 2026 at 10:58 am
  • Editorial Team
    Editorial Team added an answer Yes, it can...but, you need to set it in the… May 15, 2026 at 10:58 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.