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
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
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:
void AddToVariable(int &);
int main()
{
int blah = 5;
AddToVariable(blah);
cout << blah;
}
void AddToVariable(int &var)
{
++var;
}
chem