473,773 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
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 1579
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
1996
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; } // A mainline to exercise it: #include <numeric> int main() {
5
1644
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
1589
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 "MyBase" called "fill" and this method is not overridden in "Myderived" and I then create and object of my derived and call the fill method in the base class, the is it possible for the for fill method of mybase to know it was called from an...
0
4806
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 manually inserted a row in the table bound to the Combo box. The InsertAt Method of adding a row just does not work. Hope this helps anyone with this problem. john
2
1611
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 looks like this... Public MustInherit Class BaseClass Public Shared Sub DoIt() ' Need to know which class is "running"
3
2228
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 the subroutine. I know the base class name can be retrieved by using reflection namespace: System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name. Thanks Goran
4
1619
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
4853
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 signature. A class inherits publicly from this base class and redefines one of the two functions in the derived class. In that case, a derived class object cannot access the other base class function that it hasn't redefined. I'm posting a code snippet...
3
1367
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 is not defined
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.