Avoiding friends
-
Hi
I have a class for which I want to restrict access to its data.
The restriction is that only 2 other classes should be able to access the data.
In an ideal world I would like to only allow these 2 classes to create objects of the original class.
I have manged to restrict the data access by using friends but was wondering if there is a way to do this avoiding the use of friendsMy code so far:
@
class C1
{
public:
C1(int x, int y)
: m_x(x),m_y(y)
{}
friend class C2;
friend class C3;
private:
int m_x;
int m_y;
};class C2
{
public:
C2()
: m_C1(new C1(1,2))
{}
void setData(int x,int y)
{
m_C1->m_x = x;
m_C1->m_y = y;
}
private:
C1* m_C1;
};
class C3
{
public:
C3()
: m_C1(new C1(1,2))
{}
void setData(int x,int y)
{
m_C1->m_x = x;
m_C1->m_y = y;
}
private:
C1* m_C1;
};
@