vitual functions

i have some questions on this topic:
1. What exactly are virtual functions all about?
2. What is a virtual destuctor?
3. If a base class declares a function to be virtual, and a derived class does not use the term virtual when over riding that class, is it still virtual when inherited by a third generation class?
4. What is wrong with this code snippet?
void SomeFunction(Shape);
Shape *pRect = new Rectangle;
SomeFunction(*pRect);

Comments

  • [b][red]This message was edited by AsmGuru62 at 2002-8-29 7:34:59[/red][/b][hr]
    [b][red]This message was edited by AsmGuru62 at 2002-8-29 7:32:38[/red][/b][hr]
    [b][red]This message was edited by AsmGuru62 at 2002-8-29 7:31:47[/red][/b][hr]
    : i have some questions on this topic:
    : 1. What exactly are virtual functions all about?
    [blue]For performing different behaviour with the same function name. Example: objects RECT and CIRCLE both have the function SHOW() - but the code (the body of the function) is different in each case - one draws rectangle, other - circle, so this function must be virtual.[/blue]
    : 2. What is a virtual destuctor?
    [blue]It allows to call a destructor of the base class. Say you have an object (of a class A) which allocates some memory and releases it in its destructor. If you derive B class from A and A class does not have a virtual destructor - the code which releases the memory will be not called - it results in a memory leak. Always make a virtual destructor, just like the code Wizard does.[/blue]
    : 3. If a base class declares a function to be virtual, and a derived class does not use the term virtual when over riding that class, is it still virtual when inherited by a third generation class?
    [blue]Some compilers do that - some do not. To avoid the 'bad' compiler behaviour - always use the 'virtual' key - it is good by itself - looking at your code will prove easy to understand what methods are overriden in your class.[/blue]
    : 4. What is wrong with this code snippet?
    : void SomeFunction(Shape);
    : Shape *pRect = new Rectangle;
    : SomeFunction(*pRect);
    :
    [blue]If some object derived from Shape and passed into that function - it can be trouble. To avoid this you have to pass a pointer:

    : void SomeFunction(Shape*);

    : Shape *pRect = new Rectangle;
    : Shape *pCircle = new Circle;

    : SomeFunction(pRect);
    : SomeFunction(pCircle);

    [/blue]






  • : : i have some questions on this topic:
    : : 1. What exactly are virtual functions all about?

    : [blue]For performing different behaviour with the same function name. Example: objects RECT and CIRCLE both have the function SHOW() - but the code (the body of the function) is different in each case - one draws rectangle, other - circle, so this function must be virtual.
    [/blue]

    [red] RECT and CIRCLE both derive from a common base class SHAPE which provides a virtual function SHOW(). Simply having the same method is not enough, nor is it enough even if they come from a common base, nor is it enough even if the common base defines the function. ALL of the conditions in the first case must be true.[/red]


    : : 2. What is a virtual destuctor?
    : [blue]It allows to call a destructor of the base class. Say you have an object (of a class A) which allocates some memory and releases it in its destructor. If you derive B class from A and A class does not have a virtual destructor - the code which releases the memory will be not called - it results in a memory leak. Always make a virtual destructor, just like the code Wizard does.[/blue]

    [red] That's not the problem. The problem is _B_ will not have it's destructor called, instead A's will be called on a B object, which is fine except that you also want B's called. What happens when you call a non-virtual function of A using an A * pointing to an object of type B? The same is exactly what happens with deconstructors. This only applies when using a base class pointer accessing a derived class. [/red]

    : : 3. If a base class declares a function to be virtual, and a derived class does not use the term virtual when over riding that class, is it still virtual when inherited by a third generation class?
    : [blue]Some compilers do that - some do not. To avoid the 'bad' compiler behaviour - always use the 'virtual' key - it is good by itself - looking at your code will prove easy to understand what methods are overriden in your class.[/blue]
    [red] Borland is pretty good with standards compliance, here's what they say. This isn't from the C++ Standard (I don't feel like scouring it right now), but it's probably accurate. Even if it is not, then if you want your code to work with Borland you'll need to follow these rules.
    [code]
    The overriding function vf1 in D [derived from B] is automatically
    virtual. The virtual specifier can be used with an overriding function
    declaration in the derived class. If other classes will be derived from
    D, the virtual keyword is required. If no further classes will be
    derived from D, the use of virtual is redundant.
    [/code][/red]

    : : 4. What is wrong with this code snippet?
    : : void SomeFunction(Shape);
    : : Shape *pRect = new Rectangle;
    : : SomeFunction(*pRect);
    : :
    : [blue]If some object derived from Shape and passed into that function - it can be trouble. To avoid this you have to pass a pointer:
    :
    : : void SomeFunction(Shape*);
    :
    : : Shape *pRect = new Rectangle;
    : : Shape *pCircle = new Circle;
    :
    : : SomeFunction(pRect);
    : : SomeFunction(pCircle);
    :
    : [/blue]

    [red] As AsmGuru62's example demonstrates, you can only use the polymorphism of classes through base pointers or references. To pass by value would require knowing the size of the object at compile-time. Shape is almost certainly smaller than Rectangle, so it would be a problem to pass a Rectangle object in Shape's place by value. [/red]

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories