0

This is a question for curiosity, which I have not found the good answer yet. Suppose in "main.cc" I have the following code

namespace {

class MyClass {

public:
int GetNumber() const {return number_;}

MyClass(const int number) :
number_(number) {
}

private:
const int number_;

};

const MyClass* GetPointerToClass(const MyClass &c) {
    return (&c);
}

}  // namespace

int main(int argc, char* argv[]) {

    const MyClass my_constant_class(5);

    const MyClass* const pointer1_to_constant_class = GetPointerToClass(
        my_constant_class);
    printf("%d\n", pointer1_to_constant_class->GetNumber());

    const MyClass* const pointer2_to_constant_class = &my_constant_class;
    printf("%d\n", pointer2_to_constant_class->GetNumber());

    const MyClass& reference_to_constant_class = my_constant_class;
    printf("%d\n", reference_to_constant_class.GetNumber());

    const MyClass copy_of_my_class = my_constant_class;
    printf("%d\n", copy_of_my_class.GetNumber());

    return 0;
}

Now when I run the program, it prints: 5 5 5 5

The thing I'm curious about is why the program compiles and runs successfully?

In particular (according to my understanding, correct if I am wrong):

value of pointer1_to_constant_class is assigned to a reference of a const reference.

value of pointer2_to_constant_class is assigned to a reference of an object.

(so although both pointers are of the same type, they are assigned to different types)

value of reference_to_constant_class is assigned to an object.

value of copy_to_my_class is assigned to an object.

(so reference and object are assigned to the same type)

So why we have to call pointer2_to_constant_class->GetNumber, if it is assigned to a reference of an object.

Also, why there is no difference in calling reference_to_constant_class.GetNumber(), versus copy_of_my_class.GetNumber(),

if one is an object, another just a reference?

mercury0114
  • 1,341
  • 2
  • 15
  • 29
  • I concur with Lightness' answer. https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp4_PointerReference.html is a good reading on the confusing issue of &. – Husain Oct 07 '15 at 16:17

1 Answers1

1

Your terminology is all wrong. I can't do much more than address your specific observations. I think you're getting confused by the address-of operator &, which has effectively no relationship with the symbol & as found in reference types (or with bitwise-AND).

  • T&: the type "reference to T";
  • &a: an expression evaluating to the address of the object a (a pointer!);
  • a & b: an expression evaluating to the result of ANDing the numbers a and b.

These three usages of & are (confusingly) unrelated. So, neither return (&c) nor &my_constant_class have anything to do with references.

Reference and pointer semantics are actually remarkably simple, but once you've gone off-piste with misconceptions there's no coming back. That is one reason it is really important to learn C++ from a good, peer-reviewed book, rather than from random tutorials on the internet, your mate down the pub, or not at all!


value of pointer1_to_constant_class is assigned to a reference of a const reference.

No. A pointer of type MyClass* is assigned to pointer1_to_constant_class. No references here.

value of pointer2_to_constant_class is assigned to a reference of an object.

No. A pointer of type MyClass* is assigned to pointer2_to_constant_class. No references here.

(so although both pointers are of the same type, they are assigned to different types)

They are both of the same type, but have been assigned to different values.

value of reference_to_constant_class is assigned to an object.

An object is assigned to the object referred to by reference_to_constant_class, yes.

value of copy_to_my_class is assigned to an object.

A new object copy_to_my_class is declared and copy-initialised from an object, yes.

(so reference and object are assigned to the same type)

You never assign a reference.

So why we have to call pointer2_to_constant_class->GetNumber, if it is assigned to a reference of an object.

It's not. pointer2_to_constant_class is a pointer.

Also, why there is no difference in calling reference_to_constant_class.GetNumber(), versus copy_of_my_class.GetNumber(),

if one is an object, another just a reference?

We access member functions like a.b() whether a is the object itself, or a reference to it.

We access member functions like a->b() when a is a pointer to an object.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I am still confused on: const MyClass& reference_to_constant_class = my_constant_class; const MyClass copy_of_my_class = my_constant_class; On the right sides, we have the same types, but not on the left. Why that works? – mercury0114 Oct 07 '15 at 16:21
  • @MariusLatinis: Because references are magical aliases that do their best, in terms of syntax, to work _transparently_. Put another way, "it just does" because the language was designed that way. Speaking more generally, there are _loads_ of examples of initialisations where the RHS and LHS have different types, so this situation isn't as surprising as you think. – Lightness Races in Orbit Oct 07 '15 at 16:22