Gambit King
Gambit King

Reputation: 483

Where to write the implementation of a Class

I usually create a header file for my Class and write the declarations and implementations inside them.

I've been going through codes in larger projects, and it seems they only do the declaration inside the header file and create another .cpp/.c file to code the implementation.

What are the advantages/disadvantages in doing that ?

Upvotes: 1

Views: 325

Answers (5)

Mats Petersson
Mats Petersson

Reputation: 129454

Several good "answers" already, but I would say that the a major reason for separating the declaration and the implementation is that you don't [hopefully, if you thought your design through, etc] change the declaration very often during development, but you do, quite regularly change, the implementation during the development.

The benefit then is that you only need to actually compile the implementation file of that particular class, rather than "every file that includes the interface declaration in the header".

Of course, this has to be tempered by the fact that you can't inline functions.

For tiny to small projects, it makes little difference. For larger projects, it does make a difference. For example, if I modify the "parser.cpp" for my Pascal compiler, it takes 26 seconds to build the new compiler. If I modify a header file that causes "everything" to be rebuilt, it takes 1m28s to build. This project only has 10 source files (because I use llvm for the backend part). Imagine a project with 60 source files.

Upvotes: 0

Melkon
Melkon

Reputation: 428

This is the standard pattern how you want to write your code. It's much more readable. If you want a public method to be inline (usually getters) you must write it in the header. You must definiate Template functions and methods in header also.

However in every other case you should do it in the cpp. Maybe it will give you a bit smaller binary files, because you don't include the implementation of the functions to every file you included the header, but actually it's not a big deal.

Basically a user of a class just want to see the interface of the class, and mostly not the implementations, while someone who actually work on it mostly want to see the implementations. Because of that it's a good idea to separate these parts.

It will also reduce compilation time alot, because you never include cpp files. So you can include whatever you want in the cpp, while if you include alot of things in the header, it will be included everywhere, where you include the header. (you can even get some circular dependencys if you don't use cpp files)

Upvotes: 1

Danvil
Danvil

Reputation: 23031

There are many facets to this topic. Here are some thoughts:

  • If you want a function to be used for inline optimizations, you have to put it in the header.
  • Templates are most often put directly into the header.
  • Short functions like getters/setters are often put directly in the header.
  • Functions with short math computations can be put in the header.
  • Longer more complicated functions are better of in a source file to keep the header clear and concise.
  • If you have classes with circular dependencies like struct A { B* p; ... }; struct B { A* p; ... };, member functions of A/B using the other class must be put in a source file.
  • It is good style to put most of you code in the source file.

Upvotes: 0

Programmer
Programmer

Reputation: 1330

One reason is to separate interface and implementation. The major use comes when you are developing a library and want to share that library and you only want the library users to know the interface and not the implementation.

Upvotes: 1

Over Killer
Over Killer

Reputation: 513

If You are writing implementation utside of a class (cpp file), Your code is easier to manage. But if want create short method in a class, You can write implementation in header using inline keyword.

Upvotes: 0

Related Questions