Results 1 to 3 of 3

Thread: what is the difference between a reference and object of a class?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    what is the difference between a reference and object of a class?

    Hi all!

    (1):Can u say what are the differences between a reference and a object?

    (2): see the following ex:

    class Example
    {
    ......;
    ......;
    };
    int main()
    {
    Example e1;
    Example e2=new Example();
    .......;
    }

    2(a):I think e1 is reference to Example and e2 is object to Example... Am i right.... ?
    2(b):What is the difference between e1 and e2?

    Thanks in advance:
    regards:
    raghunadhs.v

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: what is the difference between a reference and object of a class?

    e1 is assigned memory before the execution of the program. e2 is assigned memory dynamically.

    A reference, is when you pass the address of a variable straight into another, instead of the data being copied across (you could say, its like an alias for another variable). You accomplish this using the ampersand (&) operator. For example, this would output the value "6":
    C++ Code:
    1. void AddToVariable(int &);
    2.  
    3. int main()
    4. {
    5.     int blah = 5;
    6.     AddToVariable(blah);
    7.     cout << blah;
    8. }
    9.  
    10. void AddToVariable(int &var)
    11. {
    12.     ++var;
    13. }
    chem
    Last edited by chemicalNova; Jul 19th, 2007 at 07:33 AM.

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: what is the difference between a reference and object of a class?

    Thanks chemicalNova!
    Your explenation is pretty well, i have come to an understand to distinguish an object and a reference.
    thanks alot,
    regards:
    raghunadhs.

    Quote Originally Posted by chemicalNova
    e1 is assigned memory before the execution of the program. e2 is assigned memory dynamically.

    A reference, is when you pass the address of a variable straight into another, instead of the data being copied across (you could say, its like an alias for another variable). You accomplish this using the ampersand (&) operator. For example, this would output the value "6":
    C++ Code:
    1. void AddToVariable(int &);
    2.  
    3. int main()
    4. {
    5.     int blah = 5;
    6.     AddToVariable(blah);
    7.     cout << blah;
    8. }
    9.  
    10. void AddToVariable(int &var)
    11. {
    12.     ++var;
    13. }
    chem

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