+ Reply to Thread
Results 1 to 2 of 2

Thread: Why derive class method does not get executed?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    38

    Why derive class method does not get executed?

    Following code should execute derive class method. Am I Right or am I missing something?

    Code:
    class CMyBase
    {
    public:
    	static void Method1()
    	{
    		std::cout << "CMyBase::CallMe" << std::endl ;
    	} ;
    } ;
    
    class CMyDerive : public CMyBase
    {
    private:
    	static void Method1()
    	{
    		std::cout << "CB::CallMe" << std::endl ;
    	} ;
    } ;
    
    void UseCallMe()
    {
    	CMyBase *pMyBase = new CMyDerive() ;
    	pMyBase->Method1() ;
    }
    Thanks.

  2. #2
    It seems that you are mixing the 'static' keyword with 'virtual'.

    'static' keyword does not have polymorphism associated with.

    You should use 'virtual' keyword rather than using 'static'.

    Example:

    Code:
    class CMyBase
    {
    public:
    	virtual void Method1()
    	{
    		std::cout << "CMyBase::CallMe" << std::endl ;
    	} ;
    } ;
    
    class CMyDerive : public CMyBase
    {
    private:
    	virtual void Method1()
    	{
    		std::cout << "CB::CallMe" << std::endl ;
    	} ;
    } ;
    
    void UseCallMe()
    {
    	CMyBase *pMyBase = new CMyDerive() ;
    	pMyBase->Method1() ;
    }

+ Reply to Thread

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