class ClassA
{
public:
ClassA()
{
x=0;
y=0;
}
~ClassA() {}
int GetX() {return x;}
int GetY() {return y;}
void SetX(int ix) {x = ix;}
void SetY(int iy) {y = iy;)
private:
int x,y;
};
class ClassB
{
public:
ClassB(int numObj)
{
maxObj = numObj;
a = new ClassA[maxObj];
b = new int[100];
}
~ClassB()
{
delete [] a;
delete [] b;
}
bool SomeFunc()
{
for(int i=0; i<maxObj; i++)
// ----------------
a->SetX(i);
// ----------------
}
private:
ClassA* a;
int* b;
int maxObj;
};
Class with object members
'a' is defined as follows: ClassA* a;
You initialize 'a' as follows: a = new ClassA[maxObj];
Therefore, you a creating a dynamic array of ClassA objects. These are not pointers to the objects... they are actually the objects.
So, a is getting the ClassA object at array index i. You could assign it (by value!) to another ClassA object as follows: ClassA obj1 = a; Again, this is NOT a pointer to a ClassA object, therefore you cannot use the -> operator, as this only applies to pointers.
If you want to use the -> operator, you would have to make an array of pointers to ClassA objects. Your code data would change to this: ClassA **a;
Lastly, if you are just wanting a dynamic array of ClassA objects, I would suggest using the STL. A vector is ideal for this situation:
std::vector< ClassA > a;
Or if you're wanting pointers to ClassA objects:
std::vector< ClassA* > a;
You initialize 'a' as follows: a = new ClassA[maxObj];
Therefore, you a creating a dynamic array of ClassA objects. These are not pointers to the objects... they are actually the objects.
So, a is getting the ClassA object at array index i. You could assign it (by value!) to another ClassA object as follows: ClassA obj1 = a; Again, this is NOT a pointer to a ClassA object, therefore you cannot use the -> operator, as this only applies to pointers.
If you want to use the -> operator, you would have to make an array of pointers to ClassA objects. Your code data would change to this: ClassA **a;
Lastly, if you are just wanting a dynamic array of ClassA objects, I would suggest using the STL. A vector is ideal for this situation:
std::vector< ClassA > a;
Or if you're wanting pointers to ClassA objects:
std::vector< ClassA* > a;
Thank you. I see my mistake. I guess I need to re-read a couple of chapters dynamic memory allocation and pointers.
Don't forget std::vector/map/list/etc too in that reading. Really, really handy. I was reluctant to use them for awhile (then again I was barely coding at the time too hehe) but now I love them.
Quote:Original post by klayAlloy
Thank you. I see my mistake. I guess I need to re-read a couple of chapters dynamic memory allocation and pointers.
it took me about 2 days to grasp the concept of pointers and references reading those.
Check out this link.
http://newdata.box.sk/bx/c/htm/ch08.htm
Probably will answer most questions.
Don't skim through any materials. because the author puts only essential informations in there. Everything must be memorized in that tutorial.
good luck.
pointers are one of the most important concepts for C/C++ programming, make sure you have a solid foundation with the pointers before you move on.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement