& and % operators

The & operator is too confusing.
When I write a copy constructor I write it like this

1
2
3
4
5
6
7
8
class Example
{
public:
    Example();
    Example(Example&) //what does the Example& in the parameters do?
    ~Example()
private:
}


And the second thing is the modulus operator do?
I read it many times and I don't get it.
for example what is 15 % 20?
Last edited on
Different uses of the ampersand operator in c++:
http://stackoverflow.com/a/5051937/2089675

In your case, this is the address of operator.

The modulus operator:
http://stackoverflow.com/a/12556990/2089675
I didn't get the link of & operator X(.
Sometimes I think I am stupid.
Can you give me one example
In this context (to the right of a typename, such as int&) it is a reference. A reference is not a variable itself, but rather is an 'alias' or another name for an existing variable.

Simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    int a = 5;  // a is a variable

    int& b = a;  // b is a REFERENCE to a.

    // now... at this point... you can think of 'a' and 'b' as different names for
    //   the same variable

    // changes to 'a' will be seen in 'b'
    a = 8;
    cout << b << endl;  // prints '8'

    // and vice versa:
    b = 3;
    cout << a << endl;  // prints '3'
}


When you pass a parameter to a function by reference it's the same idea:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void func(int& changeme)
{
    changeme = 0;
}

int main()
{
    int foo = 5;
    func( foo );  // 'foo' is being passed as a reference.  This means 'changeme'
        // in 'func' will be a reference to 'foo' (they will both be different names for
        //   the same variable)

    cout << foo << endl;  // prints 0
}
Thanks very much Disch I but I see some classes like this
1
2
3
4
5
6
7
8
9
10
11
12
13
class Example
{
public:
    Example();
    ~Example() {}
    const Example& operator++()//What is the 'Example&' used for?
    {
        ++value;
         return *this;
    }
private:
    int value
}


What is the 'Example&' used for in the operator overloading function on line 6?
And by the way how does the this pointer work?
That & is also a reference; it is not different than other uses of it really.

The this pointer is just a pointer to the current class.

Given your class Example:

1
2
3
4
5
6
7
int main() {
    Example e1;
    Example e2;
    ++e1; //inside operator ++(), this = &e1
    ++e2; //inside operator ++(), this = &e2
    //etc
}

What is the 'Example&' used for in the operator overloading function on line 6?


That is exactly the same as what I described above. The only difference is that it's returning a reference, rather than passing a reference to a function.

It's returning a reference to an Example object. You could use that like so:

1
2
3
4
5
6
7
Example obj;  // <- an object

Example& ref = ++obj; // <- perform the ++ operator
   // the returned value (a reference to 'obj') is assigned to the 'ref'
   //   reference.

// at this point... 'ref' and 'obj' are different names for the same object. 




And by the way how does the this pointer work?
this points to the object on which the currently executing member function is acting.

Every (nonstatic) member function requires an object. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Example
{
public:
    void printThis()
    {
        cout << this << endl;
    }
};

int main()
{
    Example a, b;  // 2 objects

    // print the address of a:
    cout << &a << endl;

    // call 'printThis' with 'a'.
    //   since we are using 'a' as the object... inside of the printThis
    //   function... 'this' will point to 'a':
    a.printThis();  // so printThis will print the same address as &a


    ////////////////////
    // print address of b:
    cout << &b << endl;

    // now call printThis with 'b'
    //   this time... since 'b' is the object we're using... inside of printThis,
    //   'this' will point to 'b':
    b.printThis();  // so printThis, this time, will print the same address as &b
}



EDIT: ninja'd by firedraco
Last edited on
The 'Example&' in that case is the return type for that operator overload, it's not that much different from a regular function it just looks a little odd with the cv specifier in the beginning there. Try putting your thumb over the 'const' to see if it is easier to understand. Once you think you know what the function is doing move your thumb and read it again to see if it still makes sense. What 'const' is doing here is extending the lifetime of your reference by binding it to the instance of the class that called this member function so that your program knows not to free up the memory before you get a chance to use it.

The 'this' keyword indicates a pointer to the instance of the object that is calling the function. In cases like this where you want the function to return the instance of the variable that called it, you dereference the 'this' operator and return it. It really is nothing more then a way for a class to indicate the specific instance that the member function is operating on.

EDIT: I knew at least one person would beat me to this.
Last edited on
Thanks guys.

ninja'd by firedraco
:D funny.
Topic archived. No new replies allowed.