C++ - 返回对象的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3668967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ - function that returns object
提问by Kevin Meredith
// Assume class definition for Cat is here.
Cat makeCat() {
Cat lady = new Cat("fluffy");
return lady;
}
int main (...) {
Cat molly = makeCat();
molly->eatFood();
return 0;
}
Will there be a "use after free"error on molly->eatFood()
?
是否会出现“免费后使用”错误molly->eatFood()
?
回答by Notinlist
Corrected your program and created an example implementation of class Cat
:
更正了您的程序并创建了一个示例实现class Cat
:
#include <iostream>
#include <string>
class Cat {
public:
Cat(const std::string& name_ = "Kitty")
: name(name_)
{
std::cout << "Cat " << name << " created." << std::endl;
}
~Cat(){
std::cout << "Cat " << name << " destroyed." << std::endl;
}
void eatFood(){
std::cout << "Food eaten by cat named " << name << "." << std::endl;
}
private:
std::string name;
};
Cat* makeCat1() {
return new Cat("Cat1");
}
Cat makeCat2() {
return Cat("Cat2");
}
int main (){
Cat kit = makeCat2();
kit.eatFood();
Cat *molly = makeCat1();
molly->eatFood();
delete molly;
return 0;
}
It will produce the output:
它将产生输出:
Cat Cat2 created.
Food eaten by cat named Cat2.
Cat Cat1 created.
Food eaten by cat named Cat1.
Cat Cat1 destroyed.
Cat Cat2 destroyed.
I suggest you learn a basic book about the C++ cover to cover before continuing.
我建议你在继续之前学习一本关于 C++ 的基础书籍。
回答by jwueller
new Cat("fluffy")
creates a pointer. You will need to specify Cat*
as return type. Since the object is created in heap, it will still be available after the function returned.
new Cat("fluffy")
创建一个指针。您需要指定Cat*
为返回类型。由于对象是在堆中创建的,所以函数返回后它仍然可用。
回答by Brian R. Bondy
There is no error as far as invalid use of memory other than a memory leak at the end of your program. If something is created on the heap (such as with new
) then you need to call delete
on it to free it.
除了程序结束时的内存泄漏之外,就内存的无效使用而言,没有错误。如果在堆上创建了一些东西(比如 with new
),那么你需要调用delete
它来释放它。
You also have a lot of syntax errors, corrected below.
你也有很多语法错误,下面更正。
Cat* makeCat()
{
Cat *lady = new Cat("fluffy");
return lady;
}
int main (int argc, char** argv)
{
Cat* molly = makeCat();
molly->eatFood();
delete molly;//This was added
return 0;
}
回答by Starkey
Since lady
is created on the heap (with a new
), it will not be destroyed when you exit the makeCat
method. So the call on molly
is perfectly valid.
由于lady
是在堆上创建的(带有new
),因此退出makeCat
方法时不会销毁它。所以调用molly
是完全有效的。
BUT, you have a memory leak. You need to delete molly
after using it (sometime in the future.) Since your program ends, this is not a big deal. In a larger program, this would be a very big deal.
但是,您有内存泄漏。molly
使用后需要删除(以后的某个时间)。既然你的程序结束了,这没什么大不了的。在一个更大的程序中,这将是一个非常大的问题。
回答by koresh
If your complier supports c++11 you can use unique_ptr here:
如果您的编译器支持 c++11,您可以在此处使用 unique_ptr:
#include <iostream>
#include <memory>
using namespace std;
class Cat {
public:
Cat() {
cout << "Cat created" << endl;
}
~Cat() {
cout << "Cat destroyed" << endl;
}
void eatFood() {
cout << "Cat is eating food" << endl;
}
};
unique_ptr<Cat> makeCat() {
unique_ptr<Cat> lady( new Cat );
return lady;
}
int main () {
unique_ptr<Cat> molly = makeCat();
molly->eatFood();
return 0;
}
Now, you don't have to worry about deleting created object. It will be deleted as soon as molly pointer goes out of scope:
现在,您不必担心删除创建的对象。一旦 molly 指针超出范围,它将被删除:
Cat created
Cat is eating food
Cat destroyed
回答by pascal
The problem is not a "Use after Free"; more probably that you're not delete-ing the new instance.
问题不是“免费后使用”;更有可能的是您没有删除新实例。
回答by Eiko
I think it should be Cat *lady
and Cat *molly
, but else it should be ok.
我认为应该是Cat *lady
and Cat *molly
,否则应该没问题。