473,387 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

pointing a variable to a 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;
}
Apr 13 '12 #1
1 1735
weaknessforcats
9,208 Expert Mod 8TB
If you are in class A and need to call a method on class B then a class B object (or a pointer to it) must be a member variable of class A:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.    public:
  4.      void MethodA();
  5.    private:
  6.    B obj;
  7. };
or
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.     public:
  4.      void MethodA();
  5.    private:
  6.    B* ptr;
  7. };
Then in your code for A::MethodA():

Expand|Select|Wrap|Line Numbers
  1. void A::MethodA()
  2. {
  3.     obj.MethodB();
  4.   //or if you have the address of a B object.
  5.     ptr->MethodB();
  6. }
Then your main():

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    A obj;  
  4.    obj.MethodA();
  5. }
Of course, other member functions of A (like the constructors) must take care of either creating the B object or getting the address of it.
Apr 14 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: akiriwas | last post by:
The subject basically says what I am having trouble with. For an example lets say I have a class A which has a function f. I then also have a class B which inherits from A and has a function g. ...
13
by: jt | last post by:
Being a newbie in C++ and comming from C, I can't find information on how to access a class member function outside its class. Below is a snippet of the class and its member function: look at...
8
by: Martin Vorbrodt | last post by:
SO i have a class: template<typename T> class Vector { public: static const Vector<T> UNIT_X(); }; template<typename T> static const Vector<T> UNIT_X() {
5
by: henkoo | last post by:
i want to explicit instantiate a member function of a class template, and got some error, can anyone give some advice to correct it? 3x complier: g++ 3.2 #include <iostream> #include...
3
by: Curten | last post by:
Hi, I'm a beginner and need help with the following: I have defined a class A where a private data member is an array of structs; private: vector<a_struct> Data; The values of the...
5
by: chhenning | last post by:
Hi there, I'm surprised the following construct seems to be illegal: template< typename T > struct A { template< typename K > void do_some( const K& ); }; template< typename T >
3
by: Lakshmank85 | last post by:
Hi, i have a c++ class, class XYZ { public: void Function1 ( MyStruct * myStruct ); // member variables, etc }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.