所以有一天,我正在浏览一些旧的C++书籍,并注意到创建一个我从未见过的C++类的方法。到目前为止,我所见过的所有东西都使用#include“header.h”,并分别编译实现文件。我看到这本书的作者所做的实际上是在头文件末尾的实现中加入一个包含指令,并从编译中省略.cpp文件。有人使用过这种风格吗?C++ #include风格差异
例如: 我 的main.cpp employee.h employee.cpp
//main.cpp
#include <iostream>
#include <stdio.h>
#include <string>
#include "employee.h"
void main()
{/*some code*/}
//employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
class employee
{
public:
//public members
private:
//private members
}
#endif
//employee.cpp
#include "employee.h"
#include <string>
//public member definitions
我会normaly编译这个项目像这样:
g++ main.cpp employee.cpp
但在笔者的例子是这样的
//main.cpp
#include <iostream>
#include <stdio.h>
#include <string>
#include "employee.h"
void main()
{/*some code*/}
//employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
class employee
{
public:
//public members
private:
//private members
}
#include "employee.cpp" // <-- the line of code that caught my attention
#endif
//employee.cpp
#include <string>
//public member definitions
而产生的代码编译为
g++ main.cpp
这仅仅是一种风格偏好还是有这个任何真正的实惠?我认为它不会很好地扩展,但我不是一个超级熟练的C++程序员。
这是哪本书? – 2011-06-03 16:24:45
是的,我也想避免那本书。 – Beta 2011-06-03 16:27:14
@Neil“C++类和数据结构”Jeffrey S. Childs ISBN 0-13-158051-5 978-0-13-1558051-0。其实它并没有那么古老,这本书拥有2008 – 2011-06-03 16:27:57