整理日: 2015年03月18日

在 C++ 中,操作符(运算符)可以被重载以改写其实际操作。同时我们可以定义一个函数为类的朋友函数(friend function)以便使得这个函数能够访问类的私有成员,这个定义通常在头文件中完成。

在Visual C++中定义一般的函数为朋友函数通常是没有问题的。然而对某些重载操作符的函数,即使我们将它们定义为类的朋友函数,VC的编译器仍然会显示出错信息,认为这些朋友函数无权访问类的私有成员。我认为这应该是VC6.0的bug。

以下代码就是个例子:

// 头文件 "Sample.h"
#include<iostream>
using namespace std; class Sample {
public:
Sample();
friend ostream &operator<<(ostream &out, const Sample s);
friend istream &operator>>(istream &in, Sample & s); private:
int x;
}; // 实现文件 "Sample.cpp"
#include "Sample.h" Sample::Sample() {
x=0;
} istream &operator>>(istream &in, Sample & s) {
cout<<"Please enter a value"<<endl;
in >> s.x ;
return in;
} ostream &operator<<(ostream &out, const Sample s) {
cout << s.x << endl;
return out;
}

以上代码在gnuc++中编译运行毫无问题。但是在VC++6.0中编译的时候就会出现以下的编译错误:

Compiling…
Sample.cpp
c:\temp\sample.cpp(8) : error C2248: ‘x’ : cannot access private member declared in class ‘Sample’
c:\temp\sample.h(19) : see declaration of ‘x’
c:\temp\sample.cpp(13) : error C2248: ‘x’ : cannot access private member declared in class ‘Sample’
c:\temp\sample.h(19) : see declaration of ‘x’
Error executing cl.exe.Sample.obj – 2 error(s), 0 warning(s)

在VC++ 6.0中解决这个问题有以下几种方法:

在头文件中实现作为朋友函数的操作符函数的重载,也就是说在实现文件”Sample.cpp”中将函数重载的实现去掉,而将头文件修改如下:

// 修改后的头文件 1 "Sample.h"
#include<iostream>
using namespace std; class Sample {
public:
Sample();
friend ostream &operator<<(ostream &out, const Sample s);
friend ostream &operator<<(ostream &out, const Sample s) {
cout << s.x << endl;
return out;
} friend istream &operator>>(istream &in, Sample & s);
friend istream &operator>>(istream &in, Sample & s) {
cout<<"Please enter a value"<<endl;
in >> s.x ;
return in;
}
private:
int x;
};

在头文件中类定义之前将类和朋友操作符函数的原型特别声明一下,也就是将头文件修改如下(实现文件”Sample.cpp”不用作任何修改):

// 修改后的头文件 2 "Sample.h"
#include<iostream>
using namespace std; // 以下3行代码为新加入
class Sample;
ostream &operator<<(ostream &out, const Sample s);
istream &operator>>(istream &in, Sample & s); class Sample {
public:
Sample();
friend ostream &operator<<(ostream &out, const Sample s);
friend istream &operator>>(istream &in, Sample & s); private:
int x;
};

第三种方法是对I/O名空间的使用实行明确声明,也就是说在头文件”Sample.h”中直接写:

#include<iostream>
using std::ostream;
using std::istream
….

取代 “using namespace std;”

注意:在这个例子里我们在实现文件 “Sample.cpp”中包含 “using namespace std;”这句话,否则在实现中就不能使用 “cout” , “cin”, “<< “, “>>” 和 endl 这些关键字和符号。修改后的完整代码如下:

// Sample.h
#include<iostream> using std::istream;
using std::ostream; class Sample {
public:
Sample();
friend ostream &operator<<(ostream &out, const Sample s);
/*friend ostream &operator<<(ostream &out, const Sample s) {
cout << s.x << endl;
return out;
}*/
friend istream &operator>>(istream &in, Sample & s);
/*friend istream &operator>>(istream &in, Sample & s) {
cout<<"Please enter a value"<<endl;
in >> s.x ;
return in;
}*/
private:
int x;
};
// "Sample.cpp"
#include "Sample.h"
using namespace std; Sample::Sample() {
x=5;
}
istream &operator>>(istream &in, Sample & s) {
cout<<"Please enter a value"<<endl;
in >> s.x ;
return in;
} ostream &operator<<(ostream &out, const Sample s) {
cout << s.x << endl;
return out;
}

VC6.0中重载操作符函数无法访问类的私有成员的更多相关文章

  1. C#箴言之用属性来访问类的私有成员

    在程序中,难免要访问某个对象的私有成员.那么以前实现这类功能的方法有两种,第一种方法最简单,就是把成员访问符从“private”改为“public”即可:而另一个就是提供公有的成员访问函数来进行访问. ...

  2. Delphi 跨单元进入(访问)类的私有成员,protected ,private部分

    http://blog.sina.com.cn/s/blog_5f8861b60102v1nl.html Delphi 跨单元进入(访问)类的私有成员,protected ,private部分 (20 ...

  3. 使用C#反射机制访问类的私有成员【转】

    首先我必须承认访问一个类的私有成员不是什么好做法.大家也都知道私有成员在外部是不能被访问的.而一个类中会存在很多私有成员:如私有字段.私有属性.私有方法.对于私有成员访问,可以套用下面这种非常好的方式 ...

  4. VC6.0中友元函数无法访问类私有成员的解决办法

    举个例子: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #inclu ...

  5. [置顶] c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date'

    c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date' ...

  6. 《从零开始学Swift》学习笔记(Day 7)——Swift 2.0中的print函数几种重载形式

    原创文章,欢迎转载.转载请注明:关东升的博客 Swift 2.0中的print函数有4种重载形式: l   print(_:).输出变量或常量到控制台,并且换行. l   print(_:_:).输出 ...

  7. VC6.0中MFC界面换肤简例

    利用VC中的MFC进行界面设计时,发现界面上的各控件无法简易地进行调整,比如字体大小.颜色.格式等. 为了改变外观,小小地美化一下,今天决定动手一试. 网上提供的库和方法不计其数,我选择了SkinMa ...

  8. [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

  9. 在VC6.0中能不能使用Duilib界面库呢?

    Duilib库的源代码是在vs2010下编译的,一般适用于vs2008及以上的版本开发使用,那么duilib能不能在vc6.0的工程中使用呢?如何在vc6.0中使用duilib库呢? 今天,由于工作要 ...

随机推荐

  1. c#常用工具类:文件和二进制转换

    //================二进制相关转换类============== #region 将文件转换为二进制数组 /// <summary> /// 将文件转换为二进制数组 /// ...

  2. 《Linux内核设计与实现》学习笔记之“Linux进程管理机制”

    一.进程(或者称为“任务”)简介 进程是OS最基本的抽象之一,通常进程包括“可执行程序代码”,“其他资源”(如:打开的文件,挂起的信号,内核内部数据,处理器状态,地址空间,一个或多个执行线程等) 二. ...

  3. delegate和event

    经过查阅资料和自己的理解整理出来的,欢迎大家指教. delegate和event 何时使用: 异步的时候,比如加载完成通知. 需要回调的时候,比如按钮点击.动画播放结束等. 发送事件通知的时候. 比如 ...

  4. verview of Spring Framework--转

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview.html 2. Introduct ...

  5. 在后台CS文件里面,隐藏和显示Repeater里面控件

    <asp:Repeater ID="Repeater1" runat="server"><ItemTemplate><asp:Pa ...

  6. javascript的面向对象编程

    面象对象编程技术的核心理念:封装.继承.多态:在一些主流的高级编程语言中,比如:C#,VB.NET,JAVA,PHP等都是很容易实现的,而如果要在javascript中实现面象对象编程,可就不那么直接 ...

  7. SqlServer 三级联动、递归表

    SqlServer 省市县三级联动 三张表递归合并成一张表sql如下: insert into table2(area_name,area_parent_id) select province,'0' ...

  8. Android 设计随便说说之简单实践(合理组合)

    上一篇(Android 设计随便说说之简单实践(模块划分))例举了应用商店设计来说明怎么做模块划分.模块划分主要依赖于第一是业务需求,具体是怎么样的业务.应用商店则包括两个业务,就是向用户展示appl ...

  9. 百度,人人,新浪,腾讯等分享Js代码

    <!-- Baidu Button BEGIN -->                        <div id="bdshare" class=" ...

  10. Sql Xml

    /* sql xml 入门: --by jinjazz --http://blog.csdn.net/jinjazz 1.xml: 能认识元素.属性和值 2.xpath: 寻址语言,类似windows ...