Sign in to follow this  

Class with object members

This topic is 4276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i]->SetX(i);
            // ----------------
    }

private:
    ClassA* a;
    int* b;
    int maxObj;
};

I wrote some similiar code today and I continued to get an error stating that a did not have an overloaded operator ->. I'm really confused why I recieved the error. Just to check it out, I used the . operator and the program ran fine. Does anyone know why?

Share this post


Link to post
Share on other sites
'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[i] is getting the ClassA object at array index i. You could assign it (by value!) to another ClassA object as follows: ClassA obj1 = a[i]; 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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this