When creating my own library for something and it uses pointers, should I make it so I don't need to use the pointers when implimenting the class, and have a "wrapper" instead? Or is it just more efficient to use the pointers? I can see having a wrapper being more safe to avoid mistakes as the wrapper can handle all the deletes and what not.
Here's an example I'm working on, it's a TCP server class. I will declare a new server object, call listen with the port # and all that, then poll for new connections. It is non blocking. If it gets a new connection a new "client" object is gotten so I can use it within the implimetor application. Now, should this client object be a pointer (the socket class creates it as pointer) or should I have a wrapper class that contains that pointer?
Part of me says to have the wrapper class another part of me says to just give the pointer out. What is normally the best practice in situations like this?
Also, if I do make a non pointer wrapper class that contains the pointer, and that object gets passed around functions, put in arrays, etc... it gets copied around creating multiple instances of that object (But it's the same pointer so it's still pointing to same actual data). How do I go about making it so if I delete the pointer in one of the instances, the other instances will know that the pointer is deleted?
For example:
Here's an example I'm working on, it's a TCP server class. I will declare a new server object, call listen with the port # and all that, then poll for new connections. It is non blocking. If it gets a new connection a new "client" object is gotten so I can use it within the implimetor application. Now, should this client object be a pointer (the socket class creates it as pointer) or should I have a wrapper class that contains that pointer?
Part of me says to have the wrapper class another part of me says to just give the pointer out. What is normally the best practice in situations like this?
Also, if I do make a non pointer wrapper class that contains the pointer, and that object gets passed around functions, put in arrays, etc... it gets copied around creating multiple instances of that object (But it's the same pointer so it's still pointing to same actual data). How do I go about making it so if I delete the pointer in one of the instances, the other instances will know that the pointer is deleted?
For example:
Code:
class A
{
//data and stuff
};
Class WA //wrapper class
{
A * data;
WA(); //creates a "A" object and stores to data
delete(); //deletes pointer to free memory
//more stuff
}
void function(WA a)
{
a.delete();
}
main()
{
WA wrapperinstance;
function(wrapperinstance)
//here is there a way I can know that the pointer was deleted if I try to use wrapperinstance?
}