Sonia Seddiki Sonia Seddiki - 2 months ago 6
C++ Question

Is it better to return a vector<int> or an int*?

I have a method which is supposed to return some integers. Those integers will be used after in a constructor. Since I am not very comfortable with pointers, I decided to use a vector as my return type so that later in the constructor of myObject I can use myVector[i] to access the different ints.

But my colleague says that a int* as a return type is better in this case because "if it's to return a container, then you'd better return a myObject type and instantiate it directly in the method".

So it seems that the use of a pointer is better in this case. Is it true? And then why?

EDIT: Ok, i'll try to detail it more down here.

Here is my class with three int attributes:

class MyClass{

public:
MyClass(int x, int y, int s);

private:
int coord_x;
int coord_y;
int size;
}


In another .cpp file, I have a method like this:

// FileWithMethod.cpp
vector<int> myMethod(){

// some code here
return my_vector;
}


my_vector has 3 int and always will because its elements will be used to instantiate a MyClass object in a second .cpp file.

The discussion here is whether the return type of myMethod() should be an int* or a vector. My colleague suggests an int* because "it is useless to implement this kind of structure (i.e. a method that returns parameters that will be used later in a constructor)". Why he says that, I don't know. He says that this structure is interesting if we use a pointer but if we use a container, then it is better to directly return a MyClass object instantiated with the 3 int. So he seems to be clearly against the idea of vector, probably in terms of memory allocation/management, and would prefer the use of an int* against a MyClass object.

And here I thought that the major asset of C++ was to get rid of the tricky pointers and to use them only when there is no other option or when the performance difference is non negligible.

I hope this clarifies a bit more the situation. Sorry for the vague explanation. It is not easy for me to estimate if the amount of details provided is enough to understand the problem when you are working on it and have no outside perspective :).

Thank you for your answers, they are already quite helpful!

Answer

From your colleague's quotes I assume he is recommending returning the object you aim to instantiate instead of vector, not the int*. This means that if sole purpose of this return value is to be an input to the other object's constructor, why not construct the object in this function and skip the temporary structures.

In your example it would be:

MyClass myMethod(){    
    // some code here
    return MyClass{ /* my_vector values */ };
}

That said, returning pointer to array (which I assume you mean by int*) is probably not a good idea, because you would have to keep track of who is supposed to delete it later (after MyClassconstruction). In this regard standard containers are preferable.

If you do not want to use vector because of its minor performance impact (which you should consider living with anyway IMO) and cannot instantiate the target class in the method, maybe there is another way. I would use tuple<int, int, int> for example as a return value. It's very lightweight and easier to use than raw c-like array. It is not as easy to maintain if, for example, number of parameters change in the future.

Comments