Question about pointers..

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#ifndef Trump_hpp
#define Trump_hpp

#include <stdio.h>

class Trump{

private:
    int age;
    
public:
    Trump(int age){
        this->age = age;
    }
    
    int getAge(){
        return age;
    }
    
};

#endif /* Trump_hpp */


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>
#include <string>
#include "Trump.hpp"
#include <vector>

using namespace std;

int main(int argc, const char * argv[]) {
  
    vector<Trump*> vec;
    vec.push_back(new Trump(60));
    vec.push_back(new Trump(70));
    vec.push_back(new Trump(80));
    for(vector<Trump*>:: iterator it = vec.begin(); it != vec.end(); it++){
        cout<<(*it)->getAge()<<endl;
    }
    
}



If "it" is a pointer to first position of vec.. why *it->getAge(); doesn't work?.. im forced to (*it)->getAge();

Why?.. there's something in my reasoning that does not work..
Last edited on
That's because * has lower precedence than ->. *it->getAge() is handled like *(it->getAge())

https://de.cppreference.com/w/cpp/language/operator_precedence
Last edited on
Oh.. now it's all clear.. thank you!!!!
Topic archived. No new replies allowed.