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

Threaded View

  1. #7
    Join Date
    Jan 2009
    Posts
    596

    Re: Reference to a pointer of another type

    Quote Originally Posted by monarch_dodra View Post
    The scenario is this:

    We have class Worker, and we have class Manager. Each Manager manages exactly 1 Worker.

    Because of this, basically, the Manager Class has a Worker* member for manipulating the corresponding worker in a generic fashion.

    here is the catch. There are 3 types of worker: worker1, worker2 and worker3. These workers have methods that are unique to them (eg not shareable via the base interface). To manage these workers, we have Manager1, Manager2 and Manager3 etc...

    Manager1 only handles Worker1 etc...

    Basically, it is parallel hierarchy.
    Instead of having the pWorker in the Manager class, as a base class pointer, why not have derived class pointers in each of the Manager subclasses? You can then access these in Manager through a virtual method GetWorker():
    Code:
    class Manager
    {
    	protected:
    		virtual Worker* GetWorker() = 0;
    };
    
    class Manager1 : public Manager
    {
    	protected:
    		Worker1* pWorker1;
    
    		virtual Worker* GetWorker()
    		{ return pWorker1; };
    
    	public:
    		Manager1() : Manager()
    		{pWorker1 = new Worker1;}
    }
    In the specific Manager classes you will always have the worker as the full type, so can use this directly.
    Last edited by Peter_B; December 9th, 2011 at 12:38 PM. Reason: Forget * on Worker1 data member

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