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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:54:09+00:00 2026-06-13T11:54:09+00:00

I am trying to understand the following bit of code: #include<iostream> using namespace std;

  • 0

I am trying to understand the following bit of code:

#include<iostream>
using namespace std;
class Base {
    public:
        virtual void f(float) { cout << "Base::f(float)\n"; }
};
class Derived : public Base {
    public:
        virtual void f(int) { cout << "Derived::f(int)\n"; }
};
int main() {
    Derived *d = new Derived();
    Base *b = d;
    d->f(3.14F);
    b->f(3.14F);
}

This prints

Derived::f(int)
Base::f(float)

And I am not sure why exactly.

The first call d->f(3.14F) calls the function f in Derived. I’m not 100% sure why. I had a look at this (http://en.cppreference.com/w/cpp/language/implicit_cast) which says:

A prvalue of floating-point type can be converted to prvalue of any integer type. The fractional part is truncated, that is, the fractional part is discarded. If the value can not fit into the destination type, the behavior is undefined

Which to me says you can’t do this since a float does not fit into an int. Why is this implicit conversion allowed?

Secondly, even if I just accept the above as being OK, the 2nd call to b->f(3.14F) doesnt make sense. b->f(3.14F) is calling a virtual function f, so this is dynamically resolved to call the f() associated with the dynamic type of the object pointed to by b, which is a Derived object. Since we are allowed to convert 3.14F into an int, because the first function call indicates that this is legal, this (to my understanding) should call the Derived::f(int) function again. Yet it calls the function in the Base class. So why is this?

edit: here’s how I figured it out and explained it to myself.

b is a pointer to a Base object, therefore we can only use b to access members of a Base object, even if b really points to some Derived object (this is standard OO/inheritance stuff).

The only exception to this rule is when a member function of Base is declared as virtual. In such a case, a Derived object may override this function, providing another implementation by using the exact same signature. If this occurs, then this Derived implementation will be called at run time even if we happen to be accessing the member function through the pointer to a Base object.

Now, in the snippet of code above, we do not have any overriding taking place because the signatures of B::f and D::f are different (one is a float, the other an int). So when we call b->f(3.14F), the only function that is considered is the original B::f, which is what is called.

  • 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-13T11:54:09+00:00Added an answer on June 13, 2026 at 11:54 am

    The two function have different signatures, so f in derived does not override the virtual function in base. Just because the types int and float can be implicitly cast does not have an effect here.

    virtual void f(float) { cout << "Base::f(float)\n"; }
    virtual void f(int) { cout << "Derived::f(int)\n"; }
    

    A clue to what is happening can been seen with the new override keyword in C++11, this is very effective at reducing these sort of bugs.

    virtual void f(int) override { cout << "Derived::f(int)\n"; }
    

    from which gcc produces the error:

    virtual void Derived::f(int)’ marked override, but does not override

    clang

    error: ‘f’ marked ‘override’ but does not override any member functions

    http://en.cppreference.com/w/cpp/language/override

    EDIT:

    for your second point, you can actually expose the float overload from base in derived which exposes an implicitly compatible member function. like so:

    class Derived : public Base {
    public:
        using Base::f;
        virtual void f(int) { cout << "Derived::f(int)\n"; }
    };
    

    Now passing a float to member function f binds closer to the function defined in the base and produces:

    Base::f(float)
    Base::f(float)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just trying to understand delegates using the following code. public class delegatesEx
I am trying to understand the following code: #include<stdio.h> #include<stdlib.h> #include<sys/io.h> #define baseport 0x378
I'm trying to understand the following code block and am a bit stumped.. specifically
i am trying to understand following fragment of javascript code <!DOCTYPE html> <html> <body>
I have the following class written by someone else that I'm trying to understand
I was trying to understand the floating point representation in C using this code
I am trying to compile the following code on Ubuntu (64-bit), with Code::Blocks 10.05
I've been trying to read and understand a bit more about better code and
I found the following code here: http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java I am trying to understand why there
I'm trying to understand Code Contracts in a bit more detail. I've got the

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.