I have an object of class which has private constructor:
class CL_GUIComponent
{
// ...
private:
CL_SharedPtr<CL_GUIComponent_Impl> impl;
CL_GUIComponent(CL_GUIComponent &other);
CL_GUIComponent &operator =(const CL_GUIComponent &other);
CL_GraphicContext dummy_gc;
};
I have a class which has a pointer to the object of the type I described before.
class Some
{
private:
CL_GUIComponent *obj;
public:
CL_GUIComponent getComp() { return *obj; }
}
But this code calls the error:
In member function ‘CL_GUIComponent Some::getComp()’:
error: ‘CL_GUIComponent::CL_GUIComponent(CL_GUIComponent&)’ is private
error: within this context
How can I store and get that object?
Return a reference instead:
and/or
The code you have their is trying to return a copy, but the copy constructor is private so it can’t access it (hence the error). In any case, for non trivial objects, it’s almost always better to return a
const&instead (in general, not always).