problem with pointer to class method

K

Kelly Mandrake

Ive been searching this newsgroup for examples and help with pointers
to class methods, there were results but none solved my problem. My
problem is a compiler error.

When I compile the folowing I recieve an error:


Error: must use .* or ->* to call pointer-to-member function in `
pGetAge (...)'

The thing is i have used ->* as directed. Here is what i have

// Main:

int main(int argc, char *argv[])
{
Cat * myCat = new Cat(6);

int (Cat::*pGetAge) (void) const;
pGetAge = &Cat::GetAge;

cout << "Cat is " << myCat->*pGetAge() << " years old!" << endl;

delete myCat;

system("PAUSE");
return EXIT_SUCCESS;
}

=======================
//Class Declaration:

class Cat
{
public:
Cat(int age = 1): mAge(age) {}
~Cat(){}

int GetAge(void) const { return mAge; }
void SetAge(int age) { mAge = age; }
protected:
int mAge;
};
From what I read the syntax is corect and this should work. I am
useing gcc 3.3.1
 
J

Jonathan Turkanis

Kelly said:
When I compile the folowing I recieve an error:
int main(int argc, char *argv[])
{
Cat * myCat = new Cat(6);

int (Cat::*pGetAge) (void) const;
pGetAge = &Cat::GetAge;

cout << "Cat is " << myCat->*pGetAge() << " years old!" << endl;

You need (myCat->*pGetAge)(); the function call operator bind tighter than the
pointer to member dereference operator.
delete myCat;

system("PAUSE");
return EXIT_SUCCESS;
}

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top