Search code examples
c++parametersvirtual

Parameter to a function in C++


Possible Duplicate:
const CFoo &bar() const

Which is the meaning of this line?

virtual void Encode (KDataStream & stream) const;

What´s the meaning of const at the end in C++?


Solution

  • Which is the meaning of this line?

    virtual void Encode (KDataStream & stream) const;
    

    It's a statement that declares a function.

    virtual means it's a member function that can be overridden by a function of the same name and compatible parameter and return types declared in a class derived from this one. The correct version will be chosen (at run-time, if necessary) according to the type of the object it's invoked on.

    void means it doesn't return anything.

    Encode is the name of the function.

    ( marks the start of the parameter list.

    KDataStream is the type of the first parameter.

    & means the parameter is passed by reference.

    stream is the name given to the parameter; it serves as documentation, but can be left out of the declaration without changing the meaning.

    ) marks the end of the parameter list.

    const means that it's a member function that can't modify non-static, non-mutable data members of the object it's invoked on. It also allows it to be invoked on objects that are declared const.

    ; marks the end of the statement.