Passing Objects By References, i'm lost on this aspect of the book!
Hello Everyone!!
I have been studying Teach Yourself C++ in 21 days but i'm on day 9 and its been over 3 months :p anyways, i'm stuck on this one part to understand wahts going on! I understand how to pass by refrences and by pointers but, when he busted out an example of passing objects by reference i was lost. Can someone give me a good and easy to understand example of passing objects by reference? If you want i can post up the example he gives, but its very poor i think.
Thanks for listening :D
sorry i posted the wrong one!
here is the right one i don't understand: :rolleyes:
Code:
Listing 9.10. Passing objects by reference.
1: //Listing 9.10
2: // Passing pointers to objects
3:
4: #include <iostream.h>
5:
6: class SimpleCat
7: {
8: public:
9: SimpleCat (); // constructor
10: SimpleCat(SimpleCat&); // copy constructor
11: ~SimpleCat(); // destructor
12: };
13:
14: SimpleCat::SimpleCat()
15: {
16: cout << "Simple Cat Constructor...\n";
17: }
18:
19: SimpleCat::SimpleCat(SimpleCat&)
20: {
21: cout << "Simple Cat Copy Constructor...\n";
22: }
23:
24: SimpleCat::~SimpleCat()
25: {
26: cout << "Simple Cat Destructor...\n";
27: }
28:
29: SimpleCat FunctionOne (SimpleCat theCat);
30: SimpleCat* FunctionTwo (SimpleCat *theCat);
31:
32: int main()
33: {
34: cout << "Making a cat...\n";
35: SimpleCat Frisky;
36: cout << "Calling FunctionOne...\n";
37: FunctionOne(Frisky);
38: cout << "Calling FunctionTwo...\n";
39: FunctionTwo(&Frisky);
40: return 0;
41: }
42:
43: // FunctionOne, passes by value
44: SimpleCat FunctionOne(SimpleCat theCat)
45: {
46: cout << "Function One. Returning...\n";
47: return theCat;
48: }
49:
50: // functionTwo, passes by reference
51: SimpleCat* FunctionTwo (SimpleCat *theCat)
52: {
53: cout << "Function Two. Returning...\n";
54: return theCat;
55: }
Output: 1: Making a cat...
2: Simple Cat Constructor...
3: Calling FunctionOne...
4: Simple Cat Copy Constructor...
5: Function One. Returning...
6: Simple Cat Copy Constructor...
7: Simple Cat Destructor...
8: Simple Cat Destructor...
9: Calling FunctionTwo...
10: Function Two. Returning...
11: Simple Cat Destructor...
-----------------------------------------------------
okay i understand that....but
hey thanks for your replys, but the thing i don't understand is this part:
Code:
functionTwo, passes by reference
SimpleCat* FunctionTwo (SimpleCat *theCat)
Is FunctionTwo return value going to be a pointer to SimpleCat Object?
and in the parmaeters, whats SimpleCat doing there? whats getting declared as a SimpleCat Object?
like i understand this... int Swap(int *px, int *py);
and how int Swap(int &rx, int &ry); works...
but i don't get it when they throw in a class object at the beggging of a function like SimpleCat* FunctionTwo, whats the return type?
and is SimpleCat just like saying
sorry about my ignorance :rolleyes: