Jump to content
  • Advertisement
Sign in to follow this  
all_names_taken

myclassT members private to myclassU

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

template <class T>
class myclass {
public:
...
  myclass(int i) : m(i) {}
  template <class U> myclass(const myclass<U>& rhs);
...
private:
  int m;
};

template <class T> template <class U>
myclass<T>::myclass(const myclass<U>& rhs) : m(rhs.m) {
}
...
int main() {
	myclass<int> x(1);
	myclass<float> y(x);
}



Gives compile error:
error: ‘int myclass<int>::m’ is private
error: within this context - line of initialization list in the 2nd myclass constructor
Adding the following doesn't help:
template <class U> friend class myclass<U>;
Is there any way to make all type instantiations of myclass become friends with each other?

Share this post


Link to post
Share on other sites
Advertisement
Please note that a template constructor can never provide a copy constructor; you will probably want to override the template explicitly for the case where T == U (even if the code is the same).

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!