Results 1 to 12 of 12

Thread: Passing Objects By References, i'm lost on this aspect of the book!

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Red face 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 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

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    If you post the example, then someone (probably me if you post it anytime within the next hour or two) will explain it to you, which is probably a good idea if it's an example you're having trouble with.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Essentially references to objects are a lot like pointers, except that the syntax to access member variables and member function is as if the object was passed by value. It's convenient, and it reduces overhead by just passing the address.
    Harry.

    "From one thing, know ten thousand things."

  4. #4

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Example:

    here is the example:
    Code:
    Listing 9.11. Passing const pointers. 
    
    
    1:  //Listing 9.11
    2:       // Passing pointers to objects
    3:
    4:         #include <iostream.h>
    5:
    6:         class SimpleCat
    7:         {
    8:         public:
    9:                 SimpleCat();
    10:                 SimpleCat(SimpleCat&);
    11:                 ~SimpleCat();
    12:
    13:                 int GetAge() const { return itsAge; }
    14:                 void SetAge(int age) { itsAge = age; }
    15:
    16:         private:
    17:                 int itsAge;
    18:            };
    19:
    20:            SimpleCat::SimpleCat()
    21:            {
    22:                   cout << "Simple Cat Constructor...\n";
    23:                   itsAge = 1;
    24:            }
    25:
    26:            SimpleCat::SimpleCat(SimpleCat&)
    27:            {
    28:                   cout << "Simple Cat Copy Constructor...\n";
    29:            }
    30:
    31:            SimpleCat::~SimpleCat()
    32:            {
    33:                   cout << "Simple Cat Destructor...\n";
    34:            }
    35:
    36:const SimpleCat * const FunctionTwo (const SimpleCat * const theCat);
    37:
    38:            int main()
    39:            {
    40:                   cout << "Making a cat...\n";
    41:                   SimpleCat Frisky;
    42:                   cout << "Frisky is " ;
    43                    cout << Frisky.GetAge();
    44:                   cout << " years _old\n";
    45:                   int age = 5;
    46:                   Frisky.SetAge(age);
    47:                   cout << "Frisky is " ;
    48                    cout << Frisky.GetAge();
    49:                   cout << " years _old\n";
    50:                   cout << "Calling FunctionTwo...\n";
    51:                   FunctionTwo(&Frisky);
    52:                   cout << "Frisky is " ;
    53                    cout << Frisky.GetAge();
    54:                   cout << " years _old\n";
    55:     return 0;
    56:            }
    57:
    58:    // functionTwo, passes a const pointer
    59:    const SimpleCat * const FunctionTwo (const SimpleCat * const theCat)
    60:    {
    61:             cout << "Function Two. Returning...\n";
    62:             cout << "Frisky is now " << theCat->GetAge();
    63:             cout << " years old \n";
    64:             // theCat->SetAge(8);   const!
    65:             return theCat;
    66: }
    
    Output: Making a cat...
    Simple Cat constructor...
    Frisky is 1 years old
    Frisky is 5 years old
    Calling FunctionTwo...
    FunctionTwo. Returning...
    Frisky is now 5 years old
    Frisky is 5 years old
    Simple Cat Destructor...

  5. #5

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Red face sorry i posted the wrong one!

    here is the right one i don't understand:
    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...
    
    
    
    -----------------------------------------------------

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well.... that isn't really an example of passing objects by reference at all... the only place a reference is used is in the copy constructor but that never gets called in this example

    Okay well an alternative version of FunctionTwo() using references would be this:

    Code:
    const SimpleCat & FunctionTwo (const SimpleCat &theCat)
    {
        cout << "Function Two. Returning...\n";
        cout << "Frisky is now " << theCat.GetAge();
        cout << " years old \n";
        // theCat.SetAge(8);   const!
        return theCat;
    }
    Not much different as you can see, but it's a little bit simpler than the pointers example. Basically you just treat the reference as if it was the original object, it works just the same.

    The main difference is really how you would use it. You don't pass the address, you pass the actual object. Like this:
    Code:
    FunctionTwo(Frisky);
    It can make things a little easier.
    Harry.

    "From one thing, know ten thousand things."

  7. #7

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    what?

    I don't understand the header at all...of the fucntion...

    Code:
    const SimpleCat & FunctionTwo (const SimpleCat &theCat)
    {
        cout << "Function Two. Returning...\n";
        cout << "Frisky is now " << theCat.GetAge();
        cout << " years old \n";
        // theCat.SetAge(8);   const!
        return theCat;
    }

    where it says
    [code]
    const SimpleCat & FunctionTwo (const SimpleCat &theCat)
    {
    //blah code
    }


    Now what is that?
    const SimpleCat & FunctionTwo, is that mean the return type is constant and a reference to the class "SimpleCat is FunctionTwo, and it takes the parm of the same class? and whats &the Cat?
    the thing that is confusing me is that there is no like,
    Code:
    SimpleCat Friskey;
    SimpleCat &rFriskey = Friskey;
    Can you explain that to me, in terms of what is getting assigned to what?

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Ah, okay 2 things have gone wrong here:

    1) I was responding to the first example you posted, I didn't see the second example in time

    2) I've misunderstood what you're learning. There are such things as references in C/C++ which are useful, but I think you're talking about passing things by reference with pointers, which is a bit difference

    Basically that code I posted was using references, which are different from pointers but similar. Passing by reference can be done either with references or with pointers, and I think what you're doing is passing by reference with pointers. So I'll try to explain the pointers thing...

    Code:
    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)
    {
        cout << "Function Two. Returning...\n";
        return theCat;
    }
    Well... FunctionTwo is the easiest to explain. The function receives a pointer to a SimpleCat object, which is the accessible by dereferencing the pointer (dereferencing means using the asterisk (*) operator to get at what the pointer points to).

    The thing to remember is that in C/C++, all parameters are actually passed by value. When you pass an integer to a function, that integer is copied and placed on the stack before the function is called, then whe function takes it off the stack and does whatever it wants with it, and whatever the function does to this copy, it doesn't affect the original variable. So whatever you send to a function is always a copy of the original thing you passed in your function call, unless it's a literal, otherwise known as an rvalue. If you don't know what lvalues and rvalues are, look it up in the book

    Now, when you pass a pointer to a variable, that value is still copied. The difference is that even though it's actually a copy of the address, the value of the address is still the same, and it's still pointing to the same variable that the original variable pointed to. So when you change the variable that this copy of a pointer is pointing to, the original variable that you passed the address of gets changed.

    I hope that's fairly clear. If not, just say what's bothering you and I'll get back to you if I'm still around.
    Harry.

    "From one thing, know ten thousand things."

  9. #9

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    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
    Code:
    int * Swap(int *x)
    sorry about my ignorance

  10. #10
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Yeah I think basically you've got it.

    The return type of FunctionTwo is a pointer to a SimpleCat object, the same as the parameter it takes is. A pointer to an object is just like a pointer to anything else really.
    Harry.

    "From one thing, know ten thousand things."

  11. #11

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Thumbs up thanks alot!!

    Thanks for your time and all,

    i think i understand now, We'll find out once i get to the "Advanced Functions"

  12. #12
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827

    Thumbs up

    No problem at all
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width