Sign in to follow this  

myclassT members private to myclassU

Recommended Posts

template <class T>
class myclass {
  myclass(int i) : m(i) {}
  template <class U> myclass(const myclass<U>& rhs);
  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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this