I have an (for C++ programmers better than me) simple problem with classes and pointers.
I thought about posting example code describing my problem but I found it easier to just explain it in words.
Assuming I have three classes:
- Class A: The main class – it contains an instance of both
BandC. - Class B: This class contains a method that outputs some string, call it
Greet(). - Class C: This one has a method too, but that method has to call
Greet()in the instance ofBthat is located in classA. Let’s name itDoSomethingWithB()
So the program starts, in the main function I create an instance of A. A, again, creates instances of B and C. Then, A calls C.DoSomethingWithB();.
And there my problem begins: I can’t access B from inside C.
Obviously, I will need to pass a pointer to B to the DoSomethingWithB() function so that I can call B.Greet() from inside C
Long explanation, short question: How do I do this?
Example code incoming:
#include <iostream>
using namespace std;
class B
{
public:
void Greet( )
{
cout<<"Hello Pointer!"<<endl;
}
};
class C
{
public:
void DoSomethingWithB( )
{
// ... b.Greet( ); Won't work obviously
}
};
class A
{
public:
B b; // Not caring about visibility or bad class/variable names here
C c;
void StartTest( )
{
c.DoSomethingWithB( );
}
};
int main( )
{
A mainInstance;
mainInstance.StartTest();
}
Wouldn’t you simply pass a pointer or reference to he
Bobject?If the
DoSomethingWithB()function won’t modify the passed inBinstance, you should mark the referenceconstso it can be called with aconstB object (for example if the owningAobject happens to beconst):You have a few options for how to pass the
Bobject to the function:as a reference (
void DoSomethingWithB( B& b)) which will let the function modify the passed in object. Changes will be refelected in the object that’s passed in.as a
constreference (void DoSomethingWithB( B const& b)) which won’t let the function modify the passed in object (unless the constness is cast away – something which can lead to undefined behavior if done on an object the is truelyconst)as a pointer or
constpointer to aBobject (void DoSomethingWithB( B* b)orvoid DoSomethingWithB( B const* pb)). These have similar performance to passing by reference, but the function could be passed a NULL pointer which needs to be dealt with properly (by not dereferencing it in that case). Also, the call of the function would need to change slightly to pass the address of theBobject:as a pass-by-value parameter (
void DoSomethingWithB( B b)). This has difference that the function can do whatever it likes with theo bject passed in and it won’t affect the originally passed object since the function is dealing with a copy. The disadvantage is that passing the parameter causes a copy to be made which might be expensive. You could also pass in aconstvalue, but there’s little to recommend that over passing aconstreference.Note that when chosing the parameter passing method, you should first chose based on the sematics of what you need the function to do (or not do). Worry about efficiency later. Always first design and code for correctness – worry about efficiency only after you have the design and code correct.