.h和.cpp以及namespace

1.当你要写一个类的时候,将类的声明 和头文件 define 的声明放在.h里。将类的实现放在.cpp,只需要囊括相应的.h文件就行。
但是.h和cpp都应囊括到一个namespace中。
https://github.com/cartographer-project/cartographer/tree/master/cartographer/common
common里的cpp 和 h文件对应的都是一个类

cpp文件编译成库是可以加密的,外在用户直接调用h文件就可以,所以适合写商业库。
hpp文件因为实现和定义都在一起,不会加密,所以适合写开源库。
2.当你只需要实现一个函数而不用写一个类时,将函数实现写在.hpp中,也要在namespace中。然后写一个.h文件囊括这个.hpp文件就行。
https://github.com/cartographer-project/cartographer/tree/master/cartographer/common
port.h 和 lua.h 都时单独实现函数

3.一定要写#ifndef #define #endif 这样的预编译头。 防止重复编译。

4.typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud PointCloud;
取别名的时候一定要在哪个.cpp中声明,就在哪个cpp中用别名
否则容易爆“error C2100: 非法的间接寻址”

猜你喜欢

转载自blog.csdn.net/guanxunmeng8928/article/details/112984560