PDA

View Full Version : pointer to members



mickey
31st December 2007, 17:11
hi, I've this simple problem with "thinking in c++" page 476; the examples seems wrong or not completely and compiler gets an errror:


class HowMany2 {
std::string name;
public:
std::string getName() const ;
};
//main.cpp
std::string (HowMany2::*fp)() const = &HowMany2::getName(); //compiler require a static method
int main() {
fp = &HowMany2::getName();
}

What's wrong?

marcel
31st December 2007, 17:29
It should be:


class HowMany2 {
int val;
public:
int getVal() const {
return val;
}
};
//main.cpp
int (HowMany2::*fp)() const = &HowMany2::getVal;



Are you sure that's the example from the book?

mickey
31st December 2007, 17:50
yes it's the same but I changed the variabiles names;
Now it works: it was the "()" at the end of getName(); (I'm sorry)

thanks.