ways to write code for a class
-
This subject is a foggy topic in my head could you help me clarify things a little. basically there are two ways to write a class in c++ -declare a class and add in the class body only class member variables and function declarations and then layout the code for function bodies somewhere outside (in the same cpp file or in a different cpp file if the class declaration was made in a header file) -declare a class and add in the class body class member variables and function bodies (not just the declarations), the later solution resembling how things are done in C#. The former approach can be used in the main cpp file but also in a c++ header file. The later approach can only be used in the main cpp file is that correct? could you place for instance a class declaration containing function bodies (not just declarations) in a another cpp file belonging to the same project or in a header file? Is it that the h and cpp file of the same name are compiled into a single binary file which is then used by the binary form of the main cpp file? and that the header file is there just as an index/entry point to the class functions
-
This subject is a foggy topic in my head could you help me clarify things a little. basically there are two ways to write a class in c++ -declare a class and add in the class body only class member variables and function declarations and then layout the code for function bodies somewhere outside (in the same cpp file or in a different cpp file if the class declaration was made in a header file) -declare a class and add in the class body class member variables and function bodies (not just the declarations), the later solution resembling how things are done in C#. The former approach can be used in the main cpp file but also in a c++ header file. The later approach can only be used in the main cpp file is that correct? could you place for instance a class declaration containing function bodies (not just declarations) in a another cpp file belonging to the same project or in a header file? Is it that the h and cpp file of the same name are compiled into a single binary file which is then used by the binary form of the main cpp file? and that the header file is there just as an index/entry point to the class functions
Let's talk first about C where the C++ came from. From the compiler's point of view there is not difference between .h and .cpp files. They are both source files and it treats them identically. By convention (and only by convention) we place declarations in the .h file and code in .cpp one. Also we have to make sure that the compiler sees the code generating lines only once. For instance if you place a function definition:
int my_function () {return 3;}
in a .h file and that file is included by two different .cpp files, the compiler will happily compile them but the linker will complain that it has two instances of the same function and it doesn't know which one to pick. What we do in this case is to place a function declaration in the .h file:
int my_func ();
Now we can put the function definition in one of the .cpp files and everyone will be happy: the compiler has a declaration for the function so it can check all calls against the function prototype and the linker has only one body for the function. Now we move to C++. Assume you place this:
class A {
public:
int func1 ();
int func2 () {return 2;}
};in a header file. The member
func1
is only declared while the memberfunc2
is also defined. Here the compiler performs a little trick: because the definition of func1 is within the body of a class it tells the linker to use only one instance of the generated code. If we add a definition forfunc1
inside the same header file:class A {
public:
int func1 ();
int func2 () {return 2;}
};int A::func1 () {return 1;}
the compiler will treat it as a regular function and again, if the header is included by two different .cpp file, the linker will complain that the function is doubly defined. We can do two things now. One is to place an
inline
specifier in front of the function:class A {
public:
int func1 ();
int func2 () {return 2;}
};inline int A::func1 () {return 1;}
The compiler will use the same trick as if the function would have been defined inside the body of the class and tell linker to use only one instance. The other is to simply place the function definition in a .cpp file and in that case the linker will see it only once. So in the end you have 3 alternatives: 1. place the function definition in the class body. 2. place it in the header file and make it inline. 3. pla
-
This subject is a foggy topic in my head could you help me clarify things a little. basically there are two ways to write a class in c++ -declare a class and add in the class body only class member variables and function declarations and then layout the code for function bodies somewhere outside (in the same cpp file or in a different cpp file if the class declaration was made in a header file) -declare a class and add in the class body class member variables and function bodies (not just the declarations), the later solution resembling how things are done in C#. The former approach can be used in the main cpp file but also in a c++ header file. The later approach can only be used in the main cpp file is that correct? could you place for instance a class declaration containing function bodies (not just declarations) in a another cpp file belonging to the same project or in a header file? Is it that the h and cpp file of the same name are compiled into a single binary file which is then used by the binary form of the main cpp file? and that the header file is there just as an index/entry point to the class functions
To add to what Mircea said, function definitions (implementations) usually go in a .cpp for two reasons: 1. By default, a header is recompiled each time that a .cpp
#include
s it, so defining large functions in headers can really slow down a compile. Some compilers optimize this, but it isn't guaranteed. 2. A header should be an interface, with implementation details going in a .cpp rather than cluttering the header. However, there are two situations when function definitions go in a header: 1. If a function is simple, usually just a single line, it's expedient to just define it in the header. 2. A function template, or a function in a class template, is defined in a header. As far as where to define functions is concerned, I prefer to define each class in its own header (Class.h) and implement its functions in Class.cpp. This isn't mandatory, because the .h and .cpp are not compiled into a single binary. Each .cpp, along with all the headers that it#include
s, is compiled into its own binary, after which the linker merges them all. Some C++ gurus say there is no rationale for putting each class in its own .h and .cpp. They're wrong, but there are exceptions: 1. A "helper" class (used only to implement another one) can be defined in the same .cpp as the only class that uses it. 2. Small, related classes can be defined in a common .h and implemented in a common .cpp. However, each important class gets its own .h and .cpp. This makes code easy to find, even outside the IDE. Besides, anything else is arbitrary; no one would suggest that one big .cpp is good for a project with 10,000+ lines of code. Some kind of organization is needed, and the approach that I've described is the obvious one.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
To add to what Mircea said, function definitions (implementations) usually go in a .cpp for two reasons: 1. By default, a header is recompiled each time that a .cpp
#include
s it, so defining large functions in headers can really slow down a compile. Some compilers optimize this, but it isn't guaranteed. 2. A header should be an interface, with implementation details going in a .cpp rather than cluttering the header. However, there are two situations when function definitions go in a header: 1. If a function is simple, usually just a single line, it's expedient to just define it in the header. 2. A function template, or a function in a class template, is defined in a header. As far as where to define functions is concerned, I prefer to define each class in its own header (Class.h) and implement its functions in Class.cpp. This isn't mandatory, because the .h and .cpp are not compiled into a single binary. Each .cpp, along with all the headers that it#include
s, is compiled into its own binary, after which the linker merges them all. Some C++ gurus say there is no rationale for putting each class in its own .h and .cpp. They're wrong, but there are exceptions: 1. A "helper" class (used only to implement another one) can be defined in the same .cpp as the only class that uses it. 2. Small, related classes can be defined in a common .h and implemented in a common .cpp. However, each important class gets its own .h and .cpp. This makes code easy to find, even outside the IDE. Besides, anything else is arbitrary; no one would suggest that one big .cpp is good for a project with 10,000+ lines of code. Some kind of organization is needed, and the approach that I've described is the obvious one.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.thanks Greg that`s very descriptive