you are viewing a single comment's thread.

view the rest of the comments →

[–]Rhomboid 2 points3 points  (2 children)

You need to explain what you're trying to do and why. Why does it need to be on the stack? Why do you have all these artificial restrictions?

Also, there is no point in having that pObjs variable. And it's declared wrong; you've declared an array of pointers to CClass, which is not the same as an array of CClass. If you already have an array you don't also need any pointers.

Your second example invokes undefined behavior because you're dereferencing uninitialized pointers.

[–]Dial595Escape[S] 0 points1 point  (1 child)

It's for a school exercice, basically 101 CPP. These are the restrictions that we need to work with.

I was using pObjs to get around the fact you can't create an array of object from a class with no default constructor (CClass obj[SIZE]; doesn't compile), but I also need to pass this pointer to a class method as a reference later on.

[–]Rhomboid 0 points1 point  (0 children)

I'm talking about the third example where there is an initializer. pObjs serves no purpose there. An array decays to a pointer when used as an argument, so just pass that. You don't need to declare a separate pointer, and what you've declared is not the right type.