0

I have problems to re-use parts of C++ code.

I need to register a number of objects in a table with the following code:

typedef map<ResourceType_t, GeneralResource*>   ResourceTable_t;
ResourceTable_t m_resourceTable;

template<class _Ty1,
class _Ty2> inline
pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)
{   // return pair composed from arguments
return (pair<_Ty1, _Ty2>(_Val1, _Val2));
}


void MainModule::registerResource(ResourceType_t type, GeneralResource* pGeneral)
{
m_resourceTable.insert(make_pair(type, pGeneral)); 
}

However, for my new case, I need to fill the table with ojbects of type SpecificResource. How can I approach this new situation while still making use of m_resourceTable?


EDIT:

The strategy of inheritance seems to work, however, a new issue pops up:

class DifferentResource : 
              public GeneralDifferentResource, 
              public SpecificResource

GeneralDifferentResource inherites from GeneralResource. Now, we have some methods result into ambigious access problems. How to let DifferentResource first resolve methods from GeneralDifferentResource and second from SpecificResource (to resolve ambiguity) ? How ambiguity can be resolved is explained here: StackOverflow Question on Ambiguity

Community
  • 1
  • 1
poseid
  • 6,986
  • 10
  • 48
  • 78
  • I edited your code. don't write `
    ` tag when quoting code. Just use `{}` from the editor's menu.
    – Nawaz Jun 07 '11 at 14:34
  • 1
    Are you really defining make_pair yourself? If so, why? –  Jun 07 '11 at 14:34
  • make_pair is a given definition in the project... not sure either why it would have advantages... – poseid Jun 07 '11 at 14:47

2 Answers2

3

However, for my new case, I need to fill the table with objects of type SpecificResource.

Derive SpecificResource from GeneralResource and make it's destructor virtual:

class GeneralResource 
{
    virtual ~GeneralResource() {}
    //..
};

class SpecificResource : public GeneralResource 
{
   //...
};

If GeneralResource is not that general, then define an AbstractResource from which you can derive all other resource classes:

class AbstractResource
{
    virtual ~AbstractResource() {}
    //...  
};
class GeneralResource : public AbstractResource
{
    //..
};
class SpecificResource : public AbstractResource
{
   //...
};

And if you follow this approach, then use this map:

typedef map<ResourceType_t, AbstractResource*>   ResourceTable_t;
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • thanks for the example... it tried to define SpecificResource indeed... but without using inheritance... I will explore that path and let you know – poseid Jun 07 '11 at 14:41
  • hmm... ok... the compiler complains about "ambiguous" access to a method initForEnding() in a child of SpecificResource. – poseid Jun 07 '11 at 15:10
  • @poseid: how would I know without seeing the code? Why don't you post the code? – Nawaz Jun 07 '11 at 15:12
  • with strategy 1 (I think it is the "decorator" pattern), the compiler complains about "ambiguous" access to a method initForEnding() in a child of SpecificResource. I found this article explaining to make the inheritance virtual: http://www.dreamincode.net/forums/topic/87971-multiple-inheritance-in-c/ I will try ... – poseid Jun 07 '11 at 15:14
1

Have SpecificResource inherit from GeneralResource, if it makes sense for you.

dario_ramos
  • 7,118
  • 9
  • 61
  • 108