Two Objects referencing each other

Hi all,

I am new here, but browsed around looking for a solution to my problem to no avail. I have a C++ program with two objects (A and B). In my main, I create both of the objects, and even run a function in A. However, I want object B to be able to use object A. I try passing object A into an initialization function so that every function in object B can use object A, but I keep getting segmentation faults (Program received signal EXC_BAD_ACCESS, Could not access memory).

Is this possible? If so, what changes need to be made so I can call runSomething() on object A inside of object B? Below is my (simplified) code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include <stdio.h>

class A{
public:
	void runSomething();
};

void A::runSomething(){
	printf("In A runSomething\n");
}

class B{
public:
	void init(A * AnewObject);
	A * AObject;
};

void B::init(A * AnewObject){
	AObject = AnewObject;
	AObject->runSomething();
}



int main(int argc, char *argv[]){
	A * mainA;
	mainA->runSomething();
	B * mainB;
	mainB->init(mainA);
}


I am expecting the runSomething to be called twice on object A, once in the main, and the second time through mainB.

I realize this is a simple question, that has probably been answered before. If there is a keyword I should be looking up, please let me know. I tried nested classes, but that didn't seem to be this issue.

Thanks in advance!
Last edited on
Your simplified program runs without any errors on my computer, and the code looks perfectly sane to me.

Although wait a minute.
@Line 20: you may wish to switch the operands; the way it is, AnewObject is being assigned the value of your AObject. Did you mean to change the value of AObject with that line?

Also, change line 21, depending on your intention to runSomething() from the argument which will be deleted on completion of that function or AObject.

-Albatross
Last edited on
Albatross,

Thanks for the reply. If it helps, I am running this on a Mac using g++ 4.2.1. What I meant to do is have AnewObject be a reference to AObject.

In the grand scheme of things for the program I am currently writing, AObject is a GUI. B is just another object. However, B does things and contains variables that I may need to place in the same GUI. So, it isn't that I want to create a new instance of AObject, instead I want to manipulate *and call functions contained in* AObject inside of BObject.

thanks!
Last edited on
Also,

You were correct about my initial code @line20. I meant to change the value of AObject in that line. I have updated the code snippet above. However, I am still getting the same segmentation fault.
My computer has almost identical specifications as yours. Admittedly, I was playing around with some system files, but... nothing regarding memory allocation, deallocation, and use in general.

Interesting. I'm sure there's something obvious that's right in front of my nose that I'm missing.

-Albatross
Albatross wrote:
I'm sure there's something obvious that's right in front of my nose that I'm missing.

Indeed :P

@Linkshot:

You never instantiate a B object. This is a problem, because in B::init you try to set a member variable of an object that doesn't exist. That and the fact that your mainB pointer could point anywhere are enough to get you a nice seg-fault :D
Your problem is you just have pointers, but the pointers aren't pointing to anything.

1
2
	A * mainA;  // mainA doesn't point to anything yet
	mainA->runSomething();  // so you can't do this!!!  What object are you running? 


Just because you have a pointer to an A doesn't mean you have an A. You must never do what you're doing.

Instead... either make your pointer point to something:

1
2
A* mainA = new A;  // now it points to a new A
mainA->runSomething();  // now we're good to go. 


Or better yet, don't use pointers:

1
2
A mainA;  // now it's an object, not a pointer
mainA.runSomething();  // good to go 


Note you have the same problem with mainB.


EDIT: doh, m4ster r0shi beat me to it. =P
Last edited on
Aha! Thank you so much. I was able to resolve this issue in my simple example using your feedback. By simple adding A* mainA = new A and B* mainB = new B, the program ran as I expected. Now to see if these changes will work in my much larger program.

Thanks again!
Oh now that's just brilliant of me. Something that simple, eh? Funny thing, I never make that mistake while programming but show me a piece of code with it and I'll skip right over it. Tsk tsk. Silly me.

-Albatross
Also note that you must delete everything you new, or else you'll have memory leaks:

1
2
3
4
5
6
7
A* mainA = new A;
A* mainB = new B;

// use mainA, mainB

delete mainA;
delete mainB;


This extra step is one reason why you should avoid pointers and dynamically allocating. I would just do this with stack allocated objects:

1
2
3
4
A mainA;
B mainB;

mainB.Init(&mainA);
Topic archived. No new replies allowed.