dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] Calling derived class from address of base reference

  1. #1
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    [RESOLVED] Calling derived class from address of base reference

    Here's one that I'm not entirely sure about. Maybe some of the more knowledgeable gurus will be able to enlighten me.
    Code:
    class Base
    {
    public:
    	virtual void Function()
    	{
    		// Base function.
    	}
    };
    
    class Derived : public Base
    {
    public:
    	void Function()
    	{
    		// Derived function.
    	}
    };
    
    class Test
    {
    public:
    	void Set(Base &base)
    	{
    		p_base = &base;
    	}
    
    	void Call()
    	{
    		p_base->Function();
    	}
    
    private:
    	Base *p_base;
    };
    
    int main()
    {
    	Derived derived;
    
    	Test test;
    
    	test.Set(derived);
    	test.Call();
    
    	return 0;
    }
    My question is, is it guarenteed in all circumstances that test.Call() will call Function() of the derived class, regardless of the class size and complexity or are there some nasty slicing issues lurking?

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Calling derived class from address of base reference

    As long as you pass/store your objects as pointers/references and you invoke virtual functions on those objects, you will get what you need.
    Har Har

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Calling derived class from address of base reference

    In addition to PadaxArt's comment:

    1) Assuming there are no major flaws in the compiler or runtime.
    2) Assuming there are no hardware malfunctions or bugs.

    [Hey, you did use the word "always"....]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Calling derived class from address of base reference

    Cheers, I suspected that it was ok, but I just wanted to check I hadn't overlooked some 'gotcha' that would cause me (or someone else) grief later.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Calling derived class from address of base reference

    This is all part of an effort for me to expunge the last remenents of C from my coding style by passing by non-const reference rather than pointer.

    (Isn't expunge a great word!)

  6. #6
    Join Date
    May 2008
    Posts
    1

    Re: [RESOLVED] Calling derived class from address of base reference

    This always call the Derived class function, bcoz this function is virtual in the base class , its overrided in the DC ,

    and the object by which the function is called is base class obj pointer has the derived class obj pointer addres in that ,
    i think it wil always call DC function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)