Virtual Function Question

V

vkp

Hi All,

Need little education on use of Virtual functions....the questions
arose from the existing code I am going through.

Class type -> Derived, Member funtion -> Not virtual, Calls Derived
first then base.
Class type -> Derived, Member funtion -> Virtual, Calls Derived first
then base.
Class type -> Base, Member funtion -> Not Virtual, Calls Base.
Class type -> Base, Member funtion -> Virtual, Calls Derived first
then base.

What happens if both Base class and derived class declare a function
virtual. Who gets the first call? Is it valid to delclare a function
Virtual in both classes? If valid, is it a good design? Is there a
situation that requires such declarations?

Thanks in advance !
-vkp.
 
R

Ron

Hi All,

Need little education on use of Virtual functions....the questions
arose from the existing  code I am going through.

Class type -> Derived, Member funtion -> Not virtual, Calls Derived
first then base.

I think you are confused. C++ doesn't autochain back to the base
class
(except for the constructors and destructors). Only the Base member
is called.
Class type -> Derived, Member funtion -> Virtual, Calls Derived first
then base.

Only the derived member is called.
Class type -> Base, Member funtion -> Not Virtual, Calls Base.
correct.

Class type -> Base, Member funtion -> Virtual, Calls Derived first
then base.

No, why would it call the non-existant Derived member? Only the base
is called.
What happens if both Base class and derived class declare a function
virtual. Who gets the first call? Is it valid to delclare a function
Virtual in both classes

Once a function is declared virtual in a base class, it is virtual in
all the derived classes, whether the programmer declares the derived
ones virtual or not (it's redundant to do so).

If valid, is it a good design? Is there a
situation that requires such declarations?

Some people like to do it because it reminds them the member
is virtual. However, it's a very weak statement (as the
function might not be virtual in the base class).
 
P

Paul N

Hi All,

Need little education on use of Virtual functions....the questions
arose from the existing  code I am going through.

Class type -> Derived, Member funtion -> Not virtual, Calls Derived
first then base.
Class type -> Derived, Member funtion -> Virtual, Calls Derived first
then base.
Class type -> Base, Member funtion -> Not Virtual, Calls Base.
Class type -> Base, Member funtion -> Virtual, Calls Derived first
then base.

What happens if both Base class and derived class declare a function
virtual. Who gets the first call? Is it valid to delclare a function
Virtual in both classes? If valid, is it a good design? Is there a
situation that requires such declarations?

You're missing an important point. If the function is not virtual,
then which function gets run depends on the type of the *pointer* you
are using. If the function is virtual, then which function gets run
depends on the type of the object itself. So, for instance, if you
have a pointer of type Base * pointing to an object of type Derived,
then you get the base class non-virtual functions but the derived
class virtual functions.

I wrote an example illustrating this - have a look for my post in this
group of 15 Sept 2006, which you can find by searching for gw7rib and
bark. Thomas Tutone added a few further points to it.

Hope that helps.
Paul.
 
V

vkp

Show some code.

--
   Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)- Hide quoted text -

- Show quoted text -

Here is sample code for the question:

Case 1: Base class declares function "test" as virtual so method
doTest calls derived class when input object is of derived type. (This
is clear to me).
// Base class
class base
{
public:
base(void);
~base(void);
virtual void test(){
std::cout<<"Base::test called\n";
}

};

// Derived class
class derived :
public base
{
public:
derived(void);
~derived(void);
void test(){
std::cout<<"Derived::test called\n";
}


};

// Function that calls class methods.

// function expects base class and calls method a.
void doTest (base& a_base)
{
std::cout << "Calling functions in the base class\n";
a_base.test();
}

// Main function.

int _tmain(int argc, _TCHAR* argv[])
{
dreived a_derived; // derived object
base b_base; // base object
doTest(a_derived); // calls method that expects Base object
doTest(b_base);
return 0;
}
// End of code.

Case 2: What if class derived also declares function "test" as
virtual? I checked this in VC++ and saw that it has no effect....but
just curious about the questions I asked in the original post.

Thanks in advance.

-vkp.
 
P

Paul N

class base
{
public:
        base(void);
        ~base(void);
        virtual void test(){
                std::cout<<"Base::test called\n";
        }

};

// Derived class
class derived :
        public base
{
public:
        derived(void);
        ~derived(void);
        void test(){
        std::cout<<"Derived::test called\n";
        }

};
Case 2: What if class derived also declares function "test" as
virtual? I checked this in VC++ and saw that it has no effect....but
just curious about the questions I asked in the original post.

If the base class declares "test" as virtual, then it is virtual in
derived as well. Some people would declare it as virtual in the
derived class, but this is unnecessary and is probably just done for
the benefit of the reader.
 
V

vkp

Here is sample code for the question:
Case 1: Base class declares function "test" as virtual so method
doTest calls derived class when input object is of derived type. (This
is clear to me).
// Base class
class base
{
public:
   base(void);
   ~base(void);
   virtual void test(){
           std::cout<<"Base::test called\n";
   }

// Derived class
class derived :
   public base
{
public:
   derived(void);
   ~derived(void);
   void test(){
   std::cout<<"Derived::test called\n";
   }

// Function that calls class methods.
// function expects base class and calls method a.
void doTest (base& a_base)
{
   std::cout << "Calling functions in the base class\n";
   a_base.test();
}
// Main function.
int _tmain(int argc, _TCHAR* argv[])

This is a Microsoftism. Unless you're posting to a Microsoft forum, use

int main()

or

int main(int argc, char *argv[])
{
   dreived a_derived; // derived object
   base b_base;            // base object
   doTest(a_derived);   // calls method that expects Base object
   doTest(b_base);
   return 0;
}
// End of code.
Case 2: What if class derived also declares function "test" as
virtual?

Doesn't matter. When a function in a base class is declared virtual, a
function in a derived class with the same signature overrides it. That's
what you're seeing.

The overriding function is also virtual, so if you had yet another level
of derivation above that, a function with the same signature would
override both derived::test and base::test.

--
   Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)- Hide quoted text -

- Show quoted text -


Thanks Pete, I will keep in mind your warning !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top