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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:34:50+00:00 2026-05-28T23:34:50+00:00

I am reading a C++ puzzle here: http://gotw.ca/gotw/005.htm I did not understand his explanation

  • 0

I am reading a C++ puzzle here: http://gotw.ca/gotw/005.htm

I did not understand his explanation on static vs dynamic overload resolution (or default paramaters), so I tried to distill the issue and write some tests myself:

class Base {
    public:
    virtual void foo() {cout << "base/no parameters" << endl;}
    virtual void foo(int a) {cout << "base/int" << endl;}
};

class Derived : public Base {
    public:
    void foo() {cout << "derived/no parameters" << endl;}
    void foo(double a) {cout << "derived/int" << endl;}
};

int main() {
    Base* ptr = new Derived;
    ptr->foo();
    ptr->foo(1.0);
}

The output is:

derived/no parameters
base/int

How come in the call to foo(), C++ seems to recognize that it is pointing to a Derived but in the call foo(1.0), C++ does not see void foo(double a) function in the Derived class?

In my mind there are competing ideas, that C++ has polymorphism, which explains the first call, but that overload resolution is done at compile-time, which explains the second call.

  • 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-28T23:34:51+00:00Added an answer on May 28, 2026 at 11:34 pm

    C++ does not see void foo(double a) function in the Derived class?

    C++ does see the function, but it’s not associated with Base::foo‘s virtualness due to function signature difference:

    virtual void Base::foo(int);  // 'Base' signature
    void Derived::foo(double);    // 'Derived' signature
    

    So here there are 2 important facts for Derived::foo(double):

    1. doesn’t relate (override) to Base::foo(int)
    2. not a virtual method (even making it virtual doesn’t help)

    1st point is more important, because when you call

    // 'ptr' refers 'Base' method signature; so 'double' implicitly converts to 'int'
    ptr->foo(1.0);
    

    Base pointer is used. In the vtable list it has only one entry of Base::foo(int). And thus it’s called.

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

Sidebar

Related Questions

In trying to resolve Facebook's Puzzle Hoppity Hop, http://www.facebook.com/careers/puzzles.php?puzzle_id=7 , I'm reading one integer
Reading manual about Sling http://sling.apache.org/site/46-line-blog.html added folder blog and blog.html to destination: \launchpad\content\src\main\resources\content\ but
Reading through Peter Norvig's Solving Every Sudoku Puzzle essay , I've encountered a few
Found the answer here (last post): http://social.msdn.microsoft.com/Forums/eu/wcf/thread/f5c0ea22-1d45-484e-b2c0-e3bc9de20915 I'm having one last issue with the
I've been reading up on assembly. From what I understand about programming languages, it
I'm reading Xcode's documentation, and here is something that puzzles me: __block typeof(self) tmpSelf
I was reading Eloquent JavaScript and I came across this example for the puzzle:
I puzzled with understanding ADO.NET, after reading several articles I do not have a
I've been reading on this quite some time...and i'm puzzled - Can you help
I am reading the O'Reilly CouchDB book. I am puzzled by the reduce/re-reduce/incremental-MapReduce part

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.