Original Post
Hi, I'm trying to prevent copying of resources by asserting when the coder manually does a copy of a resource. The only valid way should be by requesting it from the manager. Currently my code works for regular variables, but not for pointers. Why is that? How can I prevent the coders from creating a manual copy of the returned *Resource? /Robert
using namespace std;
class Resource
{
//...other code left out.
public:
Resource& operator = (Resource const &A){ cout << "Assignment of Resource not allowed, use ResourceManager.add() instead!" << endl << endl; assert(0); };
Resource(Resource const ©) { cout << "Copy of Resource not allowed, use ResourceManager.add() instead!" << endl << endl; assert(0); } ;
Resource* operator = (Resource const *A){ cout << "Assignment of Resource not allowed, use ResourceManager.add() instead!" << endl << endl; assert(0); };
Resource( Resource const *copy) { cout << "Copy of Resource not allowed, use ResourceManager.add() instead!" << endl << endl; assert(0); } ;
};
Resource First("test"); //Set manually for debugging purpose, manager returns *Resource.
Resource Second = First; //Works fine, assert() is called.
Resource *First = ResourceManager.add("test");
Resource *Second = First; //assert() doesn't work, Second gets a "copy" of First.