T
T
Type Programmer2019-02-04 22:06:41
C++ / C#
Type Programmer, 2019-02-04 22:06:41

Accessing an object through a pointer?

I can't figure out why the error is popping up. It is necessary to create objects through the heap and then assign them to the variable x with the value n ( n is the number of the object).
PS: Help I'm dumb...

#include <iostream>
using namespace std;


class Object
{
public:
  int x = 0;
};


int main(int nArg, char* pszArgs[])
{
  Object* objects = new Object[100];
  Object* objaddr;
  for (int n = 0; n < 50; n++)
  {
    objaddr = objects + (sizeof(Object)*n);
    objaddr->x = n;
  }
  return 0;

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-02-04
@MegaCraZy6

Why not objects[n].x = n

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question