Advertisement

Pointer question

Started by June 17, 2002 12:06 AM
4 comments, last by viper35al 22 years, 4 months ago
I''m learning C++ from Sam''s Teach Yourself C++ in 21 days and I saw the following code:
  

//Listing 9.10

// Passing pointers to objects


#include <iostream.h>

class SimpleCat
{
public:
	SimpleCat ();                    // constructor

	SimpleCat(SimpleCat&);     // copy constructor

	~SimpleCat();                    // destructor

};

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

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

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

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

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

// FunctionOne, passes by value

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

// functionTwo, passes by reference

SimpleCat* FunctionTwo (SimpleCat  *theCat)// why is there an  

// * next to SimpleCat?  What does this do?  I tried taking it

// away and it wouldn''t work.  Please help!

{
	cout << "Function Two. Returning...\n";
	return theCat;
}


  
My question is at the problem area (close to the end) Thanks in advance.
Ok well its not really ''true'' reference (ie. no ''&'' operator, but that is of no use here)
It is passing a pointer to the SimpleCat object into the function, so it does not make an extra copy of the object on the stack but instead basicly tells the function were the object already is in memory and only passed the pointer (of 4 byte''s instead of making a copy of the class, of however many byte''s
eg could be a few hundred) into the function which is a hell of a lot quicker...
The function then returns a pointer the the object passed in, which is just the object its self anyway, so it is just returning the address of the object. when you take away the ''*'' in the function, you are telling it to pass by value which means making a copy of the class instance/object on the stack, then you are returning the address of that object, which by the time the function returns will no longer be there so you are returning a pointer to invald memory...

I hope I could be of help...




CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Advertisement
isn''t it just saying that it''s returning a pointer?
this is just an advise, not about your problem. But about the header.
insted of using:

#include <iostream.h>

you can use:

#include <iostream>
using namespace std;

the second one is a better and newwer way of putting the header. Another example, rather than,:

#include <math.h>

you can use:

#include <cmath>

also a better and newwer way...

I didnt realy meant to help u in the cat.whatever code, only advise u in ur headers.
Remmeber, when in doubt, x always = 8.
I think I understand now, the reason why the * was there was that theCat was a pointer, and when FunctionTwo returned it, it would be returning a simplecat pointer, not a plain simplecat, so the return type was a SimpleCat pointer not SimpleCat.

Please tell me if I am right.
You are right.

- Kyle

This topic is closed to new replies.

Advertisement