Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question