简体   繁体   English

在C ++方法中返回对象的方法

[英]Ways to return an object in C++ method

Say I have a class that looks like this 假设我有一个看起来像这样的课程

class MyAnimals{
public:
    Animal getAnimal(int index){
        return animals.at(index);
    }
private:
    vector<Animal> animals;
}

From what I've learned about C++ so far, I think getAnimal currently returns a copy of the animal, not a reference like in Java. 从我到目前为止学到的关于C ++的知识来看,我认为getAnimal目前会返回动物的副本,而不是Java中的引用。 I've seen this suggested as the correct way to go about returning objects, but what if you want to modify that animal once it's returned? 我已经看到这被建议作为返回对象的正确方法,但是如果你想要在它返回后修改那个动物怎么办? I'd just be changing the copy of it and the actual animal inside of MyAnimals.animals would remain unchanged. 我只是改变它的副本,MyAnimals.animals里面的实际动物将保持不变。 One way I've seen to get around this is to return Animal& instead of Animal , and that seems to work for the most part, but what if I want to reassign the variable the was assigned to that returned Animal? 我看到解决这个问题的一种方法是返回Animal&而不是Animal ,这似乎在很大程度上起作用,但是如果我想重新分配给返回的Animal的变量怎么办? For example, 例如,

Animal& a = myanimals.getAnimal(1);
a = myanimals.getAnimal(2);

From what I get, this would change the animal at myanimals.animals[1] to be the same exact object as myanimals.animals[2] , since a was a reference. 从我得到的,这将改变myanimals.animals[1]的动物与myanimals.animals[2]完全相同的对象,因为a是一个参考。 What are the different ways to go about returning objects? 返回物品有哪些不同的方法?

From what I've learned about C++ so far, I think getAnimal currently returns a copy of the animal, not a reference like in Java. 从我到目前为止学到的关于C ++的知识来看,我认为getAnimal目前会返回动物的副本,而不是Java中的引用。

Correct. 正确。

I've seen this suggested as the correct way to go about returning objects 我已经看到这被建议作为返回对象的正确方法

If you intend to return a copy, then yes. 如果您打算返回副本,那么是。

I'd just be changing the copy of it and the actual animal inside of MyAnimals.animals would remain unchanged. 我只是改变它的副本,MyAnimals.animals里面的实际动物将保持不变。

Correct. 正确。

One way I've seen to get around this is to return Animal& instead of Animal, and that seems to work for the most part 我看到解决这个问题的一种方法是返回Animal而不是Animal,这似乎在很大程度上起作用

Yes, references are exactly what you need for that. 是的,引用正是您所需要的。

but what if I want to reassign the variable the was assigned to that returned Animal? 但是如果我想重新分配给返回的Animal的变量怎么办?

Well, typically there is no need to do re-assign an existing reference (which is not allowed anyway). 好吧,通常不需要重新分配现有的引用(无论如何都不允许)。 Instead of what you did, you could do: 而不是你做了什么,你可以这样做:

Animal& a = myanimals.getAnimal(1);
Animal& b = myanimals.getAnimal(2);

If you for some reason need to, then use a pointer instead, to get around the limitation. 如果由于某种原因需要,则使用指针代替,以绕过限制。 You can do this even when you return a reference: 即使您返回引用,也可以执行此操作:

Animal* a = &myanimals.getAnimal(1);
a = &myanimals.getAnimal(2);

I know of the following ways to return an Animal from MyAnimals . 我知道从MyAnimals返回Animal的以下方法。

  1. By const reference. 通过const引用。 This allows you to avoid making a copy when you need to just query the object. 这使您可以在需要查询对象时避免复制。

     Animal const& getAnimal(int index) const; 
  2. By reference. 引用。 This allows you to modify the object inside a MyAnimals . 这允许您修改MyAnimals内的对象。

     Animal& getAnimal(int index); 
  3. By value. 按价值。 This is the safest method to return an Animal as far as the contents of MyAnimals is concerned but is also the most inefficient. MyAnimals的内容而言,这是返回Animal的最安全的方法,但也是效率MyAnimals方法。

     Animal getAnimal(int index) const; 
  4. Provide access to the elements through iterators. 通过迭代器提供对元素的访问。

     class MyAnimals { public: typedef vector<Animal>::iterator iterator; typedef vector<Animal>::const_iterator const_iterator; iterator begin(); iterator end; const_iterator begin() const; const_iterator end const; Animal getAnimal(int index){ return animals.at(index); } private: vector<Animal> animals; }; 

You can't "reassign" a reference by design. 您无法通过设计“重新分配”参考。 But you can reassign a pointer. 但你可以重新分配一个指针。 So if you have a getAnimal() that returns a reference you can write: 因此,如果您有一个返回引用的getAnimal() ,您可以编写:

Animal* a = &myanimals.getAnimal(1);
a = &myanimals.getAnimal(2);

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

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