c++ primer,友元函数上的一个例子(By Sybase)

本文试图解释c++ primer Screen 和 Window_Mgr的例子,为什么将两个类放在两个文件中无法编译?

将两个类写在同一个文件中,通过三个例子解释问题:

第一种写法问题:

编译到Screen时,由于Screen类使用到Window_Mgr的成员函数,虽然前面给出了Window_Mgr的声明,但此时还清楚Window_Mgr的完整定义,所以编译出错。

class Window_Mgr

class Screen

{

  public:

    friend Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r, Window_Mgr::index c, Screen& s);

  private:

    int height;

    int width;

}

class Window_Mgr

{

public:

  typedef std::string::size_type index;

  Window_Mgr& Window_Mgr::relocate(index r, index c, Screen& s)

  {

    s.height += r;

    s.width += c;

    return *this;

  }

}

 

第二种写法问题在于:

编译到relocate时,由于Screen& s的实现使用到Screen的成员变量,虽然前面给出了Screen的声明,但此时还清楚Screen的完整定义,所以编译出错。

 

class Screen;

class Window_Mgr

{

public:

  typedef std::string::size_type index;

  Window_Mgr& Window_Mgr::relocate(index r, index c, Screen& s)

  {

    s.height += r;

    s.width += c;

    return *this;

  }

}

class Screen

{

  public:

    friend Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r, Window_Mgr::index c, Screen& s);

  private:

    int height;

    int width;

}

第三种写法:

将Window_Mgr::relocate的实现移动到最后,由于编译类Window_Mgr时,并不需要Screen&s 的实现细节,问题得到解决

class Screen;

class Window_Mgr

{

public:

  typedef std::string::size_type index;

  Window_Mgr& Window_Mgr::relocate(index r, index c, Screen& s);

}

class Screen

{

  public:

    friend Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r, Window_Mgr::index c, Screen& s);

  private:

    int height;

    int width;

}

 

 

  Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r, Window_Mgr::index c, Screen& s)

  {

    s.height += r;

    s.width += c;

    return *this;

  }

 

可见,这两个类如果编译成功需要严格的交替顺序

这也就解释了为什么放在两个文件中无法编译。

 

附录:

一开始的实现的不能编译的两个文件

实现分别如下:Window_Mgr.h

#ifndef WINDOW_MGR //为了避免两个文件嵌套

#define WINDOW_MGR

#include <string>

#include <Screen.h>

class Window_Mgr

{

public:

  typedef std::string::size_type index;

  Window_Mgr& Window_Mgr::relocate(index r, index c, Screen& s)

  {

    s.height += r;

    s.width += c;

    return *this;

  }

}

#endif

Screen.h

#ifndef SCREEN

#define SCREEN

#include "Window_Mgr.h"

class Screen

{

  public:

    friend Window_Mgr& Window_Mgr::relocate(Window_Mgr::index r, Window_Mgr::index c, Screen& s);

  private:

    int height;

    int width;

}

#endif

 

时间: 2024-09-20 05:54:51

c++ primer,友元函数上的一个例子(By Sybase)的相关文章

关于msdn上的一个例子

问题描述 例子如下:usingSystem;usingSystem.Collections;publicclassSamplesArrayList{publicstaticvoidMain(){//CreatesandinitializesanewArrayList.ArrayListmyAL=newArrayList();myAL.Add("Hello");myAL.Add("World");myAL.Add("!");//Displaysth

C++写的一个简单类模版 友元函数求最大最小值

问题描述 C++写的一个简单类模版 友元函数求最大最小值 如题 , 编译时总是说 [Error] ld returned 1 exit status (编译器dev c++) using namespace std; template <typename t> class CValue { t data[5]; public: CValue(); friend t Max(CValue <t> a); friend t Min(CValue <t> a); }; tem

java ftp 上传一个文件目录的例子

java ftp 上传一个文件目录 package yq1012; import java.io.File;  import java.io.FileInputStream;  import org.apache.commons.net.ftp.FTPClient;  import org.apache.commons.net.ftp.FTPReply;    public class test {             private  FTPClient ftp;        /**  

C++之:友元函数

一.定义 友元函数是可以直接访问类的私有成员的非成员函数.它是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend,其格式如下: friend 类型 函数名(形式参数); 友元提供了不同类的成员函数之间.类的成员函数与一般函数之间进行数据共享的机制.通过友元,一个不同函数或另一个类中的成员函数可以访问类中的私有成员和保护成员.c++中的友元为封装隐藏这堵不透明的墙开了一个小孔,外界可以通过这个小孔窥视内部的秘密. 友元的正确使用能提高程

C++第7周(春)项目3 成员函数、友元函数和一般函数有区别

课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目3-成员函数.友元函数和一般函数有区别] 阅读下面的程序,仔细阅读注释.然后模仿完成求点类中距离的任务. //例:使用成员函数.友元函数和一般函数的区别 #include <iostream> using namespace std; class Time { public: Time(int h,int m,int s):hour(h

C++参考——成员函数、友元函数和一般函数有区别

[项目-成员函数.友元函数和一般函数有区别](1)阅读下面的程序,体会注释中的说明. //例:使用成员函数.友元函数和一般函数的区别 #include <iostream> using namespace std; class Time { public: Time(int h,int m,int s):hour(h),minute(m),sec(s) {} void display1(); //display1是成员函数 friend void display2(Time &); /

C++第7周项目2 - 成员函数、友元函数和一般函数之区别

课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/8775137 [项目2-成员函数.友元函数和一般函数之区别]阅读程序,仔细阅读注释.然后模仿完成求点类中距离的任务.  你需要完成的任务是,利用成员函数.友元函数和一般函数,实现三个版本的求两点间距离的函数,并设计main()函数完成测试.此项目和例子的区别在于

一个例子

一个例子这一章,我们要把我们已学的知识集合起来.具体来讲,我们来写一个使用ODBC APIs的程序.为简单起见,这个程序中我使用Microsoft的Access数据库(Microsoft Access 97) . 下载例子源程序. 注意:如果你使用的windows.inc 是1.18及其以下版本,在开始编译之前要修改其中的一个小bug.在windows.inc中查找 "SQL_NULL_HANDLE",将得到下面这行: SQL_NULL_HANDLE equ 0L 将0后面的"

php文件上传的例子及参数详解

 这篇文章主要介绍了php文件上传的例子及参数,有需要的朋友可以参考一下 1.上传表单 upload.html   程序代码 HTML     代码如下: <form enctype="multipart/form-data" action="upload.php" method="post">   <input type="hidden" name="max_file_size" val