zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Dllimport函数時无法在Dll中找到的入口点
    cb35a_c++_STL_算法_for_each
    cb34a_c++_STL_算法_查找算法_(7)_lower_bound
    cb33a_c++_STL_算法_查找算法_(6)binary_search_includes
    cb32a_c++_STL_算法_查找算法_(5)adjacent_find
    cb31a_c++_STL_算法_查找算法_(4)find_first_of
    cb30a_c++_STL_算法_查找算法_(3)search_find_end
    cb29a_c++_STL_算法_查找算法_(2)search_n
    cb28a_c++_STL_算法_查找算法_(1)find_find_if
    cb27a_c++_STL_算法_最小值和最大值
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3903096.html
Copyright © 2011-2022 走看看