-1

The problem is not exactly as the question. The real question is: why did my code work?

I posted this question on stackoverflow->make objects in function arguments. What I asked is whether you can do something similar to:

....
// java code
Obj o1 = new Obj(new Objectt()); // we made and passed a new object in a function argument
// OR
returned = function_does_something(new objeect());
....

in c++? I.e .

returned = function_does_something(new objectt()); // possible in c++?

Note that in the question I asked (link above), the following code worked

store.insert(store.begin(), vector<int>({key, value})); // type(store) = vector<vector<int>>

What I had thought during that time was that: call constructor to obtain a new object.
vector<int>({values}) was the constructor I called. But in c++, a constructor does not return anything, right? So why did the code work?

Vishwad
  • 251
  • 3
  • 18
  • 2
    It does not return anything; not even void. `new` returns the address of the object created on free store. It is not clear what you do not understand. – zdf Jun 02 '21 at 05:29

2 Answers2

1

A constructor doesn't return anything, but nevertheless C++ lets you declare a temporary/anonymous object as an argument to a function, in certain cases. An object declared this way will be constructed on the stack just before the function is called, passed to the function, and then destroyed immediately after the function returns.

So in this case the code is declaring a temporary/anonymous object vector<int>({key, value}) as an argument to store.insert(). It's a bit more convenient that the alternative explicit/named-declaration syntax that would accomplish the same thing:

 {
    const vector<int> someTemporaryObjectName12345({key,value});
    store.insert(store.begin, someTemporaryObjectName12345);
 }

(note that I put the curly-braces there just to ensure that someTemporaryObjectName12345 gets destroyed immediately after store.insert() returns, to match the temporary/anonymous syntax's behavior; without them someTemporaryObjectName12345 wouldn't be destroyed until the end of the containing scope)

The temporary/anonymous object-declaration syntax above will work as long as the function-parameter is of type vector<int> or const vector<int> & (and more generally, for any passed-by-value argument or passed-by-const-reference argument of any type that matches the anonymous-object's type)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • So what happened in my code was that ````vector({values}) // name = obj```` created a temporary object. This entire object was copied into a new object which was added in the vector. And then "obj" was destroyed with its copy in the vector? – Vishwad Jun 02 '21 at 05:59
  • And I did not call the constructor? – Vishwad Jun 02 '21 at 06:26
0

In addition to what Jeremy Friesner said in their answer, I'd like to add that one of the overloads of std::vector::insert has the following signature (https://en.cppreference.com/w/cpp/container/vector/insert):

iterator insert( const_iterator pos, T&& value );           //     (since C++11)  
                                                            //(2)  (until C++20)  
constexpr iterator insert( const_iterator pos, T&& value ); //     (since C++20)

That allows the temporary object constructed by vector<int>({key, value}) to be moved(1) into the new element of the vector store, while a T const& value argument would have required a copy.

Also, please note that the Standard Library offers other containers like std::map or std::unordered_map to deal with key-value pairs.


(1) What is move semantics?

Bob__
  • 12,361
  • 3
  • 28
  • 42