Object Reference

So I'm doing an exercise for passing pointers. There is a bit of the example code that doesn't make sense to me.

On line 27 SimpleCat* FunctionTwo(SimpleCat*TheCat) is giving me a bit of confusion. What is this? The syntax tells me its a function, but it is notated like a pointer to an object within the SimpleCat class.

Also, the piece of code that's trying to show that when a function is called by value, it creates a copy.. is notated on line 16: SimpleCat::SimpleCat(SimpleCat&), is this not using a reference? Why is that associated with the copy?


I know this is a bit all over the place, but its causing quite a bit of confusion. Thanks so much for your help!


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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

class SimpleCat
{
public:
	SimpleCat();
	SimpleCat(SimpleCat&);
	~SimpleCat();
};

SimpleCat::SimpleCat()
{
	std::cout << "Simple Cat Constructor ... \n";
}

SimpleCat::SimpleCat(SimpleCat&)
{
	std::cout << "Simple Cat Copy Constructor......\n";
}

SimpleCat::~SimpleCat()
{
	std::cout << "Simple Cat Destructor.... \n";
}

SimpleCat FunctionOne(SimpleCat theCat);
SimpleCat* FunctionTwo(SimpleCat *theCat);

int main()
{
	std::cout << "Making a cat....\n";
	SimpleCat Frisky;
	std::cout << "Calling FunctionOne .... \n";
	FunctionOne(Frisky);
	std::cout << "Calling FunctionTwo ... \n";
	FunctionTwo(&Frisky);
	return 0;
}

SimpleCat FunctionOne(SimpleCat theCat)
{
	std::cout << "Function One.  Returning ... \n";
	return theCat;
}

SimpleCat* FunctionTwo(SimpleCat *theCat)
{
	std::cout << "Function Two.  Returning .... \n";
	return theCat;
}
Last edited on
1. line:27
1
2
SimpleCat* FunctionTwo(SimpleCat *theCat);
}

I dont understand whts problem in this.
This is prototype of simple function having one parameter and return value.
one parameter: SimpleCat *theCat - this the ponter of class SimpleCat.
return value: And it return pointer of same class type, i.e. "SimpleCat".

2.line:16
1
2
3
4
SimpleCat::SimpleCat(SimpleCat&)
{
	std::cout << "Simple Cat Copy Constructor......\n";
}

This simple Copy Constructor,
Which have one parameter of same class type and it is reference type.

Please tell us what exactly you dont understand?
Topic archived. No new replies allowed.