I'm not sure if I asked the question correctly, but let me explain.
First, I read this article that explains the difference between declarations and definitions:
http://www.cprogramming.com/declare_vs_define.html
Second, I know from previous research that it is bad practice to define variables and functions in a header file, because during the linking phase you might have multiple definitions for the same name which will throw an error.
However, how come this doesn't happen for classes? According to another SO answer (
What is the difference between a definition and a declaration? ), the following would be a class DEFINITION:
class MyClass {
private:
public:
};
If the above definition is in a header file. Then , presumably, you can have multiple .cpp files that #include that header. This means the class is defined multiple times after compilation in multiple .o files, but doesn't seem to cause much problems...
On the other hand, if it was a function being defined in the header file, it would cause problems apparently...from what I understand... maybe?
So what's so special about class definitions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…