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

    Design pattern or plain old polymorphism?

    Hello there,

    I have a design issue that I'd like to share with you and hopefully get some suggestions to solve what should be a straight forward problem.

    I have a class (classA) that uses reference counting on the private data members - much like Qt does. This class is one of many classes that share a number of features, like logging and error checking - which are are done on the private data members (eg "p->_logMessage", where "p" is a pointer to the shared reference counted data).

    So I've decided to put some common functionality into a separate class, classB, which classA now inherits. What I'd like to do is to access various private methods of classA from classB.

    So,

    class classB {
    public:
    classB() {}
    void someFunc() { /* do stuff and log any errors*/ }
    };

    struct classAPrivate {
    string error;
    etc etc
    };

    class classA : public classB {
    public:
    classA() : classB() {}
    setError(string msg) { p->error = msg; }
    private:
    classAPrivate* p;
    }

    So in classB I'd like to somehow be able to access the "setError" function for classA. Eg when I call someFunc(), the error message may need to be set in classA.


    One solution I've got is to declare another abstract class, classC, which declares the setError() method as pure virtual. A pointer to classC is then given to classB in the constructor and stored as a private data member. Now it is possible to call setError() from classB using the classC pointer - which will call the implementation defined in classA. Phew!

    Question is, is there a better way of doing this? I've considered using functors for this, but this wouldn't be any cleaner than my current implementation - in fact a whole lot messier. Perhaps the command pattern?

    The problem could be condensed into the following:

    "How can I call private methods in a derived class from a base class".

    Any help much appreciated!

  2. #2
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Design pattern or plain old polymorphism?

    Why don't you just define the 'setError' method as virtual or pure virtual in classB, then change it in classA. Any classA object cast into a classB will then have access to classA's setError
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  3. #3
    Join Date
    Mar 2009
    Posts
    3

    Re: Design pattern or plain old polymorphism?

    Yes, that's an option - but I have a bunch of common methods that I'd like to implement only in the base class, but they need to act on the reference counted data classes which are private to classA.
    What I think I need is a template class - probably as part of classB that will define a getref() method that allows it access to classA's private data.

  4. #4
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: Design pattern or plain old polymorphism?

    Quote Originally Posted by nullhead View Post
    Yes, that's an option - but I have a bunch of common methods that I'd like to implement only in the base class, but they need to act on the reference counted data classes which are private to classA.
    What I think I need is a template class - probably as part of classB that will define a getref() method that allows it access to classA's private data.
    You seem to be thinking as if class A object is different from Class B object, when they are the same object

  5. #5
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Design pattern or plain old polymorphism?

    Quote Originally Posted by nullhead View Post
    Yes, that's an option - but I have a bunch of common methods that I'd like to implement only in the base class, but they need to act on the reference counted data classes which are private to classA.
    What I think I need is a template class - probably as part of classB that will define a getref() method that allows it access to classA's private data.
    If classA and classB both need to access the method, you should put it in classB.
    If you want it generic to any type, make the classes templates.
    If you want only classB to be generic to any type and classA to be a specific type, only make classB a template, and vise versa.
    If the data will only exist in classA, but you still want to be able to access the data with a method in classB, you need the classB version to be virtual or pure virtual, and implement the same method in classA.
    If you give classB pure virtual members, you won't be able to instantiate it.
    In short, if you want to access classA members from classB, either some placeholder data member must be defined in classB, or you use virtual methods and instantiate a classA object, then cast it to a classB object. If you want it generic, make it generic.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  6. #6
    Join Date
    Mar 2009
    Posts
    3

    Re: Design pattern or plain old polymorphism?

    Hi Etherous
    I've looked at Scott Meyer's item 29 in the "More Effective C++" book, which almost does the right thing, but it's a good start.

    Thanks for all your replies - much appreciated!

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
  •  





Click Here to Expand Forum to Full Width

Featured