Board index » cppbuilder » Deriving Classes From Base Class with virtual Methods

Deriving Classes From Base Class with virtual Methods


2003-12-02 12:46:03 PM
cppbuilder10
It is my understanding that if a base class has any virtual methods in them,
my derived class must redclare all of them and atleast call the base classes
method.
For Instance
class Base
{
public:
virtual void f1();
private:
virtual void f3();
protected:
virtual void f2(){f3)};
};
class Derived : public Base
{
public:
virtual void f1(){Base::f1();};
protected:
virtual void f2(){Base::f2();};
private:
};
I can't do anything with f3 that I know of. What happens if I do something
like
Derived* d = new Derived();
d->f2();
will Base::f3 get called when Derived::f2 is called or will it find not find
the function for Derived::f3 in the virtual function table?
Another Question...when I am deriving a class from a Delphi Class and it has
some function that looks like this
DYNAMIC void __fastcall Progress(System::TObject* Sender,
Graphics::TProgressStage Stage, Byte PercentDone
, bool RedrawNow, const Windows::TRect &R, const AnsiString Msg);
In my derived class do I just do
DYNAMIC void __fastcall Progress(System::TObject* Sender,
Graphics::TProgressStage Stage, Byte PercentDone
, bool RedrawNow, const Windows::TRect &R, const AnsiString Msg)
{
Base::Progress(Sender, Stage, PercentDone, RedrawNow, R, MSG);
};
I looked up DYNAMIC and to me it seems its the same thing as virtual methods
except for the way that it looks up the functions at runtime. Is this
correct? Is One way faster than the other?
 
 

Re:Deriving Classes From Base Class with virtual Methods

"JunkMail" < XXXX@XXXXX.COM >wrote:
Quote
It is my understanding that if a base class has any virtual
methods in them, my derived class must redclare all of them
and atleast call the base classes method.
No. The keyword virtual says that if you inherit from the class
and if you redeclare that method then the redeclared method will
be called - even if you access the instance of the derived class
by a pointer type to the base class. If you don't redeclare a
virtual method then there exists only the one in the base class,
that's all.
Quote

For Instance

class Base
{
public:
virtual void f1();
private:
virtual void f3();
protected:
virtual void f2(){f3)};
};

class Derived : public Base
{
public:
virtual void f1(){Base::f1();};
protected:
virtual void f2(){Base::f2();};
private:
};

I can't do anything with f3 that I know of. What happens if I
do something like

Derived* d = new Derived();
d->f2();
will Base::f3 get called when Derived::f2 is called or will it
find not find the function for Derived::f3 in the virtual
function table?
Derived::f2 gets called. This calls Base::f2 and this calls
Base::f3. So far this doesn't even have anything to do with
virtual or not. The base class methods being virtual means that
even this works the same way:
Quote
Base* d = new Derived();
d->f2();
As f2 is virtual in the base class, d->f2 points actually to the
derived method Derived::f2(). This is what virtual does.
Quote
Another Question...when I am deriving a class from a Delphi
Class and it has some function that looks like this

DYNAMIC void __fastcall Progress(System::TObject* Sender,[snip]
I looked up DYNAMIC and to me it seems its the same thing as
virtual methods except for the way that it looks up the
functions at runtime. Is this correct? Is One way faster than
the other?
I only know that DYNAMIC works for me exactly like virtual. The
Delphi approach only looks a little better for me, because you
are forced to use the keyword DYNAMIC in your derived class when
there is such a DYNAMIC method in the base class. Example:
class Base
{
public:
virtual void f1();
};
class Derived : public Base
{
public:
void f1();
void f2();
};
Here f1() is virtual, thus Derived::f1() will be called in a
derived instance. Problem is: If you add virtual void f2(); to
your base class, then suddenly Derived::f2() becomes virtual,
too. If it's the same method name only by coincidence then this
is not what you wanted.
class Base
{
public:
DYNAMIC void f1();
};
class Derived : public Base
{
public:
DYNAMIC void f1();
void f2();
};
With Delphi keywords you must use DYNAMIC for Derived::f1(). If
Someone adds DYNAMIC void f2(); to Base then the compiler will
warn you that there is a DYNAMIC base method of that name. Then
you will have to tell the compiler: yes, this is to be regarded
as the same method with DYNAMIC, or: no, these are different
methods that have coincidentally the same name, by using the
keyword HIDESBASE instead.
You see, the Delphi keywords help you better here. Especially
when one person writes Base class and another Derived class.
Both C++ and Delphi are not perfect concerning these things
however and I know of no language that is (but I must admit I
don't know how java and c# handle this). If the author of Base
renames the method f1() to fx1() for example (or if he simply
changes the parameters) then you won't be informed, because
there is no keyword telling the compiler "this method has a
virtual method in the base class".
 

Re:Deriving Classes From Base Class with virtual Methods

"JunkMail" < XXXX@XXXXX.COM >wrote in message
Quote
It is my understanding that if a base class has any virtual methods
in them, my derived class must redclare all of them and atleast
call the base classes method.
Not true at all. The only time you need to do that is if the base class
methods are "pure" virtual, ie:
virtual void f1() = 0;
Then the descendants are requires to implement the methods. Otherwise it is
not required, the base class itself could implement the methods with default
behaviors, and then the descendants can override just those methods that it
is actually interested in.
Quote
will Base::f3 get called when Derived::f2 is called
Yes, because it is calling Base::f2() which then calls f3(). Had
Derived::f2() not called Base::f2(), then no, f3() would not get called at
all.
Gambit
 

{smallsort}

Re:Deriving Classes From Base Class with virtual Methods

Quote
It is my understanding that if a base class has any virtual methods in
them,
my derived class must redclare all of them and atleast call the base
classes
method.
No, you don't have to. The language just does it for you. When you inherit
from the base class, you automatically have all these methods at your hand.
Only if you want to change the behaviour should you redefine the functions.
"virtual" just means someone *might* redefine it in a derived class if they
happen not to like the default behaviour.
Incidentally, OO theorists (Dr. B. Meyer) say all functions should be
virtual, and the "virtual" keyword shouldn't exist. I agree with them. But
this group is C++, not OO theory. :-)
--
Daniel
 

Re:Deriving Classes From Base Class with virtual Methods

And of course, if you do an incomplete download of the messages you won't
realize other people have already given a proper answer, ages before. :-)
--
Daniel
 

Re:Deriving Classes From Base Class with virtual Methods

Hi!
Daniel Daranas wrote:
Quote
Incidentally, OO theorists (Dr. B. Meyer) say all functions should be
virtual, and the "virtual" keyword shouldn't exist. I agree with them. But
this group is C++, not OO theory. :-)
Java does behave this way. I'm not sure whether I like it or not. I
still have to think about it. Well, in C++ non-virtual function calls
allow the compiler to inline the code, which make functors a powerful
tool to write fast OO code.
Frank