What this const before method name mean?

前端 未结 1 831
迷失自我
迷失自我 2021-01-07 14:46

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         


        
相关标签:
1条回答
  • 2021-01-07 15:43

    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
    }
    
    0 讨论(0)
提交回复
热议问题