Declare the definition of the forwarded class in the cpp file, for what?

What is the point of transferring a class definition to a .cpp file?

Imagine that I have a private class inside another public class. I am forwarding a private class definition like class Private.

Are there any advantages to posting my private class declaration in a .cpp file, or should I just stick to my direct declaration in my public .h class and include my privateClass.h in the cpp file?

+3
source share
4 answers

Inner classes and structures are often best removed from public headers to avoid dependencies and relationships.

, , , ( /) . , ( ODR - ) , /vtable / mangling.

, ,

" " , ( cpp). , , . ; ( , ). , . pImpl. , (, Boost SmartPtr).

backgrounder, :

+4

, .h , , . Private , .

+1

. Private .

, , . , cpp, .

+1

. :

// Class2.h

class Class1;

class Class2
{
    Class1* m_class1; // Using Class1 type
};

This allows Class2.h to use Class1 without having to include Class1.h in the header file. Of course, any .cpp file that includes Class2.h should also include Class1.h.

Just remember that you can declare types in several modules, but you can only define types in one of them.

+1
source

All Articles