473,385 Members | 1,344 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,385 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 1557
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
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.