Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I implement a class in the header file? [duplicate]

Tags:

c++

header

Possible Duplicate:
Inline functions in C++

What does the compiler do if I completely implement a class in its header file? A typical example follows:

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;
};

My intention is to use the class like this: throw MyException(__FILE__, __LINE__).

I include this header file into each .cpp file. I suppose the compiler will compile the class as many times as it is defined and include the (identical) machine code into every object file it produces. Now, what will the linker do? I tried a simpler example (without all those pesky const's) and it compiled fine.

What would happen, if instead of a simple class, I implemented a three-screenful-long C function in a header file? And the final question, should I split my example into .h and .cpp files?

like image 564
peter.slizik Avatar asked Sep 04 '12 09:09

peter.slizik


People also ask

What happens if a header file is included twice?

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.

Do you put classes in header files?

Class definitions can be put in header files in order to facilitate reuse in multiple files or multiple projects. Traditionally, the class definition is put in a header file of the same name as the class, and the member functions defined outside of the class are put in a . cpp file of the same name as the class.

Can you have two classes in a header file?

There is no obligation regarding header and source files. In particular (contrarily to Java) you can (and often want) to have several declarations in a header file, and several class and function definitions (& implementation) in a source file.

What is header file in C What will happen if we include a header file twice in AC program?

Each header files in C contains different predefined functions to make programs simple to understand. When a header file is included two times in a C program, the second one gets ignored. In actual, the #, called the include, preceding a header file ensures that it is included only once during the compilation process.


2 Answers

All methods will be inline methods. You may loose some minimal time on the overall compilation, but it's ok. As far as I know the know the only problem that can occur is if you have a static non-cost member variable. Then you have to assign a storage place for it (place a definition and and initial value if you want) presumably in a .cpp or else you will get linker errors about multiple definition.

I've seen header-only projects which had only the main() function in a CPP, but that was heavily templated.

like image 106
Notinlist Avatar answered Sep 20 '22 19:09

Notinlist


A class definition itself doesn't produce any code. It just shows users of the class how it is layed out, so they can generate appropriate code to manipulate it.

It's the member functions of the class that generate code. When you define a member function inside the class definition it gives the function an implicit inline declaration.

A function call can be compiled and linked in one of two ways:

(1) A single copy of the function code with a RETURN assembly instruction at the end can be placed in the image, and a CALL assembly instruction can be placed (along with param passing and return value transfer) at the call site to transfer control to this code.

or

(2) An entire copy of the function implementation can replace the entire function call at the call site.

A function declared inline is a recommendation to the compiler to do it the second way. Further an inline declaration allows the function to be defined in several translation units (so it can be placed in a shared header file). In order for the compiler have the option of implementing the second method, it needs a copy of the function implementation at compile-time. This isn't available if the function implementation is in a foreign translation unit.

It should also be noted that modern compilers do complicated things with functions declared inline. See:

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

like image 22
Andrew Tomazos Avatar answered Sep 19 '22 19:09

Andrew Tomazos