How to call a function of one class on an object of another class?

How can I call one method in one class using another class?

I have:

class A { public : foo ( ) ; }; class B { public : bar ( ) ; }; 

primarily:

 A data ; // I am creating instance of class A data . bar ( ) ; // but, I am calling a method of another class // how can I do that ? 

Note. I could not find the appropriate title. If you have, feel free to share or edit.

+4
source share
9 answers

If the two classes are not connected (through inheritance), you cannot do this.

Member functions perform some action on the instance of the class to which it belongs.
You created an object of class A , so you can only call member functions of A through it.

Grinding Apple and hoping to get a shake of the mango will not really happen.

+1
source

Use public inheritance:

 class B { public: void bar(); }; class A : public B { }; int main() { A a; a.bar(); } 
+1
source

I think if you want to use .bar () for object A, you must inherit B.

0
source

It is not clear what you need to do data.bar() .

bar() like no access to A data, so bar() cannot have anything to do with the data variable. So, I would say that data.bar() not needed, you are only targeting bar() . Presumably if bar() is just a function, you can declare it static and call B.data()

Another option is that you wanted to inherit, which some other people have already written about. Be careful with inheritance and make sure that you inherit A from B only if you have an is-a relationship and it satisfies the Liskov principle . Do not inherit from B just because you need to call bar() .

If you want to use B, you can have an instance of B inside A. Read preferring composition over inheritance

0
source

As everyone said in their answers. Its a bad idea and impossible.

You can only use tricks that no one knows how he will behave.

You can get a pointer to object A and overlay it with poiter of B.

Again, the only use of this is to show something else not to do.

 A a; B* b = (B*)&a; b->bar(); 
0
source

I think you should read 1 or 2 c plus plus the book (s) and get a fair idea of ​​what classes and goals they are intended to serve.

Some suggestions are: C ++ programming by Bjarne Stroustrup or Thinking in C ++ by Bruce Eckel or a web search for tutorials.

0
source

You can use a pointer to a function. The only way to make this non-static is to use templates.

 class A { public: void setBar(void (*B::func)(void)) { bar = func; }; void callBar() { bar(); }; private: void(*B::bar)(void); }; class B { public: static void bar() { printf("you called bar!"); }; }; int main() { A a; a.setBar(B::bar); a.callBar(); } 
0
source

You can also declare class B as friend class A

I believe the syntax for it is this:

 class A { public: foo(); friend class B; }; class B { public: bar(); }; 

But with that, I believe you can only use functions / variables from A inside B Inheritance is likely to be your best approach to it.

0
source

Although this question is strange !, but here are some solutions Using inheritance

 class A: public B 

Type cast

 A data; ((B*)&data)->bar(); 

Or reinterpret casting

 B* b = reinterpret_cast <B*> (&data); b->bar(); 

If bar () uses any member variables of B, then the result is not predictable.

-1
source

Source: https://habr.com/ru/post/1414574/


All Articles