473,225 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

name lookup in derived class

Hi, can anyone suggest how the compiler will decide which bar() to
invoke when we call d.dio()? I am getting the following output:

B foo
B bar

but is there a situation when the same code can print

B foo
D bar

?
#include<iostream>
using namespace std;

class Base
{
public:
void foo()
{
cout<<"B foo"<<endl;
bar();
}

void bar()
{
cout<<"B bar"<<endl;
}
};
class Derived : public Base {
public:
void dio()
{
foo();
}
void bar()
{
cout<<"D bar"<<endl;
}
};
int main()
{
Derived d;
d.dio();
return 0;
}

Mar 1 '06 #1
3 1549
Jordan Taylor wrote:
Hi, can anyone suggest how the compiler will decide which bar() to
invoke when we call d.dio()? I am getting the following output:

B foo
B bar

but is there a situation when the same code can print

B foo
D bar

?


Is this homework?

The only situation is if you add 'virtual' to Base::bar().

In which case, this question becomes a textbook example of polymorphism.
Objects of type Base always call Base::bar(), Derived objects call
Derived::bar(), and objects that think they are Base but are really Derived
call Derived::bar().

This trick is most useful to decouple code, and limit the places you must
edit to upgrade code. Users of base classes needn't couple to derived class
behaviors.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 1 '06 #2
Phlip wrote:
Jordan Taylor wrote:
Hi, can anyone suggest how the compiler will decide which bar() to
invoke when we call d.dio()? I am getting the following output:

B foo
B bar

but is there a situation when the same code can print

B foo
D bar

?
Is this homework?


I don't understand why every simple question gets tagged as homework.
Isn't there something called a "Beginner" anymore? Besides all colleges
are moving to Java so you shouldn't be so paranoid about homework
problems appearing. Just MHO

The only situation is if you add 'virtual' to Base::bar().

In which case, this question becomes a textbook example of polymorphism.
Objects of type Base always call Base::bar(), Derived objects call
Derived::bar(), and objects that think they are Base but are really Derived
call Derived::bar().
If d calls foo() does it morph into an object of type Base? My
understanding is that the scope of derived classes are nested. If
Derived inherits from Base, then Derived::bar() is also in the scope of
Base. Why doesn't the compiler let us use Derived::bar() then?

This trick is most useful to decouple code, and limit the places you must
edit to upgrade code. Users of base classes needn't couple to derived class
behaviors.
I'll look this up.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


thanks!

Mar 1 '06 #3

Jordan Taylor wrote:
If d calls foo() does it morph into an object of type Base? My
understanding is that the scope of derived classes are nested. If
Derived inherits from Base, then Derived::bar() is also in the scope of
Base. Why doesn't the compiler let us use Derived::bar() then?


The object doesnot get morphed, but the call is resolved at compile
time itself, since the function is not virtual. In other words, since
base::bar() is not virtual, any call to base::bar() is "early-bound",
or resolved at compile time. Had base::bar() been virtual, the binding
would have been delayed till run time, in which case the derived::bar()
would have been called if the function was invoked on an instance of
derived class.

Mar 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Srini | last post by:
I was reading the "Exceptional C++" of Herb Sutter. In an example, he mentions the following. // In some library header: namespace N { class C{}; } int operator+(int i, N::C) { return i+1; }...
5
by: Hartmut Sbosny | last post by:
Hello NG, I have a ordinary class `Base' with two functions `void foo()' and `void any()': class Base { public: Base() {} void foo() {} void any() {}
3
by: martin | last post by:
Hi, If I have a base class, say called "MyBase" and a derived class called "Myderived" is it possible to get the name of the derived class from the base class fo example if I have a method in...
0
by: JSantora | last post by:
Essentially, InsertAT is broken! For the past couple of hours, I've been getting this "Parameter name: '-2147483550' is not a valid value for 'index'." error. Apparently, its caused by having...
2
by: BBM | last post by:
I may be putting too fine a point on this, but I'd like to be able to know the class name of a shared method from within the method. BTW, the shared method is in a "MustInherit" class. So it...
3
by: Goran Djuranovic | last post by:
Hi all, Is there a way to retrieve a derived class name inside a subroutine or a function of the base class? I am trying to get some data from the database, based on which derived class is calling...
4
by: StephQ | last post by:
I need to know if it is possible to solve the following problem (I am really stuck at it). Consider the CRTP pattern: struct Derived { ....// May contain void operate( double x ) , may not....
2
by: developer.new | last post by:
Hi I have a question regarding this concept I learned about recently: Name Hiding. Here's what I've come across: There is a base class with two functions with the same name but different...
3
by: WaterWalk | last post by:
Hello. Consider the following two examples: class Test1(object): att1 = 1 def func(self): print Test1.att1 // ok class Test2(object): att1 = 1 att2 = Test2.att1 // NameError: Name Test2...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.