?
Solved

polymorphism ?

Posted on 2003-10-27
3
Medium Priority
?
289 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 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…
Suggested Courses

752 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