gettin some error and dont give me s**t about the header files :D

getting some problem in constructors can u givme an example program in copying of constructors
closed account (3qX21hU5)
http://www.cplusplus.com/articles/y8hv0pDG/
yo @Zereo why do we use the ampersand(&) inside the parameters?
and thnx for the link
closed account (3qX21hU5)
The & is the reference operator, when you use it in a function parameter you are telling the function to alter directly whatever you pass as a arguement instead of creating a temporary copy of the arguement.

For example here is some code that demostrates this.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void changeToZero(int &number)
{
    // Changes whatever you put in for the number parameter directly to 0
    number = 0;
}

int main()
{
    int myNumber = 7;
    
    // Notice how the number is 7
    cout << myNumber << endl;

    // This changes myNumber to 0 without returning anything.
    // This happens because the & operator is telling the function to directly alter myNumber.
    changeToZero(myNumber);

    // Number is now 0
    cout << myNumber << endl;

    return 0;
}


Now there is other reasons as to why people use the reference operator when dealing with member functions and functions. One of them is when you have a very large complex object, it is usually not good for performance to have function parameters create a copy of it so they can do calculations with it. So they use the reference operator (or a pointer) so that the object doesn't need to be copied. This is getting into some more advanced topics about performance so I won't go into detail about it, but here is a link that can help http://en.wikipedia.org/wiki/Reference_(C%2B%2B)

Hope this helps explain it, if it doesn't or you have any other questions just let me know and I would be glad to help with anything else.
Last edited on
ur awesome :D





Topic archived. No new replies allowed.