Alternative implementation syntaxes for C ++ class members

When declaring and implementing a class or structure in C ++, we usually do:

H file

namespace Space{ class Something{ void method(); } } 

CPP file

 void Space::Something::method(){ //do stuff } 

OR

 namespace Space{ void Something::method(){ //do stuff } } 

Note how you can wrap all implementations inside a namespace block, so we don’t need to write Space :: in front of each member. Is there a way to wrap class elements in a similar way?

Please note that I want to keep the source and header section . This is usually good practice.

+4
source share
4 answers

You cannot if you save the .cpp file.

Otherwise, you can, but you are essentially just deleting the .cpp file.

In your .h file, you can:

 namespace Space{ class Something{ void method() { //Method Logic } }; } 

In addition, as you know, there will be restrictions as to where you might include the headline.

I'm not sure this will help you, OP, as you seem to know what you are doing, but I will include it for future users of the question:

What should be in the .h file?

+1
source

Not without prejudice to the separation between the header file and the implementation (cpp).

You can declare everything inside the string in the header inside the class, but this is very dirty for large classes.

What problem are you trying to solve? Or is it just printing time? :)

+3
source

Yes:

.h file:

 namespace Space{ class Something{ void method() { // do stuff } }; } 

I do not recommend doing this.

+2
source

No, unless you put class members directly in the class definition

 class Something { void method() {} }; 

This is discouraging as it causes the header to bloat if your class definition is in the header file.

I prefer not to use the packaging method myself, so each element of the function explicitly indicates its namespace. This helps to detect typos or dead code if you remove the prototype.

+1
source

Source: https://habr.com/ru/post/1494537/


All Articles