C ++ private member declared in header vs static variable declared in cpp file

I have a variable that I prefer to declare in the cpp file instead of the header file. It should be available only for objects of this class. This variable must have a separate copy for each object of this class. Inheritance is not required.

Usually I just declare it in the class definition.

hijras:

class A { private: int number; } 

But can I do this instead?

Bh:

 class B { private: // nothing } 

B.cpp:

 static int number; 
+4
source share
6 answers

No, if you take the second approach, you make it a static variable, which means that you will not have another copy for each object of this class (they will all share this variable).

In any case, if only this class should have access to it, it should go in the class declaration and should not be a global variable. By setting a global global variable, you restrict access only to the scope of the file, not to the class. As a good programming practice, try to use as few global variables as possible.

+10
source

If your goal is to hide implementation details from a client that sees only your header files (either for privacy or to exclude dependencies on internal library elements), the design template you are looking for is the pimpl template ("implementation pointer").

myclass.h:

 class MyClassImpl; class MyClass { public: MyClass(); ~MyClass(); int someFunc(); private: MyClassImpl * pimpl; } 

myclass.cpp:

 class MyClassImpl { public: MyClassImpl(); int someFunc(); private: // whatever members you actually need } MyClass::MyClass() { pimpl = new MyClassImpl(); } MyClass::~MyClass() { delete pimpl; } int MyClass::someFunc() { return pimpl->someFunc(); } // go on implementing MyClassImpl as you would have implemented MyClass 

Beware: This sample code does not have the correct copy semantics. Instead, you can use some kind of smart pointer or implement the correct constructor / assignment mechanism for the shenannigans operator.

+5
source

You can hide data in a cpp file using a personal implementation idiom.

 //a.hpp struct AData; class A { public: A(); private: AData* data_members; }; //a.cpp struct AData { int number; }; A::A(): data_members(new AData()) {} 

Not sure if it is calculated with just one integer. But this can be used to reduce compilation time: you can modify the "data members" and implement as you wish without having to recompile files, including a.hpp.

Also, prefer a suitable smart pointer over a simple pointer (one that has suitable copy behavior and can be declared with an incomplete type).

+4
source
 This variable should have a separate copy for every object of that class. 

Saving this static sound will not allow you to achieve this. This will be one instance common to all instances.

If you want each object to have its own copy of number , you will need to make it a member of your class.

+1
source

If you need a separate copy for each object of your class, you need to declare it in the header file or have some kind of static map between the value of the object and the value for this object, which is initialized by the constructor of the object.

0
source

If you want a variable to be associated with a class object, it must be declared inside the class. All object data must be inside the class. This is called Encapsulation.

0
source

All Articles