The underlying issue is that the consumers of MyNewClass
need to know how big it is (e.g. if it needs to be allocated on the stack), so all members need to be known to be able to correctly calculate the size.
I'll not address the patterns you already described. There are a few more that could be helpful, depending on your use-case.
1. Interfaces
Create a class with only the exposed methods and a factory function to create an instance.
Upsides:
- Completely hides all private members
Downsides:
- requires heap allocation
- lots of virtual function calls
Header:
class MyClass {
public:
static MyClass* create();
virtual ~MyClass() = default;
virtual void doSomeAction() = 0;
};
Source:
class MyClassImpl : public MyClass {
public:
void doSomeAction() override { /* ... */ }
private:
void method1UsesForwarded() { /* ... */ }
void method2UsesForwarded() { /* ... */ }
Forwarded f;
};
MyClass* MyClass::create() {
return new MyClassImpl();
}
2. Subclass Implementation
This is somewhat similar to the Interfaces one, with the upside that it gets rid of the virtual functions.
Upsides:
- Completely hides all private members
Downsides:
Header:
class MyClass {
public:
static MyClass* create();
// only the destructor needs to be virtual now
virtual ~MyClass() = default;
void doSomeAction();
private:
MyClass() = default;
};
Source:
class MyClassImpl : public MyClass {
public:
void method1UsesForwarded() { /* ... */ }
void method2UsesForwarded() { /* ... */ }
Forwarded f;
};
void MyClass::doSomeAction() {
// e.g.:
static_cast<MyClassImpl*>(this)->method1UsesForwarded();
// ...
}
MyClass* MyClass::create() {
return new MyClassImpl();
}
3. Pre-allocating space for private members
If you don't mind a bit of manual work you can calculate how much space the private members will need and just provide a large enough buffer for that in the base class.
Upsides:
- Completely hides all private members
- Does not require a heap allocation
Downsides:
- You need to manually check how large a buffer you need
- Required size might change when using different compilers / different compiler versions
Header:
class MyClass {
public:
MyClass();
~MyClass();
void doSomeAction();
private:
void method1UsesForwarded();
void method2UsesForwarded();
struct MyClassMembers* getMembers();
/*
here you need to enter the size and alignment requirements
of your private data buffer.
*/
std::aligned_storage_t<8, 4> m_members;
};
Source:
struct MyClassMembers {
int i = 0;
int j = 0;
};
MyClass::MyClass() {
static_assert(sizeof(m_members) >= sizeof(MyClassMembers), "size too small!");
static_assert(alignof(m_members) >= alignof(MyClassMembers), "alignment too small!");
new (&m_members) MyClassMembers();
}
MyClass::~MyClass() {
getMembers()->~MyClassMembers();
}
MyClassMembers* MyClass::getMembers() {
return std::launder(reinterpret_cast<MyClassMembers*>(&m_members));
}
void MyClass::doSomeAction() {
method1UsesForwarded();
/* ... */
}
void MyClass::method1UsesForwarded() {
/* ... */
std::cout << getMembers()->i << std::endl;
}
void MyClass::method2UsesForwarded() {
/* ... */
}
One gotcha of this approach is that you need to provide the correct size & alignment for your data struct (in this case MyClassMembers
).
You can either eyeball it (the static_asserts ensure that it won't compile if the requirements are not met) by trying to compile with different values until it works, or write a short function that prints out the correct values for you:
struct MyClassMembers {
int i;
int j;
};
int main(int argc, char* argv[]) {
std::cout << sizeof(MyClassMembers) << std::endl;
std::cout << alignof(MyClassMembers) << std::endl;
}