merija
merija

Reputation: 215

How does C++ cope with vector of objects with the same name?

Say I create an object in some local scope, and add it to a vector that exists outside the scope.

std::vector<sometype> Vec;
for( int i = 0, etc... )
{
    sometype Object;
    Vec.push_back(Object);
}

Now a copy of Object will be passed to Vec, and Object itself will be destroyed.

However if I keep doing this, I will keep creating many objects of name "Object" that are stored in my vector. Now I can access them through their index, but how does C++ cope with the fact that I have many Objects apparently all with the same name?

Upvotes: 1

Views: 1073

Answers (4)

2785528
2785528

Reputation: 5576

How does C++ cope with vector of objects with the same name.

Non-sequitur.

In your for loop, the word "Object" occurs 2 times.

a) The first occurrence, "sometype Object;", is a declaration and initialization of a region of 'automatic' memory. In this context, the word 'Object' identifies the location of the instance, within that limited scope and lifetime.

b) The second occurrence, "Vec.push_back(Object);", is a command for the compiler to generate code to pass the instance into the function 'push_back'. Here, it is passed by reference (as opposed to value), which means that the function "receives the address of", not the actual instance.

The difference is this:

  • the function's formal parameter is "const T& value"

(based on cppreference.com, where the function is declared as std::vector::push_back(const T& value) with formal name "value".)

  • that functions actual parameter is "sometype& Object".

(from your code)

  • Clearly, these two names are not the same.

So your question draws "a conclusion or statement that does not logically follow from the previous argument or statement."


Summary:

a) Your code has nothing called "value" nor 'T'.

b) And if you inspect the std::vector code, you will not find 'sometype' nor "Object'.

The names are not the same.

Upvotes: 0

Aconcagua
Aconcagua

Reputation: 25546

Just consider a variable name as an alternative representation of some memory address (transparent to you!). You have created an object at some specific location in memory, which is referred to by your variable name. You now copy the object into a new location, which has a different address, totally unrelated to the one your original object resides, so it gets a new "name" as well, while the variable name still references the old address (as long as still in scope).

Sure, inside the vector, this new "name" is not just an address, it's rather a recipe how to find it (vec[someIndex]: "go to beginning of vector's internal data, you'll find your object at given offset from there", which is; rakishly spoken, what the index operator actually does – again transparent for you).

Upvotes: 0

eerorika
eerorika

Reputation: 238491

The name of the object isn't Object. That is the name of a variable. The name of the variable that was used to copy-construct the element of the vector is in no way connected with that element.

Objects don't have names in general. Some do, in particular those objects which are named by a variable. But dynamically constructed objects do not. Even the connection between a variable of an object, and the name is quite loose. For example, if you only had pointer to an object, there would be no way of finding out if that object is named by a variable.

Upvotes: 3

fiveclubs
fiveclubs

Reputation: 2431

The name of the sometype object is irrelevant. You are correct that a copy of Object will be placed onto Vec with each call to push_back, but it's not the name of the object that is placed on the vector. An actual object is placed into the vector, which can be referred to by properly indexing into the vector.

Upvotes: 3

Related Questions