zoukankan      html  css  js  c++  java
  • 'static' can indeed be used in C++ to create a Static Member Function

    'static' can indeed be used in C++ to do what you want - to create a Static Member Function.
    The compiler message is actually telling you that the 'static' keyword is not valid on the definition of the method, it should only be used in the class definition.
    So the following compiles
    class Rect
    {
    private:
        
    int x;
        
    int y;
        
    int width;
        
    int height;
    public:
        
    static bool intersects(Rect& a, Rect& b);  //Use 'static' in declaration
    };

    bool Rect::intersects(Rect& a, Rect& b)    //no 'static' - compiler know that from class declaration
    {
        
    return !(a.x > b.x+(b.width-1|| a.x+(a.width-1< b.x || a.y > b.y+(b.height-1|| a.y+(a.height-1< b.y);
    }

    int main()
    {
        Rect myRect1,myRect2;
        
    bool theSame;
        theSame 
    = Rect::intersects(myRect1,myRect2);
        
    return 0;
    };

    I have changed the result type to bool, as that appears to be your intent.
    I'll leave it to one of the experts to explain why static should only appear in the declaration - I sometimes feel the real reason for things like that are to make life harder for people new to the language:-)
  • 相关阅读:
    log4j/log4e的使用
    数据库主键不应该具有任何业务意义
    孔雀森林,何时开屏
    spring + hibernate
    JAVA的运行时类型识别(RTTI)
    开年感想,2005年总结
    iphone真机(越狱)通讯录导入进模拟器
    xcode中工程引用设置
    UIButton setImage 图片大小选择
    加密技术资源
  • 原文地址:https://www.cnblogs.com/smartvessel/p/2037729.html
Copyright © 2011-2022 走看看