Reputation: 73
i have a variable that i prefer to declare in a cpp file instead of the header file. It should be accessible to objects of that class only. This variable should have a separate copy for every object of that class. Inheritance is not necessary.
Normally, I'd just declare it in the class definition.
A.h:
class A {
private:
int number;
}
But, can I do this instead?
B.h:
class B {
private:
// nothing
}
B.cpp:
static int number;
Upvotes: 7
Views: 16695
Reputation: 1801
It is possible to hide data members in the cpp file using the private 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 pays off with just one integer. But this can be used to reduce compile times: you can modify A's "data members" and implementation at will without having to recompile files including a.hpp.
Also, prefer a suitable smart pointer instead of the plain pointer (one that has suitable copy behavior and can be declared with an incomplete type).
Upvotes: 7
Reputation: 70263
If your goal is to hide implementation details from a customer who only sees your header files (either for secrecy, or to avoid dependencies on library internals), the design pattern you're looking for is the pimpl
pattern ("pointer to implementation").
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 example code does not have proper copy semantics. You might want to use some smart pointer instead, or implement the proper copy constructor / assignment operator shenannigans.
Upvotes: 17
Reputation: 23105
If you want a variable to be associated with a class object, it has to be declared inside the class. All the object data should be inside the class. That is called Encapsulation.
Upvotes: 0
Reputation: 14786
If you want a separate copy for every object of your class, you need to declare it in the header file or have some sort of static map between the object value and the value for that object that is initialized by the object constructor.
Upvotes: 0
Reputation: 206536
This variable should have a separate copy for every object of that class.
Keeping it static wont let you acheive this. It will be one copy shared by all instances.
If you want each object to have its own copy of number
then you will have to make it an member of your class.
Upvotes: 2
Reputation: 10553
No, if you take the second approach, you're making it a static variable, which means you won't have a different copy for each object of that class (they'll all share that variable).
Either way, if it should only be accessed by that class, it should go in the class declaration, and should not be a global variable. By making it a static global variable, you're restricting access just to the scope of the file, not to the class. As good programming practice, try using as few global variables as possible.
Upvotes: 14