I'm sorry to ask another newbie question, but google could'nt quite help me (or maybe I just didn't understand it).
I'm trying to code a class that is capable of storing some simple connection data. My early concept looks like the following:
struct connectionElement{
string ip;
SOCKET soc;
};
class ConnectionData{
private:
vector<connectionElement> connections;
public:
ConnectionData();
~ConnectionData();
void addConnection(string ip, SOCKET soc);
};
void ConnectionData::addConnection(string ip, SOCKET soc) {
connectionElement newElement;
newElement.ip = ip;
newElement.soc = soc;
connections.push_back(newElement);
return;
}
Now I've read that objects being initialized without the use of new will be delocated once the code reaches the end of scope. So since I'm a java guy and don't know shi* about memory allocation, I was wondering what the correct way'd be to to initialize the new connectionElement in addConnection().
Do I have to use new in order to prevent the data from being deleted or does the compiler assume that a stored structure might be accessed again later on? And if I use the new operator do I have to delete all the objects manually before the thread terminates or does that happen automatically?
connectionElement
consists only a string (which manages its own memory) and a SOCKET (integer), there's nothing for you to worry about. – FrolicsomeconnectionElement
should have a constructor that takes an ip and soc. With that,addConnection
becomesconnectionElement newElement(ip, soc); connections.push_back(newConnection);
. Or evenconnections.push_back(connectionElement(ip, soc));
. – Cankered