1. Let's say there is a class - that is part of a library - intended for the programmer's use. But if I give the programmer a header file with the full definition, he can easily change private members to public. Can I let the programmer use a partial definition that contains only the public members?
2. Can I use a reference to a pointer? If yes, is it int*& ptrref; or int&* ptrref ?
But the user could do that. Nothing about the mangled names of the variables include the access type of the variable. Having said that, I would not recommend a user do such
a thing. But if you really, really don't want the user to see the details of your class, the
solution is to use the pImpl idiom. (pointer to implementation). Search online for pImpl.
A simple pImpl is:
// Implementation.h
// Implementation class definition.
class Implementation
{
public:
// constructor
Implementation( int v ): value( v )
{
}
// set value to v
void setValue( int v )
{
value = v;
}
// return value
int getValue() const
{
return value;
}
private:
int value; // datat that we would like to hide from the client
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Interface.h
// Proxy Class interface definition
// Client sees this source code, but the source code does not reveal the data layout of class Implementation
class Implementation; // forward declaration
class Interface
{
public:
Interface( int );
void setValue( int );
int getValue() const;
~Interface();
private:
Implementation *ptr;
};
// Interface.cpp
// Implementation of class Interface -- client receives this file only as precompiled object code, keeping
// the implementation hidden.
#include "Interface.h"
#include "Implementation.h"
// constructor
Interface::Interface( int v ) : ptr ( new Implementation( v ) )
{
}
void Interface::setValue( int v )
{
ptr->setValue( v );
}
int Interface::getValue() const
{
return ptr->getValue();
}
Interface::~Interface()
{
delete ptr;
}
Thanks for your help! I don't think I need a pimpl...I just wanted to know what the standard method is. I'm working on an open-source project anyway so I don't really need to hide anything.
What's: Can I use a reference to a pointer? If yes, is it int*& ptrref; or int&* ptrref ?
did you mean: int** ptrref;???
you need a pointer to a pointer right?Or I didn't get it right?