Up until now, I've been using the old C way of sharing objects by passing pointers instead of references. But I've run into some trouble.

Here's a simplified case of what I'm trying to do:

#include <iostream>

class Test{
	protected:
		int a;
	public:
		Test() : a(0) {}
		
		void change(int b) { a = b; }
		int getA() { return a; }
};

class Obj{
	protected:
		Test test;
	public:
		Obj(Test& t) : test(t) {}
		void check() {
			test.change(5);
			std::cout << "o.test.getA() = " << test.getA() << "\n";
		}
};

int main(){
	Test t;
	Obj o(t);
	
	t.change(4);
	std::cout << "t.getA() = " << t.getA() << "\n";
	o.check();
	std::cout << "t.getA() = " << t.getA() << "\n";
	
	return 0;
}

I'm trying to alter Test t defined in the main function through o, but the output shows that's not what's happening:

t.getA() = 4
o.test.getA() = 5
t.getA() = 4

I expected the last t.getA() to return 5. Did I so something wrong or am I misunderstanding what references are supposed to be used for, other than a replacement for sending pointers?

Aha, I'm an idiot. I forgot to define o.test as a reference.

The problem is that check() does not change the value of t, but the value of test. And about references, you've passed one only to constructor, not to check(). This works:

#include <iostream>

class Test{
	protected:
		int a;
	public:
		Test() : a(0) {}
		
		void change(int b) { a = b; }
		int getA() { return a; }
};

class Obj{
	protected:
		Test test;
	public:
		Obj(Test& t) : test(t) {}
		void check(Test& t) {
			t.change(5);
			std::cout << "o.test.getA() = " << t.getA() << "\n";
		}
};

int main(){
	Test t;
	Obj o(t);
	
	t.change(4);
	std::cout << "t.getA() = " << t.getA() << "\n";
	o.check(t);
	std::cout << "t.getA() = " << t.getA() << "\n";
	
	return 0;
}

Another thing: do your class inherit from whom?
I see protected acess, but no inheritance.

Asafe: That makes me a double-idiot. That wasn't the version of the code I meant to post. I'll edit the OP.

As for the protecteds, I'd been working with a lot of base classes lately. This is just a quick test app I threw together, so there's no inheritance. I guess it slipped my mind.

I've also found that the following works for what I was trying to do:

class Obj{
	private:
		Test &test;
	public:
		Obj(Test& t) : test(t){}
		void check() {
			test.change(6);
			std::cout << "o.test.getA() = " << test.getA() << "\n";
		}
};

Namely, Test &test instead of Test test.

Looks like I'm on an idiot-roll. Since I can't edit my post, here's what should be in the OP:

#include <iostream>

class Test{
	private:
		int a;
	public:
		Test() : a(0) {}
		
		void change(int b) { a = b; }
		int getA() { return a; }
};

class Obj{
	private:
		Test test;
	public:
		Obj(Test& t) : test(t){}
		void check() {
			test.change(6);
			std::cout << "o.test.getA() = " << test.getA() << "\n";
		}
};

int main(){
	Test t;
	Obj o(t);
	
	t.change(4);
	std::cout << "t.getA() = " << t.getA() << "\n";	// 4
	o.check();	// 6
	std::cout << "t.getA() = " << t.getA() << "\n";	// 4
	
	return 0;
}

I've been using the old C way of sharing objects by passing pointers instead of references.

You can pass something by value or reference. If you understood the difference between the two ways, mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.