2

I apologize if this question has already been asked, but I could not find the same question. Please redirect me to the relevant question.

#include<iostream>

using namespace std;

class ABC
{
    int a;
    int &ref;
public:    
    ABC(int arg = 0):a(arg), ref(a){}
    void mutate_func(int arg) const {
        ref = arg;
    }
    void print_val() {
        cout << endl << &a << "\t" << &ref;
        cout << endl << a << "\t" << ref;
    }
};

int main()
{
    ABC abc_obj(5);
    cout << sizeof(abc_obj);

    abc_obj.print_val();
    abc_obj.mutate_func(10);
    abc_obj.print_val();
    return 0;
}

I am trying to modify a data-member of a class inside a const member function through a reference variable which is part of the same class only.

I have two questions -

  1. Why it is not throwing compilation error.

  2. I am printing address of both variables and as expected both are showing same address, but still sizeof() for the instance is showing size as 8 bytes.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
ajayg
  • 51
  • 4
  • possible duplicate of [Modifying reference member from const member function in C++](http://stackoverflow.com/questions/2431596/modifying-reference-member-from-const-member-function-in-c) – Andriy Oct 18 '12 at 07:53
  • @Andrey This is a bit different since the value being referred to is also part of the same object. – ajayg Oct 18 '12 at 07:58
  • Compiler couldn't know that when compiling `mutate_func` – Andriy Oct 18 '12 at 08:03
  • @Andrey Is this the same reason why object size is 8 bytes ?? – ajayg Oct 18 '12 at 08:15
  • Yes. You could expect the compiler is so clever to optimize `ref` away, but it is not the case. See the answer of Johannes Schaub. – Andriy Oct 18 '12 at 08:32

2 Answers2

2
  1. The compiler cannot track reference destinations completely at compile time.
  2. Because somewhere it needs to be stored where a reference refers to.

This is a class making it very easy to trigger undefined behavior. Just create a const object of it and call the const member function.

C++ supports the mutable keyword for a supported mechanism of changing the bits of an object in a const member function.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Thanks Johannes, this was useful. I am aware of mutable keyword. But I was wondering isn't using reference to change the datamember inside const function kind of hacking??? isn't there some runtime check in place ?? – ajayg Oct 18 '12 at 08:36
1

I can answer the sizeof and address part.

A reference can be seen like a pointer with a lighter syntax so it needs storage space equivalent to a pointer (4 bytes here).

A reference is modified only during its initialization ref(a). After its initialization, any operation on the reference is performed on the referenced value, so &ref gives the address of the referenced value and not the reference itself. So &a == &ref makes perfect sense.

Thelvyn
  • 264
  • 3
  • 8
  • so what happens to the original memory of reference ?? isn't this kind of memory leak problem encountered with pointers ?? – ajayg Oct 18 '12 at 08:22
  • 1
    Memory leaks appear only when you allocate data and not delete it later (using `new` and `delete` for example). If you're only pointing data there are no leaks possible. A reference references some data there's no allocation here. The space used by the reference contains the location of referenced data and nothing else. – Thelvyn Oct 18 '12 at 08:30