I was wondering if
*pntr.age = 19;
is valid considering that pntr is a pointer to an object that has age as a varible. If it is valid, can it be used with an array of pointers to objects? if yes can i get an example please? thank you.
I was wondering if
*pntr.age = 19;
is valid considering that pntr is a pointer to an object that has age as a varible. If it is valid, can it be used with an array of pointers to objects? if yes can i get an example please? thank you.
No its not valid. And thats because of the operator precedence.
Here is an example that shows how to make it valid :
class Int{
public :
int var;
Int() { var = 0; }
Int(const int& i) { var = i; }
};
int main(){
Int * num = new Int(0);
//how to access num data
(*num).var = 1; //option 1
num->var = 1; //option 2
delete num;
}
The operator -> is a special operator and is made for the sole reason
to make the syntax better. Rather than doing (*num).var = 0, doing
num->var = 0 is the same thing.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.