What happens if I implement a class in a header file?

Possible duplicate:
Built-in functions in C ++

What does the compiler do if I fully implement the class in its header file? The following is a typical example:

class MyException
{    
public:
    explicit MyException(const char* file, int line) file(file), line(line) {};
    const char* getFile() const { return file };
    int getLine() const { return line };
private:
    const char* const file;
    const int line;
};

I intend to use the class as follows: throw MyException(__FILE__, __LINE__).

I include this header file in every .cpp file. I believe that the compiler will compile the class as many times as it is defined and include (identical) machine code in every object file it creates. Now, what will the linker do? I tried a simpler example (without all of these pesky const) and it compiled fine.

, C ? , .h .cpp?

+5
3

. , , , .

- , . - , inline.

:

(1) RETURN , CALL ( ) .

(2) .

inline . , inline ( ). , . , .

, , inline. :

http://gcc.gnu.org/onlinedocs/gcc/Inline.html

+3

. , . , , , , , -. ( , ), , .cpp, .

, main() CPP, .

+3

- , inline.

?

++ 03 ยง7.1.3/4:

  • , .
  • , ( w.r.t ) .

, inline. .

mainstream , , inline # 1 , , , inline.

.h .cpp ?

Yes, this is the usual compilation model used by most projects, in which you extract the interface (.h) from the implementation (.cpp). Interfaces are shared with users of your code in the form of header files, while the implementation is provided in the form of binary files. To some extent, this protects your intellectual property.
This is called the Separation Model .

C ++ projects that use templates typically use an Inclusion Model rather than a Separation Model for regular C ++ projects.

+3
source

All Articles