如何在C ++中更新对象

时间:2013-08-02 22:49:40

标签: c++ oop

我有以下代码:

   vector<SomeClass> objs;
   SomeClass obj;  // create a new obj with a new name
   objs.push_back(obj);

   while (someConditionIsTrue()){
       use(&obj);
       obj = new SomeClass(); // create a new obj with an existing name
       objs.pushback(obj)
   }
此代码中的

new SomeClass();采用java OOP形式 - 而不是c ++代码。应该使用什么代码而不是obj = new SomeClass();

3 个答案:

答案 0 :(得分:2)

答案是:

obj = SomeClass();

new SomeClass()将返回指向SomeClass对象的指针。您无法将其分配给变量obj,其类型为SomeClass,而不是SomeClass*

编辑:如果我记得很清楚,SomeClass obj();也应该有用。

答案 1 :(得分:0)

我认为你想要的是这样的:

vector<SomeClass *> objs;
SomeClass* obj = new SomeClass;  // create a new obj with a new name
objs.push_back(obj);

while (someConditionIsTrue())
{
  use(&obj);
  obj = new SomeClass; // create a new obj with an existing name
  objs.push_back(obj)
}

答案 2 :(得分:0)

您对语言的理解有点偏差。以下是您注释的代码:

vector<SomeClass> objs;
SomeClass obj;  // create a new obj with a new name
objs.push_back(obj);

while (someConditionIsTrue()){
    use(&obj);
    obj = new SomeClass(); // create a new obj with an existing name
    objs.pushback(obj)
 }

这是实际发生的事情

// create an object called "objs" which is a std::vector header, on the stack.
vector<SomeClass> objs;
// create an instace of "SomeClass" on the stack, call it obj.
SomeClass obj;
// allocate storage in the heap for some number of SomeClass instances to
// act as the storage for "objs", and assign them to it's "m_data" member.
// default construct the first one,
// then call it's copy constructor with the stack instance of "Obj" to
// copy the values/data into the first entry of the heap allocation of objs. 
objs.push_back(obj);

while (someConditionIsTrue()){
   // pass a pointer to the stack-allocated instance of obj.
   use(&obj);
   // create a new instance of "SomeClass" on the heap,
   // default/zero construct it,
   // return the pointer to this heap allocation and pass it
   // to obj.operator=(const SomeClass*) to copy the data into
   // the stack space allocated for "obj".
   obj = new SomeClass(); // create a new obj with an existing name
   // leak the pointer to the previous heap allocation by not storing it.

   // increase the size of the heap allocation backing objs and then
   // default construct the first SomeClass instance in the new space,
   // then copy our freshly copy-initialized stack instance back into it.
   objs.pushback(obj)
}

你可以通过许多其他方式编写这段代码,但似乎最明智的是。

std::vector<SomeClass> objs;

while (someConditionIsTrue()) {
    objs.resize(objs.size() + 1); // grow me a new element.
    use(&objs.back());
}

或者让“使用”作为参考。

void use(SomeClass& obj);
//...
    use(objs.back());

如果你真的想要一个本地对象来尝试这个条目。

while (someConditionIsTrue()) {
    objs.resize(objs.size() + 1);
    // the compiler will either optimize this out entirely,
    // or use a pointer under the hood.
    SomeClass& obj = objs.back();
    use(&obj); // take the address of the object obj references.
}

另外,请注意“resize()”可能会变得昂贵。您可能希望提前执行此操作:

objs.reserve(1024); // or something you know is likely to cover most use cases.

如果你真的,真的必须使用指针。

SomeClass* obj = nullptr;

while (someConditionIsTrue()) {
    objs.resize(objs.size() + 1);
    obj = &objs.back();
    use(obj);
}

而不是“objs.resize()”您可以使用place-new和ctor:

    objs.emplace_back(SomeClass());

其他人建议您使用

obj = Someclass();

但请注意,这是复制对象的默认构造堆栈副本。

{
    SomeClass tempObj;
    obj.operator=(std::move(tempObj));
}

我认为这不是你真正想做的事情。它比仅具有这样的堆栈本地副本更昂贵:

while (someCondition()) {
    SomeClass stackCopy;
    ...
}

编译器足够智能,不会在每个循环中放弃堆栈空间。它会做什么叫做“Placement new”来重新调用同一个对象上的构造函数:

SomeClass obj;

while (someCondition()) {
     new (&obj) SomeClass(); // Call SomeClass() ctor on the memory at &obj
     ...
}

但是 - 这基本上是编译器如何实现我之前的代码示例。