|
-
Jul 19th, 2007, 05:48 AM
#1
Thread Starter
Addicted Member
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
-
Jul 19th, 2007, 07:29 AM
#2
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:
void AddToVariable(int &);
int main()
{
int blah = 5;
AddToVariable(blah);
cout << blah;
}
void AddToVariable(int &var)
{
++var;
}
chem
Last edited by chemicalNova; Jul 19th, 2007 at 07:33 AM.
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jul 19th, 2007, 08:06 AM
#3
Thread Starter
Addicted Member
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.
 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:
void AddToVariable(int &);
int main()
{
int blah = 5;
AddToVariable(blah);
cout << blah;
}
void AddToVariable(int &var)
{
++var;
}
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|