What is this trying to show?

Started by
2 comments, last by taby 18 years, 9 months ago
I'm learning about pointers and references. I have the following code... The bracket sections of the comment marks are what I don't understand. Also, you might want to bear in mind that this is the first time I'm being ontroduced to copy constructors.

// Passing Objects by Reference
#include <iostream>

using namespace std;
class SimpleCat
{
  public:
     SimpleCat ();           // constructor
     SimpleCat (SimpleCat&); //copy consructor (why have SimpleCat& inside the brackets? Does the & do anything)
     ~SimpleCat();           // destructor
};

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

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

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

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

int main()
{
   cout << "Making a cat..." << endl;
   SimpleCat Frisky;
   cout << "Calling FunctionOne..." << endl;
   FunctionOne(Frisky);
   cout << "Calling FunctionTwo..." << endl;
   FunctionTwo(&Frisky);
   char response;
   cin >> response;
   return 0;
}

// FunctionOne passes by value
SimpleCat FunctionOne(SimpleCat theCat)
{
   cout << "Function One. Returning..." << endl;
   return theCat;
}

// FunctionTwo passes by reference
SimpleCat* FunctionTwo(SimpleCat *theCat)
{
   cout << "Function Two. Returning..." << endl;
   return theCat;
}

Now the output SHOULD, in theory, be ... Making a cat ... Simple Cat Constructor... Calling Function One... Simple Cat Copy Constructor.. FunctionOne. Returning... Simple Cat Copy Constructor... Simple Cat Destructor... Calling Function Two... Function Two. Returning... Simple Cat Destructor... Now, would someone please be kind enough tell me what is going on and the basic message of this example that I need to understand. Thanks in advance.
My favourite:" Hi, I'm John Doe Jr. I am 14 years old and I know Perl, PHP, HTML, Java, C, C++, Basic, Fortran, Pascal, Ada, SmallTalk, Lisp/Scheme, Python, Modula, Algol, Simula, Haskell, Forth, Curry, Prolog, SQL, Tcl,..... ..... Sed and Awk. Now I am getting bored, and want to learn another language. Which one should I learn now? "
Advertisement
Your overloaded copy constructor is called when you pass the SimpleCat variable into FunctionOne because the parameter type is not a pointer or a reference. It instantiates a whole new copy of Frisky the SimpleCat for use within that function.

The same applies when FunctionOne returns a copy of Frisky as well. This is used for assigning the returned SimpleCat to a variable as the function returns. ie: SimpleCat Boots = FunctionOne(Frisky);

There are no new SimpleCats created for/after FunctionTwo because the variable type which is being instantiated is a SimpleCat*, which is then set to point to Frisky by passing in the value of Frisky's memory address location (& is the "address of" operator during that specific usage).

Opposite of FunctionOne as well, here Frisky does not need to be copied to a new SimpleCat at the end of FunctionTwo, since it's only the pointer containing the value of his memory address location which is returned.
What is 'SimpleCat&' doing inside of the parentheses of the copy constructor on like 9?
My favourite:" Hi, I'm John Doe Jr. I am 14 years old and I know Perl, PHP, HTML, Java, C, C++, Basic, Fortran, Pascal, Ada, SmallTalk, Lisp/Scheme, Python, Modula, Algol, Simula, Haskell, Forth, Curry, Prolog, SQL, Tcl,..... ..... Sed and Awk. Now I am getting bored, and want to learn another language. Which one should I learn now? "
It is an unnamed reference to a SimpleCat variable. It is not used inside of the copy constructor though, so a name is not needed. However, the new SimpleCat will not contain a copy of the current SimpleCat, but instead a copy of the default SimpleCat.

Change that to something like:
SimpleCat::SimpleCat(SimpleCat &src_cat)



You can then reference the member variables of the SimpleCat that was passed in (which is automatically handled for you when entering and leaving FunctionOne).

Think of it as a pre-dereferenced pointer. You can alter the original variable that was passed in, but you don't have to do any fancy pointer coding (pointers have their place in C++, but here it's references that rule).

ie:
// on the contrary...// use const SimpleCat& because you definitely don't want to edit the original SimpleCat// either way it's a reference, and that saves on CPU time because no copy is neededSimpleCat::SimpleCat(const SimpleCat &src_cat){ this->hair_colour  = src_cat.hair_colour; this->eye_colour   = src_cat.eye_colour; this->num_whiskers = src_cat.num_whiskers;}

This topic is closed to new replies.

Advertisement