Reputation: 87
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
Upvotes: 4
Views: 22001
Reputation: 49
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.
Upvotes: -1
Reputation: 21
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.
Upvotes: 0
Reputation: 1431
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();
}
Upvotes: 0
Reputation: 59
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.
Upvotes: 0
Reputation: 477
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();
Upvotes: 0
Reputation: 3316
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
Upvotes: 0
Reputation: 323
Use public inheritance:
class B {
public:
void bar();
};
class A : public B
{ };
int main() {
A a;
a.bar();
}
Upvotes: 1
Reputation: 206566
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.
Upvotes: 1
Reputation: 7455
I think if you want use .bar() on A object A must inherit by B.
Upvotes: 0