Home  >  Q&A  >  body text

c++头文件问题

若设计一个结构体三个类,和一堆宏定义以及一堆常量以及一堆全局函数:


#define ABC 1
#define DEF 2

int const MIN;

int funa(...);
int funb(...);

struct a;

class x;
class y;
class z;

其中,宏定义、常量、常量函数放在common.h中;
class x拥有一个成员变量是class y的实例;
class y拥有一个成员变量是class x的实例;
class z的大部分成员方法都会用到class xclass y

请问,如果要分成若干个.h文件和若干个.cpp文件,该如何写?

黄舟黄舟2765 days ago822

reply all(6)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 13:01:37

    class x has an instance whose member variable is class y;
    class y has an instance whose member variable is class x;

    This. . Unless you use pointers, it’s impossible


    The pointer is as follows:

    common.h

    #ifndef COMMON_H
    #define COMMON_H
    
    //宏定义
    #define ABC 1
    #define DEF 2
    
    //结构定义
    struct structA
    {
        int a;
        int b;
        int c;
    };
    
    
    //常量声明
    extern const int MIN;
    const int MAX = 2;
    
    //全局函数声明
    extern const int funa();
    extern int funb();
    
    
    #endif

    common.cpp

    //全局函数实现
    const int funa()
    {
        return 0;
    }
    int funb()
    {
        return 1;
    }
    
    //常量初始化
    const int MIN = funa();

    classx.h

    #include "common.h"
    
    class classy;
    
    class classx{
        public:
            //classx成员声明
            int func();
            classy* _py;
    };

    classx.cpp

    //classx成员函数实现
    int classx::func()
    {
    }

    classy.h

    #include "common.h"
    
    class classx;
    
    class classy{
        public:
            //classy成员声明
            int func();
            classx* px;
    };
    

    classy.cpp

    //classy成员函数实现
    int classy::func()
    {
    }

    classz.h

    #include "classx.h"
    #include "classy.h"
    
    class classz{
        public:
            //classz成员声明
            classz();
            ~classz();
            classx* getX();
            classy* getY();
        private:
            classx* _x;
            classy* _y;
    };

    classz.cpp

    #include "classz.h" 
    
    //classz成员函数实现 
    classz::classz()
    {
        _x = new classx();
        _y = new classy();
    }
             
    classz::~classz()
    {
        delete _x;
        delete _y;
    }
    
    classx* classz::getX()
    {
        return _x;
    }
    classy* classz::getY()
    {
        return _y;
    }

    main.cpp

    #include <cstdio>
    #include "classz.h"
    
    int main()
    {
        classz instancez;
        printf("%p %p", instancez.getX(), instancez.getY() );
        return 0;
    }

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:01:37

    common.h

    #define ABC 1
    #define DEF 2
    
    static int const MIN;
    
    static int funa(...);
    static int funb(...);
    
    static struct a;
    

    classx.h

    #include "common.h"
    class classy;
    
    class x{
    };
    

    classy.h

    #include "common.h"
    class classx;
    
    class y{
    };
    

    classz.h

    #include "classx.h"
    #include "classy.h"
    
    class classz{
    };

    reply
    0
  • PHPz

    PHPz2017-04-17 13:01:37

    Owner, are you afraid of circular references and causing errors? If this is the case, it is recommended that you take a look at C++ class forward declaration, which is enough to solve your problem.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:01:37

    It’s C++ anyway, so all constants should be able to be turned into constexpr.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:01:37

    This cannot be solved by forward declaration. The compiler needs to know the complete definition of class y when compiling class x. To know the complete definition of class y, it must first know the definition of class x. The compiler will tell you "I can't figure it out". If you have to do this, you can use a pointer: class x has a pointer to an instance of class y. The compiler knows what the pointer is, and the same applies to class y.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:01:37

    This is to use forward declaration, but only pointers or references can be used, because the compiler must clarify the size of the members when compiling, and the sizes of pointers and references are determined.

    It’s like designing a house and a bed. Where can I buy a bed before the house is designed? The size is unknown, but pointers can be used.

    reply
    0
  • Cancelreply