以类对象为形参的成员函数,为什么可以直接访问该形参的私有变量?

如:
class test{
public:
test()
{name = "lllll";}
void show(test & n)
{ std::cout << n.name};
private:
std::string name;
}

因为访问类对象的私有变量的成员函数所在的类与这个对象所属的类是同一类。
在一个类的成员函数中可以访问这个类的对象的所有成员(包括私有成员)。追问

也就是说其他类对象的私有成员还是要通过原来的成员函数访问。

追答

也不是。

对于一个类A的成员函数而言:
1.
可以直接访问类A、类A的子类、类A的孙类(以及所有类A的后代类)的对象的、继承自类A的所有成员,包括私有成员。
2.
可以直接访问把类A作为它的友元类的其它类的对象的所有成员,包括私有成员。
3.
可以直接访问把此成员函数作为它的友元函数的类的对象的所有成员,包括私有成员。

下面是一个例子,你可以研究下。
#include

using namespace std;

class B;
class C;
class Friend;
class Friend2;

class A
{
public:
A(string s="访问A的私有成员str"):str(s)
{

}
void a_instance_print(A,B,C,Friend,Friend2);
static void a_static_print(A,B,C,Friend,Friend2);
private:
string str;
};
class B : public A
{
public:
B(string str="访问B继承自A的私有成员str"):A(str)
{

}
};
class C : public B
{
public:
C():B("访问C继承自A的私有成员str")
{

}
};
class Friend
{
private:
Friend():str("访问类A的友元类Friend的私有成员str")
{

}
// 将类A声明为类Friend的友元类
friend class A;
string str;
};
class Friend2
{
private:
Friend2()
{
str="访问类Friend2的私有成员str";
}
// 将以下两个类A的成员函数声明为类Friend2的友元函数
friend void A::a_instance_print(A,B,C,Friend,Friend2);
friend void A::a_static_print(A,B,C,Friend,Friend2);
string str;
};
void A::a_instance_print(A a=A(),B b=B(),C c=C(),Friend f=Friend(),Friend2 f2=Friend2())
{
cout<<"从类A的实例方法(此实例方法是类Friend2的友元函数):"<<endl;
cout<<a.str<<endl;
cout<<b.str<<endl;
cout<<c.str<<endl;
// cout<<b.A::str<<endl;
// cout<<c.A::str<<endl;
cout<<f.str<<endl;
cout<<f2.str<<endl;
}
void A::a_static_print(A a=A(),B b=B(),C c=C(),Friend f=Friend(),Friend2 f2=Friend2())
{
cout<<"从类A的静态方法(此静态方法是类Friend2的友元函数):"<<endl;
cout<<a.str<<endl;
cout<<b.str<<endl;
cout<<c.str<<endl;
// cout<<b.A::str<<endl;
// cout<<c.A::str<<endl;
cout<<f.str<<endl;
cout<<f2.str<<endl;
}
int main()
{
A::a_static_print();
cout<<endl<<endl;
A().a_instance_print();
return 0;
}

#include

using namespace std;

class B;
class C;
class Friend;
class Friend2;

class A
{
public:
A(string s="访问A的私有成员str"):str(s)
{

}
void a_instance_print(A,B,C,Friend,Friend2);
static void a_static_print(A,B,C,Friend,Friend2);
private:
string str;
};
class B : public A
{
public:
B(string str="访问B继承自A的私有成员str"):A(str)
{

}
};
class C : public B
{
public:
C():B("访问C继承自A的私有成员str")
{

}
};
class Friend
{
private:
Friend():str("访问类A的友元类Friend的私有成员str")
{

}
// 将类A声明为类Friend的友元类
friend class A;
string str;
};
class Friend2
{
private:
Friend2()
{
str="访问类Friend2的私有成员str";
}
// 将以下两个类A的成员函数声明为类Friend2的友元函数
friend void A::a_instance_print(A,B,C,Friend,Friend2);
friend void A::a_static_print(A,B,C,Friend,Friend2);
string str;
};
void A::a_instance_print(A a=A(),B b=B(),C c=C(),Friend f=Friend(),Friend2 f2=Friend2())
{
cout<<"从类A的实例方法(此实例方法是类Friend2的友元函数):"<<endl;
cout<<a.str<<endl;
cout<<b.str<<endl;
cout<<c.str<<endl;
// cout<<b.A::str<<endl;
// cout<<c.A::str<<endl;
cout<<f.str<<endl;
cout<<f2.str<<endl;
}
void A::a_static_print(A a=A(),B b=B(),C c=C(),Friend f=Friend(),Friend2 f2=Friend2())
{
cout<<"从类A的静态方法(此静态方法是类Friend2的友元函数):"<<endl;
cout<<a.str<<endl;
cout<<b.str<<endl;
cout<<c.str<<endl;
// cout<<b.A::str<<endl;
// cout<<c.A::str<<endl;
cout<<f.str<<endl;
cout<<f2.str<<endl;
}
int main()
{
A::a_static_print();
cout<<endl<<endl;
A().a_instance_print();
return 0;
}

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网