Mahesha999
Mahesha999

Reputation: 24721

What this const before method name mean?

In one of our text-books it is suggested that we should use interfaces in C++ as a good design practice. They give below example;

class IAnimation
{
  public:
      virtual void VAdvance(const int deltaMilisec) = 0;
      virtual bool const VAtEnd() const = 0;
      virtual int const VGetPostition() const = 0;
};

I did not get the meaning of:

virtual bool const VAtEnd() const = 0;
virtual int const VGetPostition() const = 0;

I know const is used after () to make them invocable from const instances. But what const before VAtEnd and VGetPosition (method names) mean?

Thank you.

Upvotes: 0

Views: 2135

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258568

It means the return type is const, it's the same as:

virtual const bool VAtEnd() const = 0;
virtual const int VGetPostition() const = 0;

It has no practical meaning though, as the return value is copied anyway.

If you'd be returning an object though:

struct A
{
    void goo() {}
};

const A foo() {return A();}



int main()
{
    A x = foo();
    x.goo();      //ok
    foo().goo();  //error
}

Upvotes: 7

Related Questions