4

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

I have ;

class A { 
 public :
  foo ( ) ;
};

class B {
 public :
  bar ( ) ;
};

in main :

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 a appropriate title. If you have one, feel free to share or edit

Coding Mash
  • 3,338
  • 5
  • 24
  • 45
user1414276
  • 87
  • 2
  • 2
  • 5
  • A quick reference to deriving classes might be helpful. http://en.cppreference.com/w/cpp/language/derived_class – R Sahu Jun 18 '14 at 02:50

9 Answers9

1

Unless the two classes are related(through inheritance) You cannot do that.

A member functions performs 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 an Apple and hoping to get a mango shake, won't really happen right.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

Use public inheritance:

class B {
 public:
  void bar();
};

class A : public B
{ };

int main() {
 A a;
 a.bar();
}
starius
  • 311
  • 4
  • 10
0

I think if you want use .bar() on A object A must inherit by B.

Mateusz Rogulski
  • 7,357
  • 7
  • 44
  • 62
0

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

bar() as no access to A's data, so bar() cannot have anything to do with the variable data. So, I would argue, that data.bar() is unnecessary, you are aiming for just bar(). Presumably, if bar() is just a function, you can declare it static and call B.data()

The other option is that you wanted inheritance which some other people have already written about. Be careful with inheritance, and make sure you inherit A from B only if you there is a is-a relationship, and it satisfies the Liskov Principle. Don't inherit from B just because you have to call bar().

If you want to use B, you can have a instance of B inside A. Read about prefering composition over inheritance

Chip
  • 3,226
  • 23
  • 31
0

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

You can only use tricks that no one really knows how its gonna behave.

You can get the pointer of an object A and cast it to be poiter of B.

Again the only use of that is to show other what not to do.

A a;
B* b = (B*)&a;
b->bar();
Horonchik
  • 477
  • 2
  • 11
0

I think you should read 1 or 2 c plus plus book(s) and get a fair idea what classes are about and what purpose they are meant to serve.

Some suggestions: The c++ programing by Bjarne Stroustrup or Thinking in c++ by Bruce Eckel or search over net for tutorials.

0

You can use a function pointer. The only way to make it not 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();
}
aj.toulan
  • 1,341
  • 1
  • 16
  • 25
0

You can also declare class B as a friend of class A.

I believe the syntax for it is:

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

But with this, I believe you can only use functions/variables from A inside B functions. Inheritance will probably be your better approach to it.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
-1

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 cast

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

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

Aimar Hassano
  • 49
  • 1
  • 1
  • The cast versions both cause undefined behaviour – M.M Dec 30 '14 at 04:46
  • Both cast versions are actually equivalent. If bar() doesn't make use of any non-static member variable form B, it can be specified as a static member function without any error or effect. – Aimar Hassano Dec 31 '14 at 02:21
  • If bar() doesn't make use of any non-static member variable form B, it acts like a static function. It can be specified as a static member function without any error or side effects. Then any valid pointer of the type B* can be used to invoke bar() without error. Try it!. – Aimar Hassano Dec 31 '14 at 02:31