Member Access Operators: . and ->
http://msdn.microsoft.com/en-us/library/b930c881.aspx

I read the above article and played around with some codes. I thought line 25 and 26 would work but they didn't. Can you guys please tell me why? Thanks for helping!

#include <iostream>

using namespace std;

class State
{
private:
    int* state;
public:
    State() { state = new int; }
    ~State() { delete state; }
    void setState(int num, State* ptr);
    int getState() {return *state;};
};

void State::setState(int num, State* ptr)
{
    for (int i = 0; i < 100; i++)
    {
        // deference pointer and set private data state to num
        *ptr[i].state = num;
        cout << ptr[i].getState();

        // since "state" is a pointer, why doesn't -> work?
        // ptr[i] -> state = num; // doesn't work. why?
        // cout << ptr[i] -> state; // doesn't work. why?
    }
}

int main()
{
    int n = 23;
    State* A;
    A = new State[100];

    A -> setState(n, A);
}

Recommended Answers

All 5 Replies

ptr[i] is not a pointer, so you need to use the dot operator .

SetState() only needs one parameter, now twom In main() you would write this:

for(int i = 0; i < 100; i++)
    A[i].setState(n);

And setState() simplifies to just one line

void State::setState(int num)
{
        *state = num;
}        

ok i will be very brief with my answer.
Your question:

since "state" is a pointer, why doesn't -> work? ptr[i] -> state = num; // doesn't work. why?

Yes it will work only if you understand how pointers work.
1. when you operate on pointers like *ptr[i].state=num; you loose the pointer in the way pointer is been called. you subscript or dive into the container. Something like a manual operation.
You can code like ptr->state = &num; to put the address of num into the state pointer how ever your intensions are different.
You are mixing apples and oranges. You need to understand both skills.
Also understand the differences between the 2 datatype.
int *state is an int pointer which its used commonly in 3 different ways due to safety but other people may diffe as this is entirely my point of view.
1. state = new int;
2. state = &num;
3 state = num; where num is an int pointer.

Your last question:

// cout << ptr[i] -> state; // doesn't work. why?
Because ptr has lost the pointerness. You have traded off its power as a pointer to a mere array. Arrays and pointer shares some similarities and this operation is one of them. Its called indexing.
You could write cout << ptr->state; but that will cause 3 errors according to what you wanna achieve.
1. You will recive the address of the first value of the ptr array.
2. You will not have the revolver or the array indexing of the ptr.
3. You cant call a member pointer of a dynamic object like that.

The solution to the first one is the call the getState()
function to get the value from the state pointer.

To get the indexing that means you must change the syntax. Yes thats the rules of the game. You follow some rules to get what you want don you?
ptr[i].getState(); Now you index ptr as an array.

I just landed in Cyprus and i am in the taxi to my hotel.
I am on my phone at the moment and hopefully i have been a help to you.

Thanks for the replies. So ptr[i] is an array and not a pointer, that's why ptr[i] -> state didn't work, because it tries to deference an array. For ptr[0], it is an object of State class, and it's in the array position 0.

Is it correct to say that, *ptr[i].state = num; the '*' dereferences the state pointer and not the array?

Is it correct to say that, ptr[i].state = num; the '' dereferences the state pointer and not the array?

Yes, and you must make sure that memory has been allocated to state pointer.

Thanks, Ancient Dragon!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.