A
Asfand Yar Qazi
Consider the following code:
=========================================
#include <iostream>
template<class ValueT, OwnerT>
class Restricted_Public_Variable
{
public:
typedef Restricted_Public_Variable Self;
private:
Restricted_Public_Variable()
: data()
{
}
virtual
~Restricted_Public_Variable()
{
}
Restricted_Public_Variable(const ValueT& arg)
: data(arg)
{
}
Restricted_Public_Variable(const Self& arg)
: data(arg.data)
{
}
const Self&
operator=(const ValueT& arg)
{
data = arg;
return *this;
}
friend OwnerT;
ValueT data;
public:
operator ValueT()
{
return data;
}
const ValueT&
get() const
{
return data;
}
};
class Big
{
public:
Big(int arg)
: myint(arg)
{
}
Restricted_Public_Variable<int, Big> myint;
};
int
main()
{
Big b(3);
std::cout << b.myint << std::endl; // 3
// The following is disallowed, we can
// only read the 'myint' value
// b.myint = 5;
}
=========================================
Yet the compiler (gcc 3.3.3) says:
template parameters cannot be friends
Bah foiled...
Can anybody explain why this is?
=========================================
#include <iostream>
template<class ValueT, OwnerT>
class Restricted_Public_Variable
{
public:
typedef Restricted_Public_Variable Self;
private:
Restricted_Public_Variable()
: data()
{
}
virtual
~Restricted_Public_Variable()
{
}
Restricted_Public_Variable(const ValueT& arg)
: data(arg)
{
}
Restricted_Public_Variable(const Self& arg)
: data(arg.data)
{
}
const Self&
operator=(const ValueT& arg)
{
data = arg;
return *this;
}
friend OwnerT;
ValueT data;
public:
operator ValueT()
{
return data;
}
const ValueT&
get() const
{
return data;
}
};
class Big
{
public:
Big(int arg)
: myint(arg)
{
}
Restricted_Public_Variable<int, Big> myint;
};
int
main()
{
Big b(3);
std::cout << b.myint << std::endl; // 3
// The following is disallowed, we can
// only read the 'myint' value
// b.myint = 5;
}
=========================================
Yet the compiler (gcc 3.3.3) says:
template parameters cannot be friends
Bah foiled...
Can anybody explain why this is?