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);
}