call an object from inside of another same kind object

hi
im kinda new in learning c++.
i have a problem with objects. i have a vector of a certain class which means a vector of objects. how can i call an object from inside of another object?
Last edited on
based on what little you've told us:
(a) if the called object is the same type as the vector objects, search 'operator overloading c++'
(b) if the called object is of different type to the vector objects, it could be passed as arguments of a member function. also search 'containment c++ while you're at it if this is something relevant
again, with this sparse detail not possible to be more specific unless you post some code
class foo{ void bar(){;}};

vector<foo> v;

v[someindex].bar(); //calls the class function for the instance at "someindex" that is inside the vector. Here, [] is overloaded for the vector class to allow that access. Containers vary a lot, so this is specific to a vector of objects.

But the general truth here is that you simply access the item inside the container, the same way you would if the container were storing integers, then from that point, access the object, as if you just had a direct instance of the object. You just combine those 2 things together into 1 statement, which can sometimes look rather complicated but that is all it is doing.


Last edited on
@jonnin @gunnerfunner thanks a lot for your answers

but what i meant is kinda difference

i mean is it possible to use a method in class that links between same object?
for example calling object 2 from inside object 1.
its sometimes work with this pointer in array of object. is there way for same kind calling for vector based objects?
Last edited on
It's difficult to understand the problem at least because of some misused terminology. I imagine that by "vector-based objects" you mean "objects stored in a std::vector", and by "call" you mean "access". I don't understand what you mean by "links" in this context.

My best guess is this:
Your goal is to access the vector from an element of the same vector.

Whether my guess is correct or not, this seems like an XY problem ( http://xyproblem.info ). What problem are you trying to solve?
Yes
Your guess is right.
Now how can I solve this?
Make sure the vector itself, or a pointer or reference to it, is visible to the elements of the vector. For example:
1
2
3
struct A{ void add_new_a(); };
std::vector<A> v;
A::add_new_a() { v.emplace_back(); }

If you need to do this, you're almost certainly doing something wrong. It is highly doubtful that any object should control the collection it is an element of.

It is possible you will find a much better solution if you decide to explain your goal.
It is possible you will find a much better solution if you decide to explain your goal.

yes, fully agree and OP you've teased us enough, now please show some code even if it's incomplete, inaccurate & c
Topic archived. No new replies allowed.