class pointing to class

Hey, I'm using VS 2015, though this 'should' work for any version of C++ console version.

basically, I store a number in a class object. and I want another class object to grab that number from that class. here is what I've got:

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
32
33
34
35
36
#include <iostream>

using namespace std;

class Classy
{
public:
	int TakeANumber() {  return NO;	}
	void GiveANumber(int input){ NO = input;	}
private:
	int NO = 0;
};

class Classy2
{
public:
	void FeedClass(Classy T) {  HoldClass = &T;	}

	int Return() {	return HoldClass->TakeANumber();	}

private:
	Classy *HoldClass;
};

void main()
{
	Classy John;
	Classy2 Bill;
	John.GiveANumber(12);

	Bill.FeedClass(John);

	cout << Bill.Return() << endl;
	cin.get();

}


This compiles just fine, but the output is definitely not what I'm looking for (should be 12)

I hope this post comes out correctly..

Thank you!

Edit: I relized I have no comments which is a pain for people to look at, so here is a rundown of what it's supposed to do with comments:

1
2
3
4
5
6
7
8
	Classy John; // make a class object John
	Classy2 Bill; // make a class object Bill
	John.GiveANumber(12);  // feed a number for John to hold on to

	Bill.FeedClass(John); // Tell Bill where the Class John is so it can acess it's functions.

	cout << Bill.Return() << endl; // ask bill what the number is that John was holding.
	cin.get();
Last edited on
You need to pass John as a reference to Bill and then it will work.
1
2
3
4
void FeedClass(Classy& T) 
{ 
   HoldClass = &T; 
}
+Thomas1965

Lmao! I really should have been able to spot that >.<

Thank you! that saved me a lot of frustration! =D
Topic archived. No new replies allowed.