1

Let's say i have an array of objects and i want to set variables within those objects based on their array index (0, 1, 4... etc.). Is there a way to get the object's index (within its parent array) though a member function, that is, without passing an integer?

Made up example:

class Car
{
    public:
        void init();
    private:
        short weight;
};

void Car::init()
{
    // affect 'weight' based on object's array index
}

Car myCars[7];

myCars[2].init();

Is there a way to retrieve myCars' index (i.e. 2) from within init() without the function having received an integer from the outside?

I know this is not necessary, but i was curious whether it was possible.

Thank you.

3
  • 5
    Nope. (filler goes here) Commented Jan 16, 2013 at 16:47
  • Simply no! It's not possible unless you store the indices along with your Car instances. Commented Jan 16, 2013 at 16:49
  • Thanks everyone for the answers, I'll go the usual routes. Commented Jan 16, 2013 at 19:14

3 Answers 3

4

Are you willing to give more information to the init method? You can do something as follows with some pointer arithmetic:

#include <iostream>

using namespace std;

class Car
{
    public:
        void init(const Car*);
    private:
        short weight;
};

void Car::init(const Car* arr)
{
    // affect 'weight' based on object's array index
    int idx = this - arr;
    cout<< "My index: " << idx << endl;
}

int main()
{
  Car myCars[7];

  for(int i = 0 ; i < 7 ; ++i)
    myCars[i].init(myCars);
  return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

why is myCars[i].init(myCars); better than passing it directly as myCars[i].init(i);.
It's not necessarily better, although, potentially more robust if you don't actually know i or want to do sanity checks. For instance, if there was a pointer flying around Car* myCar = &myCars[x]; for some arbitrary x, then you do not need to hold that value in order to do this computation. As for sanity checks, this way you can check if &arr[idx] == this or not to see if the calculation is being done on the correct array. A lot of this would depend on implementation, really and what the OP wants to guarantee.
2

No, the C++ language doesn't provide such a capability. If you really need that (double check your design) you'll have to pass it in and maintain it if the index ever changes for your object (for example if you insert in the middle of a container).

1 Comment

Thank you, that's all i wanted to know. It it not crucial for my design, where nothing prevents me from passing variables the usual way. It was just a question of principle.
1

No.

But as an alternative, you could move the initialization to the constructor (if applicable). That gets called automatically when you create the array. But this also won't let you adjust the weight based on the index though.

2 Comments

How having a constructor is an alternative to finding the index of some array at which the object is placed?
No, the name init misleaded me. Updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.