Jump to content
  • Advertisement
Sign in to follow this  
Verg

chicken/egg "typedef"

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Seems like this has come up before, but I'm just too stupid to remember how it goes... I need a typedef in a class... but if I make that class a "template", then you can't reference it (unless you know the template parameters). So... you derive from a base...
template<typename NeededType>
class WorkerImpl : public Worker{

public:

     typedef typename NeededType   the_type;
};
Fine... but, what if you have a cyclical dependency... where the Worker type is constructed by passing an object that depends on the Worker type?
template<typename NeededType>
class WorkerImpl : public Worker{

public:

     WorkerImpl(Aggregated<WorkerImpl<NeededType> > &a) {}

     typedef typename NeededType   the_type;
};
This "Aggregated" class had a previous "friend" relationship with the non-templated version of "Worker"... so...
template<typename WorkerType>
class Aggregated{

public:

protected:

    friend typename WorkerType;
};
The complication being that "Aggregated<WorkerType>" (which used to NOT be templated) is used in multiple places, in other classes... usually in places where "WorkerType" is not known. So:
template<typename WorkerType>
class AggregatedImpl : public Aggregated{

public:

protected:

    int _test_friendship;

    friend typename WorkerType;
};
So now, the class looks like this:
template<typename NeededType>
class WorkerImpl : public Worker{

public:

     WorkerImpl(AggregatedImpl<WorkerImpl<NeededType> > &a)
     {
          int foo = a._test_friendship;  // everything good so far
     }

     typedef NeededType   the_type;
};
==================================================================== All this is fine, actually... except for a small detail... All of the implementation will now be exposed in the header file where both AggregatedImpl<> and WorkerImpl<> are defined, and that's not desired. Template specializations in a .cpp file are not a scaleable way to go about hiding the implementations. Maybe something else about this "design" is bugging me, too... but I'm not sure what it is [smile]... basically I'm just looking to instantiate objects in the application code (at the top-most level), and have "types" propogate down. The "NeededType" would be referenced like this: NeededType::fn() where "NeededType::fn()" was a static member function of the NeededType class. The real type of "NeededType" should ONLY be known at the top-most level, though it will be referenced through some type of typedef in some lower-level implementation. If only there were a way to pass in a type, without having to make a template out of the class that NEEDED the type... the complications would disappear. And forget about the "caller" knowing the "WorkerImpl<>" type; WorkerImpl<> must only be referenced through the base type (Worker) in this case. Anyway... this is just a "curious tale" more than a "serious problem"... unless you're really intrigued, there's no need to try and work something out with it Thanks Chad

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!