Solved

polymorphism ?

Posted on 2003-10-27
3
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Assisted Solution

by:burcarpat
burcarpat earned 20 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 30 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

730 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