Jump to content
  • Advertisement
Sign in to follow this  
Clotty

What is this trying to show?

This topic is 4774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
What is 'SimpleCat&' doing inside of the parentheses of the copy constructor on like 9?

Share this post


Link to post
Share on other sites
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 needed
SimpleCat::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;
}







Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!