【发布时间】:2011-06-03 16:23:30
【问题描述】:
所以前几天,我翻阅了一些旧的 C++ 书籍,发现了一种创建 C++ 类的方法,这是我以前从未见过的。到目前为止,我所看到的所有内容都始终使用#include“header.h”并单独编译实现文件。我看到这本书的作者所做的实际上是在头文件的末尾为实现添加了一个包含指令,并从编译中省略了 .cpp 文件。有人用过这种风格吗?
例如: 我有 主文件 雇员.h 员工.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
我通常会像这样编译这个项目:
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++ 程序员。
【问题讨论】:
-
是的,我也想避开那本书。
-
@Neil “C++ 类和数据结构”作者 Jeffrey S. Childs ISBN 0-13-158051-5 978-0-13-1558051-0。其实没那么老,这本书有2008年的版权
-
@Matt,这不会是一个问题,因为有包括警卫,但这肯定不是推荐的方法。
-
@Beta 我会说它更强一些。从那本书中逃跑!!!看来这本书在amazon 的评分略高于 2 星,主要是因为糟糕的例子。
-
+1 告诉我要避免哪本书。 :)
标签: c++ coding-style include