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

Thread: Polymorphism basics

  1. #1
    Join Date
    May 2015
    Posts
    144

    Polymorphism basics

    Hello,

    In the following code snippet: The classes used are
    Code:
    class AI_DATA MultiTechCell : public LogicalBase, 
     public MultipleActiveAntennasOwner,
     public DynamicTechParamHolder
    {
    }	 
     
     
    class AI_DATA LogicalBase : public AIObjectBase, public StatusObject
    {
    }
    
    class AI_DATA StatusObject : public FilterableObject
    {
    }
    The GenerateTipText(), the MultiTechCell object's base class pointer ie FilterableObject is got. With that base class pointer we call IsAttributeValid() function. In that function we call

    const FilterableObject::AttributeData* pAttributeData(GetObjectAttributes());

    the GetObjectAttributes().

    This function is defined as virtual in the base class FilterableObject, and my expectation was it calls the MultiTechCells GetObjectAttributes(). But to my surprise, it is calling the intermediate class LogicalBase 's GetObjectAttributes()/

    Could some experts help me with this..I want to call the MultiTechCells's method.
    Code:
     std::string ViewTipsGenerator::GenerateTipText(std::list<AI_DBObject*>& lstObjects, HWND hWnd) const
    {
    
        for ( ; iterObjects != iterObjectsEnd; ++iterObjects)
         {
             AI_DBObject* pObj = *iterObjects;
    
              FilterableObject* pFiltObj = NULL;
    
              if (pObj)
                pFiltObj = pObj->GetFilterableObject();
    
              if (!pFiltObj->IsAttributeValid(rAttribID.GetAttribute()))
                     continue;
    
    }
    
    --------------------------------------------------------
    FilterableObject* MultiTechCell::GetFilterableObject()
    { 
    return this; 
    }
    ---------------------------------------------------------
    Code:
    bool FilterableObject::IsAttributeValid(ObjectAttributes::Attribute eAttrib) const
    {
    //call GetAttributes and iterate the list looking for match with eAttrib
    const FilterableObject::AttributeData* pAttributeData(GetObjectAttributes());
    }
    
    virtual const FilterableObject::AttributeData* GetObjectAttributes() const = 0;
    --------------------------------------------------------

    Code:
    const FilterableObject::AttributeData* MultiTechNode::GetObjectAttributes() const
    {
        using namespace ObjectAttributes;
        using namespace FilterableObjectEnums;
    
         const FilterableObject::AttributeData* pAttributeData(MultiTechNode::GetAttributes());
    
         return pAttributeData;
    }
    
    const FilterableObject::AttributeData* GetObjectAttributes() const override;
    --------------------------------------------------------
    Code:
    const virtual FilterableObject::AttributeData* GetObjectAttributes() const override;
    
    const FilterableObject::AttributeData* LogicalBase::GetObjectAttributes() const
    {
       using namespace ObjectAttributes;
       using namespace FilterableObjectEnums;
    
       const FilterableObject::AttributeData* pAttributeData(LogicalBase::GetAttributes());
    
       return pAttributeData;
    }
    ------------------------------------------------------



    +++++++++++++++++
    Last edited by VictorN; July 25th, 2019 at 01:31 AM. Reason: Added CODE tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,423

    Re: Polymorphism basics

    Quote Originally Posted by pdk5 View Post
    Hello,

    In the following code snippet: The classes used are
    Code:
    class AI_DATA MultiTechCell : public LogicalBase, 
     public MultipleActiveAntennasOwner,
     public DynamicTechParamHolder
    {
    }
    ...
    What is AI_DATA in this class definition?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,764

    Re: Polymorphism basics

    Can you produce and share the minimal console code that shows this behaviour.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.2)

  4. #4
    Join Date
    May 2015
    Posts
    144

    Re: Polymorphism basics

    @kaud: Thanks a lot. I think, now I found the issue. Sorry it was my mistake.

    There were many places, where the override function needed to be added for my bugfix. This is because i had added a pure virtual function in one of the base classes. I missed it in one of the derived classes. And so underlying second order baseclass function was getting called.

    Now i added the function in the respective derived class and it works ok


    It is some what like: A is base class. B is derived from A. C is derived from B

    I added pure virtual function in A.
    Because legacy code was instantiating B, I had to add that in B.
    But didnot add in C.

    So C;s function was not getting called.

    There were many derived classes from the A. So had to correct in all those places, and missed out one of them

  5. #5
    Join Date
    Feb 2017
    Posts
    458

    Re: Polymorphism basics

    Quote Originally Posted by pdk5 View Post
    This is because i had added a pure virtual function in one of the base classes.
    Once you add a pure virtual function I think it's time to start thinking in terms of object oriented programming (OO).

    Your current design is miles away from good OO. Interfaces should be pure virtual, there should be no inheritance of implementation other than in implementation classes, etcetera.

    You current design will cause you nothing but grief. Look at what a small oversight got you at this stage already. Just imagine what will happen down the line when someone tries to modify your code. In OO terms your design is brittle.
    Last edited by wolle; July 26th, 2019 at 03:30 PM.

  6. #6
    Join Date
    May 2015
    Posts
    144

    Re: Polymorphism basics

    @wolle: Not sure. But this change is after discussion with the senior c++ guy who has more than 20 yrs in c++.
    I myself is coming back to c++ after one year break.

    Could you elaborate your concerns about the code ?

    Quote Originally Posted by wolle View Post
    Once you add a pure virtual function I think it's time to start thinking in terms of object oriented programming (OO).

    Your current design is miles away from good OO. Interfaces should be pure virtual, there should be no inheritance of implementation other than in implementation classes, etcetera.

    You current design will cause you nothing but grief. Look at what a small oversight got you at this stage already. Just imagine what will happen down the line when someone tries to modify your code. In OO terms your design is brittle.

  7. #7
    Join Date
    Feb 2017
    Posts
    458

    Re: Polymorphism basics

    Quote Originally Posted by pdk5 View Post
    @wolle: Not sure. But this change is after discussion with the senior c++ guy who has more than 20 yrs in c++.
    Well, if you work for a company and this is the consensus way of using C++ then of course that's what you should do. And if you're working with legacy code then introducing a virtual function in an implementation base class may even be the best option, I don't know.

    But my general point is that when you start using the OO constructs of C++ I think it's a good idea to have a look at OO design. It contains a wealth of information about how to make best use of inheritance and virtual functions. You will benefit from that to produce better regular C++ code even without committing to full OO.
    Last edited by wolle; July 30th, 2019 at 12:16 AM.

Tags for this Thread

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)