CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2009
    Posts
    28

    re-assigning a non-pointer object variable

    Hi All,

    I am working on a project that has a large base class that uses a baseUtil class:
    Code:
    Class BaseClass
    {
    public: 
        BaseUtil m_baseUtil;
    }

    I have derived both these classes: DerivedClass and DerivedUtil
    Code:
    Class DerivedClass : public BaseClass
    {
    public: 
        DerivedUtil m_derivedUtil;
    }
    The BaseClass and BaseUtil are huge classes; BaseClass calls BaseUtils at multiple instances. I am inheriting most of the features and making very few changes in the derived classes.

    My objective is: I want BaseClass to use DerivedUtil instead of BaseUtil.
    The problem is: BaseClass is using a regular object, and not pointer, for BaseUtil. Further, the BaseUtil has disabled copy constructor and assignment operator with the following code in the "private" section

    Code:
        BaseUtil(const BaseUtil&right);
        BaseUtil& operator=(const BaseUtil&right);
    Is there an elegant solution, or should I just make changes in BaseClass and BaseUtil rather than using inheritance and polymorphism.

    Thanks for looking.
    best,
    SG

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: re-assigning a non-pointer object variable

    Why don't you make a BaseUtil* member of BaseClass. Set it to point to whatever Util class is appropriate for your class that contains it.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: re-assigning a non-pointer object variable

    Quote Originally Posted by sgiri1981 View Post
    Is there an elegant solution, or should I just make changes in BaseClass and BaseUtil rather than using inheritance and polymorphism.
    What you have here is a (incorrectly implemented) Strategy pattern. The alternative for that using templates instead of inheritance is known as Policy Based Design. Which one is more appropriate depends on your requirements.

    In addition to GCDEF's answer: when you change the m_baseUtil member to a pointer in order to allow polymorphism, it's probably useful to incorporate a Factory pattern in the BaseClass, assuming the DerivedClass determines which kind of util class should be instantiated. You can do this with a virtual function in the base class to create this instance. Downside is that you cannot call this function in the constructor (since it's virtual).
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: re-assigning a non-pointer object variable

    Quote Originally Posted by sgiri1981 View Post
    I am inheriting most of the features and making very few changes in the derived classes.
    I think this is where you are going down the wrong path.

    You shouldn't inherit from a base class just to save typing in code. If you're using public inheritance, then the expectation is that Derived IS-A Base class (the IS-A principle). For example, an Animal can move, make noise, and die. Does that mean you can derive an Engine from Animal just because an Engine also moves, makes noise, and can die? Of course not.

    If you're inheriting just to save typing and that the interface is similar, then that should not be a reason for public inheritance. Instead, as D_Drmmr stated, you are using a strategy pattern, and that is usually done via templates and policy-based design, not inheritance.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2009
    Posts
    28

    Re: re-assigning a non-pointer object variable

    Thanks for your inputs.

    The reason I am inheriting is for code management. I am using the classes made available to me by other teams; these classes are typically large files. I need to make modifications to functionalities of these classes (i work on Medical Imaging systems; the classes control different aspects of the imaging system).

    I use inheritance and polymorphism to avoid making changes to the code that is given to me by other teams after thorough testing. My changes are then restricted to on my classes which have very few lines of code; it helps me keep track of my changes (I work on R&D and we try new strategies almost every day).

    I will dig deeper into the patterns concepts that you have suggested.

    Thanks again for your suggestion.

    Best,
    SG

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: re-assigning a non-pointer object variable

    If you're leveraging another class's implementation but no other part of the program needs to know that, consider using private inheritance or composition.

  7. #7
    Join Date
    Feb 2009
    Posts
    326

    Re: re-assigning a non-pointer object variable

    As per my understanding (I could be completely wrong, so feel to correct me)
    - BaseClass is used by multiple teams.
    - you want to use the same BaseClass but want to use it DerivedUtil instead of BaseUtil

    I have a given a small program which tries to do the above mentioned:

    Explanation:
    - BaseUtil has 2 functions f1() and f2()
    - DerivedUtil doesn't re-implent f1() as it is happy with BaseUtil implementation
    - DerivedUtil re-implements f2(), therefore BaseUtil :: f1() is made virtual
    - BaseClass specification is modified to make m_derivedUtil as reference to BaseUtil.
    - m_derivedUtil is made as a reference instead of a pointer, so that the syntax remains the same in all the other occurrences of m_derivedUtil


    Code:
    #include <iostream>
    
    class BaseUtil
    {
        public:
            void f1() { std :: cout << "BaseUtil :: f1()\n"; }
            virtual void f2() { std :: cout << "BaseUtil :: f2()\n"; }
    };
    
    class DerivedUtil : public BaseUtil
    {
        public:
            //f1() is not modified assuming you are happy with the base class's implementation
            void f2() { std :: cout << "DerivedUtil :: f2()\n"; } //Re-implemented function
    };
    
    class BaseClass
    {
        public:
            BaseClass(BaseUtil& pBaseUtil) : baseUtilRef(pBaseUtil) {}
            void display() { baseUtilRef.f2(); }
        private:
            BaseUtil& baseUtilRef;
    };
    
    int main()
    {
        BaseUtil bu; 
        DerivedUtil du; 
    
        BaseClass bc1(bu), //The object is constructed outside BaseClass and passed to it
                  bc2(du); //There could be many ways BaseUtil and DerivedUtil are created (eg Factory method)
    
        bc1.display();
        bc2.display();
    
        return(0);
    }
    BaseUtil's copy constructor and assignment operator are private:
    ========================================
    It may be declared as private for some reason. See if you can use a reference instead of copying / assigning, if that is not possible you might need to check with someone as to why it was made private.
    Last edited by Muthuveerappan; July 9th, 2011 at 05:23 AM. Reason: added more explanation

  8. #8
    Join Date
    May 2009
    Posts
    28

    Re: re-assigning a non-pointer object variable

    Thanks Muthuveerappan.
    This still requires changing the code of BaseClass (the current implementation does not have an overloaded constructor.
    I guess it is just poor design that I am dealing with, and what the earlier replies have alluded to. I will forward these suggestions for the next release

    Best regards,
    SG

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured