简体   繁体   English

为包含两个类的头文件构建C ++文件

[英]Building C++ Files for a header file containing two classes

Object_t.h Object_t.h

class node_t;
typedef node_t * node_tptr;
class list_t;
typedef list_t * list_tptr;

typedef void * TYPE;   //void * means we can point to anything with a typecast

class node_t
{
   public:
      node_t(void);                      //default constructor
      node_t(TYPE newentity);   //constructor
      void set_next(node_tptr );
      node_tptr get_next(void);
      TYPE get_entity(void);
   private:
      node_tptr next;     //next link in the list
      TYPE entity;         //the entity
};

class list_t
{        
   public:
      list_t(void);                   //default constructor
      list_t(const list_tptr);    //copy constructor
      void insert(TYPE newentity);
      TYPE start(void);
      TYPE get_next(void);
   private:
      node_tptr head;
      node_tptr tail;
      node_tptr current;
};

The header file above contains the definitions for two different classes. 上面的头文件包含两个不同类的定义。 I assume the next step would be to create a C++ file called object_t.cpp, but how would that work when there are two classes defined within the header file? 我假设下一步是创建一个名为object_t.cpp的C ++文件,但是如果在头文件中定义了两个类,那将如何工作?

Unlike as in Java, a C++ file can contain more than one class implementation. 与Java不同,C ++文件可以包含多个类实现。 Just implement both ;-) 只需实现两个;-)

It works because in the implementation file you define your functions using the scope resolution operator, eg 之所以起作用,是因为您在实现文件中使用范围解析运算符定义了函数,例如

TYPE list_t::get_next(void) {}
nodet_ptr node_t::get_next(void) {}

which allows the compiler to correctly match up the declarations with the definitions. 这使编译器可以正确地将声明与定义匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM