简体   繁体   English

C ++正确的结构初始化

[英]C++ proper structure initialization

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. 现在我已经读过,一旦代码到达范围结束,在不使用new的情况下初始化的对象将被分离。 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() . 因为我是一个java人,并且不知道shi *关于内存分配,我想知道在addConnection()中初始化新connectionElement的正确方法是什么。

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? 我是否必须使用new来防止数据被删除,或者编译器是否假设以后可能再次访问存储的结构? 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? 如果我使用new运算符,我必须在线程终止之前手动删除所有对象,还是自动执行?

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? 我是否必须使用new来防止数据被删除,或者编译器是否假设以后可能再次访问存储的结构?

No, in your snippet, the class ConnectionData owns its data member connections , and the elements in the vector are stored by value. 不,在您的代码段中, ConnectionData类拥有其数据成员connections ,并且向量中的元素按值存储。 Hence, connections is existant as long as its owning class instance exists: 因此,只要存在其拥有的类实例, connections就是存在的:

void someFunctionInYourProgram()
{
    ConnectionData example{};

    example.addConection(/* ... */);

    // do stuff with the ConnectionData instance and its connections

    void doMoreStuffWith(example);

 } // Now, example went out of scope, everything is automatically cleaned up.

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? 如果我使用new运算符,我必须在线程终止之前手动删除所有对象,还是自动执行?

If you allocate objects with new and don't pass the raw pointer returned to some smart poiter taking care of its deletion, you must indeed manually clean it up with delete . 如果您使用new分配对象并且没有将返回的原始指针传递给某个智能指针来处理它的删除,那么您必须使用delete手动清理它。 But there shouldn't be too many situation where this applies, as std::shared_ptr and std::unique_ptr are there to the rescue, and they ship with std::make_shared and std::make_unique , which even makes it obsolete to manually invoke the new operator. 但是应该没有太多适用的情况,因为std::shared_ptrstd::unique_ptr可以用于救援,并且它们附带std::make_sharedstd::make_unique ,这甚至使得它已经过时了调用new运算符。

One last note on this snippet 关于此片段的最后一点说明

connectionElement newElement;
newElement.ip = ip;
newElement.soc = soc;
connections.push_back(newElement);

You can simplify this to 你可以简化这个

connections.push_back({ip, soc});

which might save a copy construction (if not already optimized out by the compiler). 这可能会保存复制结构(如果编译器尚未优化)。

Your code works! 你的代码有效!

vector.push_back()

Copies the object, so a copy of the entire structure will exist in the connections vector. 复制对象,因此整个结构的副本将存在于连接向量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM