Refrence

Hello
I have a question.
what dose & mean in functions?
int &a()
{
}
Thanks alot.
That means that it returns a reference. It's usually used for classes which don't have the copy constructor
eg:
1
2
3
4
5
ostream &operator << ( ostream &os, other argument)
{
    //...
    return os;
}
best with an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A
{
public:
  A() : a_(0) {;}
  int & refA() { return a_; }  // return reference to a_
  int valA() { return a_; }     // return copy of a_
protected:
  int a_;
};

A aa;

aa.refA() = 1; // you are changing the value of a_
aa.valA() = 1;  // illegal as a_ is not accessible via valA()


hth
Thanks alot i got it
Thanks alot i got it
Topic archived. No new replies allowed.