Thread: Changing an inherited function

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    Changing an inherited function

    Hello.

    I have a class called CMinion, which inherits from CEnemy.
    CEnemy gives CMinion a Render() function, still CMinion wants to change this Render() function a little by adding a little something. How do I let CMinion change the Render() function? I've seen it done somewhere I believe...

    More in detail. CEnemy's Render() function renders body parts, but CMinion wants to render an outfit too... Like, some extra things only CMinion is allowed to wear...

    I ain't fully sure how to explain this, but hope you people get it :P

    Thanks in advance!
    Currently research OpenGL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Akkernight
    CEnemy gives CMinion a Render() function, still CMinion wants to change this Render() function a little by adding a little something. How do I let CMinion change the Render() function?
    What do you mean by "adding a little something", and is Render() a virtual function?

    Quote Originally Posted by Akkernight
    CEnemy's Render() function renders body parts, but CMinion wants to render an outfit too.
    What are "body parts" and "outfit"?

    Basically, if Render() is a virtual function, then CMinion can override it. But if you need to change the signature of Render(), then either way you have to overload rather than override.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you have:
    Code:
    class CEnemy
    {
    public:
       virtual void Render(...);
    };
    
    class CMinion : public CEnemy
    {
    public:
       virtual void Render(...);
    }
    Ok so far?

    Then you want CMinion to do something MORE than CEnemy's render, but ALSO do CEnemy's render without having to dublicate all the code?

    Code:
    void CMinion::Render(...)
    {
       // Do CMinion specifc render operation. 
      CEnemy::Render(...);
      // Possibly more CMinion specific rendering. 
    }
    is the usual way to do this.

    Of course, if you have to do things in the middle of, you either have to split the render into two parts and make member two functions.

    The other solution is to have CEnemy::Render() call another virtual member function, which does different things when called on CMinion than on CEnemy.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Awesome, thanks!
    This was my code...
    Code:
    void CMinion::Render(){
    
    	CSprite::Render();
    	outfit.Render();
    
    }
    ofc, CEnemy got Render() inherited by a pretty long inheritance list, but I didn't wanna get into all that :P
    Currently research OpenGL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Advice changing a function please
    By cjohnman in forum C Programming
    Replies: 16
    Last Post: 05-12-2008, 12:56 PM
  4. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM