-1

almost absolute beginner here. This is the code for an example from the Qt docs:

#include <QObject>

class Counter : public QObject
{
    Q_OBJECT

public:
    Counter() { m_value = 0; }

    int value() const { return m_value; }

public slots:
    void setValue(int value);

signals:
    void valueChanged(int newValue);

private:
    int m_value;
};

value() is a function? So is the value in the parameters for setValue(int value) calling the function value() without using the parenthesis and, hence, getting the return value of value() to pass to setValue()?

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

2

You asked:

So is the value in the parameters for setValue(int value) calling the function value() without using the parenthesis and, hence, getting the return value of value() to pass to setValue()?

No. The identifier value in the function setValue() shadows the member function value. Inside setValue(), you can refer to both of them but they need different syntax.

void setValue(int value)
{
    m_value = value; // This is the argument.
    this->value()    // This calls the member function.
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Don't know of any other way of posting 'thanks' to all who answered. I am embarrassed that I didn't see that. I remember now that when I first tried learning C++ years ago we never used the same identifier(?) for different things so it kind of threw me. – user2741671 Dec 20 '14 at 08:35
0

No. The name of setValues parameter is just an ordinary local variable of type int. It is not related in any way to the Counter::value() function.

Inside Counter::setValue function parameter value will hide class member function value(). This means that inside Counter::setValue unqualified name value will always refer to the parameter.

You still can work around the hiding and refer to the member function by using a qualified name Counter::value() or by referring to it as this->value().

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765