Alexander Kleinhans
Alexander Kleinhans

Reputation: 6258

accessing the member of a class of pointer array of another class

I'm trying to figure out how I can or why I can't access the member of this class. First I'll show you what works so you know what I'm thinking, then I'll show you what I can't seem to do.

What I can do is this: I have a class with a member. I make an pointer array of that class and make NEW pieces of it (through loop) and that's fine and all. I can also make another class with a similar array and even make NEW instances of that as well and initialize them, but when I try to access them, I have problems.

This code almost works fine:

#include <iostream>
using namespace std;

class testClass{
    public:
    int number;
};

class testPoint{
    public:
    testClass testInstance;
    testClass *testclassArray[5];
    void makeArray();
    void setToI();
};

void testPoint::makeArray(){
    for (int i = 0; i < 5; i++){
        testclassArray[i] = new testClass;
    }
}

void testPoint::setToI(){
    for (int i = 0; i < 5; i++){
        (*testclassArray[i]).number = i;
    }
}

int main(void){
    testPoint firstTestPoint;
    firstTestPoint.makeArray();
    firstTestPoint.setToI();
//  EXCEPT FOR THIS LINE this is where I have problems
    cout << firstTestPoint.(*testclassArray[0]).number << endl;
    return 0;
}

I know this should work becuase this works

int main(void){
    testPoint firstInstance;
    firstInstance.testInstance.number = 3;
    cout << firstInstance.testInstance.number << endl;
    // and this works
    return 0;
}

and this works

int main(void){
    testClass *testPointer[5];
    for (int i = 0; i < 5; i++){
        testPointer[i] = new testClass;
        (*testPointer[i]).number = i; 
    }
    cout << (*testPointer[0]).number << endl; 
    return 0; 
}

so why can't I access the members on the cout function the same way?

Upvotes: 1

Views: 2282

Answers (3)

featherless biped
featherless biped

Reputation: 173

Try using this code:

cout << firstTestPoint.testclassArray[0]->number << endl;

Upvotes: 0

inspector-g
inspector-g

Reputation: 4176

The following is invalid syntax:

cout << firstTestPoint.(*testclassArray[0]).number << endl;

The most common way to write what you are trying to accomplish is:

cout << firstTestPoint.testclassArray[0]->number << endl;

But, if you prefer, you can also write:

cout << (*firstTestPoint.testclassArray[0]).number << endl;

(The second way is far less common.)

The . operator is used to access members of direct objects, e.g. a.member where a might be declared as struct A a;. The -> operator is used to access members of indirect objects (aka pointers to objects), e.g. b->member where b might be declared as struct B* b = new B();.

Upvotes: 3

fredrik
fredrik

Reputation: 6638

You are dereferencing the variable in an incorrect way. Try doing

cout << firstTestPoint.testclassArray[0]->number << endl;

instead. In the same way the second attempt, where it works for you, could also have been written:

out << testPointer[0]->number << endl;

Upvotes: 2

Related Questions