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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:36:30+00:00 2026-06-12T17:36:30+00:00

I am learning about virtual functions and i am very confused with the results

  • 0

I am learning about virtual functions and i am very confused with the results of the below program
I was hoping that both a1.virFun(b1) and b1.virFun(b1) should return “hello from B”, but the program returns “hello from A”. This is contrary to my understanding. Can you please explain why b1.sayHello() is not being invoked, even when i am passing b1 as argument and b1.sayHello() is virtual function.

#include<iostream>

using namespace std;

class A
{
 public:
 virtual void sayHello();
 void virFun(A obj);

 };

class B : public A
{
 public:
 void virFun(A obj);
 virtual void sayHello();

};

void A::sayHello()
 {
   cout << "hello from A" << endl;
 }

 void B::sayHello()
 {
   cout <<"hello from B" << endl;
 }

 void A::virFun(A obj)
 {
    obj.sayHello();
 }

 void B::virFun(A obj)
{
    obj.sayHello();
}

int main()
{
 A a1;
 B b1;

a1.virFun(b1);
b1.virFun(b1);

return 0;
}
  • 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-12T17:36:32+00:00Added an answer on June 12, 2026 at 5:36 pm

    Make virFun take a reference, not an A object by value:

     void A::virFun(A& obj) { obj.sayHello(); }
    

    Reason: If you take an A by value, the parameter will be initialized by making a copy of the value passed. If you pass a B, it will copy the ‘A’ part of it into a new variable of the parameter type (A) – this is known as object slicing. The parameter will no longer ‘be-a’ B instance, so it will behave as an A instance, alright.

    No need to override virFun in class B, since it uses the argument obj in both cases

    In fact, virFun could be a static function. The name is confusing since it isn’t even a virFun (virtual function) – it is a regular function that uses a virtual function.

    #include<iostream>
    
    using namespace std;
    
    struct A     { virtual     void sayHello() { cout << "hello from A" << endl; } };
    struct B : A { /*virtual*/ void sayHello() { cout << "hello from B" << endl; } };
    
    static void virFun(A& obj)
    {
        obj.sayHello();
    }
    
    int main()
    {
        A a1;
        B b1;
    
        virFun(a1);
        virFun(b1);
    }
    

    See it live on http://liveworkspace.org/code/1624496ced29eb4683f5b19072f72f60

    hello from A
    hello from B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning about raw sockets. I heard that ping utility is using raw
I'm learning about the MVC architecture these days and I'm confused MVC with the
I'm learning about polymorphism, and I am confused by this situation: Let's say I
I'm learning the use of boost smart pointers but I'm a bit confused about
learning about loops (still a beginner) in VB.net. I have got the below code
I'm learning about the blocking I/O functions for writing linux device driver and I'm
In learning about Web Deploy I came across some netsh.exe commands that talk about
Just learning about mercurial's --style and --template options that can be used on hg
Learning about ARM NEON intrinsics, I was timing a function that I wrote to
I'm learning about fractal tree indices such as that found in TokuDB . I

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.