We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,031 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

pointer to avariable member function outside a class

Hello, my question is this.
In C++, how do i call a method member of class A from a class B, using a pointer.
By the way Class A and B are of different types.

I read that when a pointer is pointing to member function it can only point member functions within the class. But how can i point to a member function outside the class.?????

for example

class A
{
public:

int add(int x) {

    return x+x;
}
};
int main()

 {

  typedef int (A::*pointer)();
  pointer func = &A::add;
  A objt;
  B objt2;

  obt2.*func(2);// the compiler give me an error of incompatible with object type ‘B’


 return 0;
  }
1
Contributor
0
Replies
1
View
wilintec
Newbie Poster
1 post since Apr 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0526 seconds using 2.68MB