1、在C++创建类的过程中,可以将类的原型定义在.cppm的module文件中,类的初始化也可以在.cppm文件中,但类本身的实现,即使如类本身也要在.cpp中来实现,包括类的method。
2、在C++的.cppm中,如果没有类的定义,只有struct 结构的定义,也可以没有相应的类.cpp文件,如employee.cppm如下:
export module employee;
export struct Employee {
char firstInitial;
char lastInitial;
int employeeNumber;
int salary{ 75'000};
};
此时,便不需要有Employee.cpp文件,直接在主文件中使用即可,如下:
import employee;
int main()
{
Employee anEmployee{ 'J','D',42,80'000 };
Employee anEmployee1{
.firstInitial = 'J',
.lastInitial = 'D',
.employeeNumber = 42,
.salary = 80'000
};
return 0;
}