An exception to the "only one implementation" rule ?
Posted
by ereOn
on Stack Overflow
See other posts from Stack Overflow
or by ereOn
Published on 2010-06-17T10:42:42Z
Indexed on
2010/06/17
10:53 UTC
Read the original article
Hit count: 215
While I was reading the accepted answer of this question, I had the following question:
Typically, methods are defined in header files (.hpp
or whatever), and implementation in source files (.cpp
or whatever).
One of the main reasons it is bad practice to ever include a "source file" (#include <source_file.cpp>
) is that its methods implementation would then be duplicated, resulting in linking errors.
When one writes:
#ifndef BRITNEYSPEARS_HPP
#define BRITNEYSPEARS_HPP
class BritneySpears
{
public:
BritneySpears() {}; // Here the constructor has implementation.
};
#endif /* BRITNEYSPEARS_HPP */
He is giving the implementation of the constructor (here an "empty" implementation, but still).
But why then including this header file multiple times (aka. on different source files) will not generate a "duplicate definition" error at link time ?
© Stack Overflow or respective owner