[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

polymorphism ?

Posted on 2003-10-27
3
Medium Priority
?
293 Views
Last Modified: 2010-04-02
Hi C++ Experts,

   I have a code that I don't quiet understand :
 ---------------------------------------------
class Base{
 public :
   virtual int f(int i){ cout << " Base  f()" << endl;}
} ;

class Derived: public Base{
 public:
  Derived(){ f(0);}
  int f(char *s){ cout << "Derived f()" << endl ;}
};
int main()
{
  Derived d ;
  return 0 ;
}
------------------------------
In the main code is executed, it prints : "Derived f()". However, I feel
kind of wierd ... because when f(0) is called in the constructor,
the object d actually hasn't been constructed yet... then how can it call the f(0) in the derived class ? not in the base class ?

In any case, is this a polymorphism ? if not, what's the reason that it calls the f() in the derived class ?

Thanks a lot !!!


--------------------------
0
Comment
Question by:meow00
3 Comments
 
LVL 7

Assisted Solution

by:burcarpat
burcarpat earned 80 total points
ID: 9628545
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9628548
The compiler will look for the closest methods possible before going up to the base class.
Derived() { Base::f(0); } would call the other f(int) in base. (Note that 0 is like a NULL address for char*)
What you're doing is just normal function overloading. virtual int f(int) in base is not related to int f(char*) in Derived.
0
 
LVL 6

Accepted Solution

by:
GaryFx earned 120 total points
ID: 9628742
> the object d actually hasn't been constructed yet

Actually, it has - at least enough for its own methods to be called.  By the time you get to the body of the constructor, all of the base class constructors have been called, and all of the constructors for the member variables have been called.  The presumption is that you know what you're doing inside your constructor, and that you'll only call methods that you know are safe to call at this point.  The body of the constructor is allowed to call any method of the object.

This is important for a common pattern, in which there's a separate initialize method that is called both by the constructor and by other methods that need to reinitialize the object.  Since the constructor cannot be called explicitly by other methods, the only way to achieve this is partition some of the initialization code into a separate method and have both the constructor and higher level methods call the initialization code.

Gary
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question