Thread: Virtual Function Help.

  1. #1
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    Virtual Function Help.

    I'm not really comfortable with these 'pure virtual' functions or 'abstract
    classes' yet, so I don't know if I'm using them right.

    But, I have a base class that has a few virtual functions, then I
    have a derived class that has those functions defined, plus a few
    more. Can I not use the extra functions that are in the derived
    class?

    Code:
    class cl_One
    {
        public:
          cl_One();
          virtual ~cl_One();
    
          virtual void Function1(void) = 0;
          virtual void Function2(void) = 0;
    
        private:
          int var1;
          int var2;
    };
    
    class cl_Two: public cl_One
    {
         public:
           cl_Two();
           ~cl_Two();
    
           void Function1(void);
           void Function2(void);
           //extra function..
           void Function3(void);
        
        private:
           int var3;
           int var4;
    };
    
    cl_One* Cowptr = new cl_Two;
    
    Cowptr->Function3();
    I get a "Function3() is not a memober of 'cl_One' error. Do I need
    to include a pure virtual Function3 in cl_One? I can do that, but
    not all of my classes that derive from cl_One will use a Function3().
    Is it common to just define these unused functions as functions
    that do nothing ({}), or am I using these virtual functions completely
    wrong?

    What do I do?

    Thanks,

    ethic.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    70

    > cl_One* Cowptr = new cl_Two;


    Here you are creating new object of derived class and use it as a pointer to base class. So it will not allow you to access Function3() which is declared and defined in derived class only.


    If you want to access derived class's function through pointer to base class then you must have to define the same function in base class as virtual function. And that is wright use of virtual functions.


    >am I using these virtual functions completely wrong?


    No man....this is the use of virtual functions. You are doing right thing.
    Chintan R Naik

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Creating a pointer to the base class will allow you to access all common functions of any derived class (namely, those functions which belong to the base class). If you have a pointer to a base class, then you can try to cast it to the derived type as follows:

    Code:
    cl_One* ptr1 = new cl_Two;
    cl_One* ptr2 = new cl_Three;
    
    cl_Two* ptr3 = dynamic_cast<cl_Two*>(ptr1);
    
    cl_Two* ptr4 = dynamic_cast<cl_Two*>(ptr2);
    
    if(ptr3 != 0) // Cast succeeded
       ptr3->Function3();
    else
       ...
    
    if(ptr4 != 0)
       ...
    else // Cast failed, null pointer returned.
       std::cout << "ptr2 was not a pointer to an instance of cl_Two.";
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    One other thing, you can use dynamic_cast on references as well, but you have to be sure of what derived type you have, because you cannot check if dynamic_cast failed.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Stroustrup Talk on C++0x
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-20-2007, 02:02 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM