I have struct called Rectangle:
我有一个名为Rectangle的结构:
struct Rectangle {
std::vector<float> m_min;
std::vector<float> m_max;
exType timeOfArrival, timeOfexpiry;
unsigned int id;
};
In a function I pass a rectangle by value and save the object into a DB. I want to return the pointer of the rectangle so that I can use it out of the function and save it (the pointer) in a vector. From what I have read as of C++11 there is no need to return a pointer so I return the rectangle. Is this the right way? How can I have the pointer to the object created in the function?
在函数中,我按值传递一个矩形并将对象保存到DB中。我想返回矩形的指针,以便我可以将其用于函数并将其(指针)保存在向量中。从我从C ++ 11开始读到的,没有必要返回一个指针,所以我返回矩形。这是正确的方法吗?如何获得指向函数中创建的对象的指针?
This is how I do it now:
这就是我现在的做法:
Rectangle Insert(Rectangle rect){// "Insert" is a function of another class
SaveIntoToDB(rect);// Calls other functions that use reference
return rect;
}
I need something fast that does not create duplicates in memory.
我需要快速的东西,不会在内存中创建重复。
1 个解决方案
#1
1
What would be wrong with this?
这会有什么问题?
void Insert(const Rectangle &rect)
{
...
}
Called like
Rect myrect = something...;
Insert(myrect);
doSomething(&myrect); // Do something with pointer to myrect
Generally, based on your question and code, you should be avoiding pointers completely at this stage.
通常,根据您的问题和代码,您应该在此阶段完全避免使用指针。
#1
1
What would be wrong with this?
这会有什么问题?
void Insert(const Rectangle &rect)
{
...
}
Called like
Rect myrect = something...;
Insert(myrect);
doSomething(&myrect); // Do something with pointer to myrect
Generally, based on your question and code, you should be avoiding pointers completely at this stage.
通常,根据您的问题和代码,您应该在此阶段完全避免使用指针。