Results 1 to 5 of 5

Thread: Unable to call overridden virtual function

  1. #1
    Join Date
    Nov 2009
    Posts
    72

    Unable to call overridden virtual function

    Hello friends,
    I am first year Computer Science student. In our last lecture I learn about virtual function. I have written following program on virtual function, but problem is that it unable to call overridden virtual function. I don't know what is the mistake in my program. Please help me.
    Code:
    class SomeParamEg;
    class IBases
    {
    public:
        virtual void Func(SomeParams* params = NULL)
        {
            cout << "Bases funcs";
        }
    };
    
    class DerivedsA : public IBases
    {
    public:
        void Funcs()
        {
            
            cout << "DerivedsA funcs";
            IBases::Funcs();
        }
    };
    
    class DerivedsB : public IBases
    {
    public:
        void Funcs()
        {
          
            cout << "DerivedsB funcs";
            IBases::Funcs();
        }
    };
    
    
    void FuncsCallers(IBases *instances1, IBases *instances2)
    {
        IBases *is1 = instances1;
        IBasess *is2 = instances2;
        i1->Funcs();
        i2->Funcs();
    }
    
    DerivedsA *as = new DerivedsA;
    DerivedsB *bs = new DerivedsB;
    FuncCallers(as,bs);

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Unable to call overridden virtual function

    From your information it seems that you have written wrong code and that's why you are getting such type of problem. You have written " FuncCaller()" which is wrong way to define function. This is wrong way of defining overriding like making the derived class function const. This is not possible when base class function is not constant.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Unable to call overridden virtual function

    You have written wrong code and that's why you are not able to call overridden virtual function in your code. In the following code I have use "SomeParamEg" class to include all methods. I also have use IBases class to overridden. In the following code I have use inheritance.
    Code:
    class SomeParamEg;
    class IBases
    {
    public:
        virtual void Func(SomeParams* params = NULL)
        {
            cout << "Bases funcs";
        }
    };
    
    class DerivedsA : public IBases
    {
    public:
        void Func(SomeParams* params = NULL)
        {
            
            cout << "DerivedsA funcs";
            IBases::Funcs();
        }
    };
    
    class DerivedsB : public IBases
    {
    public:
        void Func(SomeParamEg* params = NULL)
        {
           
            cout << "DerivedB funcs";
            IBases::Funcs();
        }
    };
    
    
    void FuncCaller(IBases *instances1, IBases *instances2)
    {
        IBase *is1 = instances1;
        IBase *is2 = instances2;
        is1->Funcs();
        is2->Funcs();
    }
    
    DerivedA *as = new DerivedsA;
    DerivedB *bs = new DerivedsB;
    FuncCaller(as,bs);

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Unable to call overridden virtual function

    You haven't define default parameter to virtual functions and that's why you are getting such type of problem. In your code you have to use default parameter like IBases to overridden virtual functions to fix this problem. I have written following code for you, just try to understand it.
    Code:
    class DerivedsB : public IBases
    {
    public:
        void Func(SomesParams* params=NULL)
        {
          
            cout << "DerivedsB funscs";
            IBases::Funcs();
        }
    };

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Unable to call overridden virtual function

    I think you have written wrong code and that's why you are unable to call overridden virtual function. You have to write following code to fix this problem. I have written following code for you. It is very simple code. In the following code I have use FuncCaller() function.
    Code:
    void FuncCaller(IBase instances)
    {
        instances.Funcs();
    }
    
    void Funcs()
    {
    DerivedsA *as = new DerivedsA;
    DerivedsB *bs = new DerivedsB;
    FuncsCallers(*as);
    FuncsCallers(*bs);
    }

Similar Threads

  1. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  2. Pure virtual function call
    By Heather5 in forum Software Development
    Replies: 4
    Last Post: 21-10-2009, 10:54 PM
  3. Winword.exe R6025 - Pure Virtual Function Call
    By Dharuna in forum MS Office Support
    Replies: 2
    Last Post: 09-06-2009, 12:27 AM
  4. Virtual function in c#
    By kaartik in forum Software Development
    Replies: 2
    Last Post: 10-01-2009, 12:10 PM
  5. Error Message #R6025 pure virtual function call
    By URmySunshine in forum Vista Help
    Replies: 1
    Last Post: 13-03-2008, 02:21 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,245,936.68043 seconds with 17 queries