C++ 如何在C++;
我有以下代码:C++ 如何在C++;,c++,oop,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
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)
}
vectorbojs;
SomeClass obj;//使用新名称创建新的obj
objs.推回(obj);
while(someConditionIsTrue()){
使用(&obj);
obj=new SomeClass();//使用现有名称创建新的obj
对象回推(obj)
}
在此代码中
newsomeclass()代码>是java OOP表单,而不是C++代码。应该使用什么代码来代替obj=newsomeclass()代码> 我想你想要的是这样的:
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)
}
vectorbojs;
SomeClass*obj=新的SomeClass;//使用新名称创建新的obj
objs.推回(obj);
while(someConditionIsTrue())
{
使用(&obj);
obj=new SomeClass;//使用现有名称创建新的obj
objs.推回(obj)
}
答案是:
obj = SomeClass();
newsomeclass()
将返回指向SomeClass对象的指针。不能将其分配给变量obj
,该变量的类型是SomeClass
,而不是SomeClass*
编辑:如果我记得清楚的话,SomeClass obj()代码>也应该起作用。您对该语言的理解有点不正确。以下是注释的代码:
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)
}
如果确实希望本地对象尝试该条目
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()”之外,您还可以将placement 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
...
}
但是-这基本上就是编译器实现我上一个代码示例的方式。您的代码编译吗obj
看起来像它存在于堆栈上,而newsomeclass()
将在堆上创建新实例,而您的objs
向量看起来不像它存储指针。你的代码看起来会泄漏内存并过度使用SomeClass
的复制构造函数。你到底想做什么?为obj
使用SomeClass
的引用或指针。std::vector没有pushback函数,但它有pushback。如果你不费心回答一个问题,那就很难了解释你想做什么。“如果我想做一些我不想告诉你的事情,我应该使用什么代码?”这不是一个实际的问题。我不知道你为什么被否决,因为这或多或少是OP代码的正确版本。除了你需要向量objs之外。呵呵,我也在想。谢谢你发现代码中的错误。我错过了那一个。1)编辑前代码不正确;2) 这只是要求内存泄漏。。。它不是你应该建议给新C++用户IMO的答案。只需把这些向量直接放在向量中,或者使用SysDypTrs。只需要确保有一个正确的拷贝赋值操作符。我认为它甚至可以与默认的拷贝赋值操作符一起工作。使其不起作用的唯一方法是“删除”复制赋值运算符。编辑:我不确定,也没有能力测试它,只是SomeClass obj()代码>应该可以工作。
{
SomeClass tempObj;
obj.operator=(std::move(tempObj));
}
while (someCondition()) {
SomeClass stackCopy;
...
}
SomeClass obj;
while (someCondition()) {
new (&obj) SomeClass(); // Call SomeClass() ctor on the memory at &obj
...
}