Search code examples
c++qtc++11qt5qmap

func(QWidget* const &widget) VS func(QWidget* const widget)


I've noticed a peace of code works even throwing away the ampersand/reference signal.

QWidget* widget;
func(widget);

Do the following expressions mean the same?

func(QWidget* const &widget)
func(QWidget* const widget)

I understand both are pointers that cannot be modified to stuff that can be modified.

An answer focused on the practical effects of both will be more valuable.


Solution

  • Read the definitions right-to-left:

    The first means: widget is a reference to a const pointer to a QWidget object

    QWidget* const &widget
    

    The second means: widget is a const pointer to a QWidget object

    func(QWidget* const widget)
    

    Of course they are not the same.

    Both definitions work because references are automatically derenferenced by the compiler.