当前位置:首页 » 密码管理 » 友元函数无法访问

友元函数无法访问

发布时间: 2022-08-05 22:02:44

① 为什么友元函数无法访问类中的private成员

有类函数变量的等级决定,
public:为公开的成员和函数,谁都可以访问。
private:为私有的成员和函数,仅对象本身可以访问。

② c++友元函数不能访问私有成员是为什么

因为同样的代码在visual studio2008里能正常编译,但放到VC6.0里却提示不能访问类complex的私有数据成员。根据C++的标准特性,类的友元函数应该允许访问类的私有数据。

当在VC6.0里重载<<和>>时就会出现这种问题,奇怪的是重载别的运算符(如++)时,同样访问了类的私有数据成员,却没有报错。

③ 一个函数定义为俩个类的友元函数为什么该函数无法访问这两个类中的数据

using namespace std;//这句去掉
#include<iostream>改成#include<iostream.h>
这是VC6的BUG

④ 友元函数无法访问静态私有类成员

友元函数可以访问私有成员
可以看下以下代码:
class Test{
public:
friend void show(const Test& t){
cout << Test::num << endl;
}
private:
const static int num=10;
};

int main(int argc, char** argv) {
Test t;
show(t);
return 0;
}
我用的是dev-c++编译工具,以上代码可以通过编译,程序能正常运行
你是不是用C++ primer plus (第六版)?

⑤ VS2013下友元函数不能访问类的私有成员

这不是友元函数不能访问私有成员,这是类对象不能访问私有成员。

⑥ C++友元函数问题,类的成员函数无法访问私有成员

改成下面的代码 就能执行了。。vc6编译通过。。

#include<iostream>
usingnamespacestd;
classX;//这里要前导声明
classY
{
public:
boolifelse(X&temp);
};

classX
{
public:
friendboolY::ifelse(X&);
private:
inta;
intb;
};

boolY::ifelse(X&temp)
{
returntemp.a>temp.b?true:false;
}

intmain()
{
Xx;
Yy;
if(y.ifelse(x))cout<<"itistrue"<<endl;
elsecout<<"itisfalse!"<<endl;
return0;
}

⑦ c++中,为什么友元函数访问不了private成员

A,B类的定义顺序需要交换一下,另外,A类中的
friend void display(A &t);

需要改为
friend void B::display(A &t);

整个代码改正这样试试:

#include<iostream>
using namespace std;
class A;
class B
{
private:
int a;
int b;
public:
B();
void display(A &t);
};

class A
{
private:
int a;
int b;
public:
friend void B::display(A &t);
A()
{
a = 5;
b = 8;
}
};

B::B()
{
a = 1;
b = 2;
}
void B::display(A &t)
{
cout << t.a << endl << t.b << endl;
cout << a << endl << b << endl;
}
int main()
{
class A a1;
class B b1;
b1.display(a1);
system("PAUSE");
return 0;
}

热点内容
交通银行怎么登陆不了密码 发布:2024-05-17 13:54:48 浏览:543
安卓如何自动连接无线 发布:2024-05-17 13:53:51 浏览:262
python的urlparse 发布:2024-05-17 13:44:20 浏览:769
linux命令全称 发布:2024-05-17 12:07:54 浏览:110
ftpnas区别 发布:2024-05-17 12:06:18 浏览:949
512g存储芯片价格 发布:2024-05-17 12:04:48 浏览:963
脚本运行周期 发布:2024-05-17 11:39:09 浏览:809
阿里云服务器怎么配置发信功能 发布:2024-05-17 11:37:24 浏览:313
编程中的变量 发布:2024-05-17 11:33:06 浏览:777
加密视频怎么解密 发布:2024-05-17 11:02:52 浏览:572