virtual funtions

This is a discussion on virtual funtions within the C++ Programming forums, part of the General Programming Boards category; if you redefine a virtual function does your redefine code get added to the original function or does it replace ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    21

    virtual funtions

    if you redefine a virtual function does your redefine code get added to the original function or does it replace it?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    it replaces it.

    You basically call the function in the base class and the call actually gets dispatched to the same function but in the derived class as long as you use dynamic binding.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It is possible to call a base classes function though. Also you should know that when you do something like this.

    Code:
    class base {
    public:
        base() { whatever(); }
        virtual ~base() { }
        virtual void whatever() { 
             cout << "hello world";
        }
    };
    
    class child : public base {
    public:
        child() : base() { } //this is done by default
        virtual ~child() { }
        virtual void whatever() {
            cout << "something";
        }
    };
    The whatever() called in base's constructor will be base's whatever() even when a child is created. What I mean (that wasn't very clear) is that when a new child calls base's constructor during its own construction base::whatever() will be called NOT child::whatever().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 05:08 AM
  2. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 02:42 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21