现定义一个类Person:
#include <string>
class Person {
private:
int age;
std::string name;
public:
int get_age(){return age;}
std::string get_name();
};
然后在另一个cpp文件中,如下这样声明一个函数而它用到class Person 时,你并不需要该 class 的定义式,纵使函数以 by value 方式传递该类型的参数(或返回值)亦然:
class Person; // class 声明式
Person get_person();
void process_person(Person d);
但如果是定义一个函数,且用到了class Person时,不可以by value方式传递该类型的参数(或返回值):
class Person; // class 声明式
Person get_person(){}
void process_person(Person p){}
// 报错:
error: return type ‘class Person’ is incomplete
error: ‘p’ has incomplete type
但上例如果是以指针或引用方式是没问题的,因为类型对象大小就是指针的大小。