Solved

polymorphism ?

Posted on 2003-10-27
3
285 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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 149
PDF library for Delphi 2 121
is twain_32.dll cmpatible with windows 10 ? 10 163
How to convert MFC::CString to UTF8 wchar_t* 10 210
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 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