所以有一天,我正在瀏覽一些舊的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