Jump to content
  • Advertisement
Sign in to follow this  
webwraith

Derivation Confusion

This topic is 3779 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

I'm working on templatised Observer/Subject classes, and seem to have run into a little problem. My code includes;
template < class Type >
class DesignPattern::Observer
{
        ....
};

template < class Type >
class DesignPattern::Subject
{
        ....

	protected:
                ....
		typedef std::vector<DesignPattern::Observer<Type>*>::iterator list_iter;
};

When I come to compile it, I get the following error;
error: type `std::vector<DesignPattern::Observer<Type>*, std::allocator<DesignPattern::Observer<Type>*> >' is not derived from type `DesignPattern::Subject<Type>'
I'm not sure why the compiler seems to think the iterator needs to be derived from the Subject class, but I'm guessing it's something I'm just being a bit too dense to see. Can someone put me right? Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
iterator is not a dependant type of Subject. Throw in the typename keyword after the typedef and watch and see.

Share this post


Link to post
Share on other sites
OK, so that works, and I thank you for it, but exactly how is that working? I was under the impression that typename was only used in template parameters, and was, even then, interchangeable with class (which is what I use in that case, anyway...)

Share this post


Link to post
Share on other sites

template <typename T>
class General {
typedef int iterator;
};

// The language allows us to do this:
template <>
class General<float> {
std::string iterator; // notice it's not even a typedef any more
};


And that causes a problem when you want to use 'iterator' as a typedef inside another templated class. The language requires the 'typename' keyword to help the compiler figure out what's going on.

See also google results.

Share this post


Link to post
Share on other sites
'Typename' specifies that the lexeme that follows is a type name, and not a member or static name. For instance, if you were to enter the following in your source...

class MyContainer<A, B, C>
{
std::vector<MyPainfullyLongTypename<A, B, C> > data;
typedef std::vector<MyPainfullyLongTypename<A, B, C> >::iterator d_iter;
}

In the typedef, C++ sometimes has know way of knowing whether std::vector<T>::iterator is the name of a typedef within std::vector<T>, or a static member of std::vector<T>. So, by inserting the typename keyword between typedef and std::vector, you tell it to assume it is a type name.

Share this post


Link to post
Share on other sites
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!