Suppose we have B.h
, main.cpp
and B.cpp
, main.cpp
-> main.obj
and B.cpp
-> B.obj
Because of separate compilation
, I think main.obj
and B.obj
should both have the definition of class B
. Why it doesn't have the error:multiple definition of B
when linking them?
B.h
#ifndef B_H
#define B_H
class B {
public:
void fun();
private:
int x = 0;
};
#endif
B.cpp
#include "B.h"
void B::fun() {
}
main.cpp
#include "B.h"
int main() {
}