I have a struct that I never explicitly defined as a pointer. But when I try to access its member variables with a .
instead of ->
, I get an error, and ->
seems to be the only correct option.
As far as I understand, you only use ->
to dereference a pointer to a class and access its members. In the code below, Employee
is not a pointer, but if I use .
, I get an error. Why is that so?
struct Employee {
public:
int getAge(){ return this.age; }
void setAge(int age){ this.age = age; }
private:
int age{18};
};
int main() {
Employee emp;
emp.setAge(55);
std::cout << emp.getAge() << '\n';
}