【问题标题】:How to declare and implement a const and inline member function?如何声明和实现一个 const 和 inline 成员函数?
【发布时间】:2012-10-17 17:47:32
【问题描述】:

代码:

point3f.h

Class Point3f {
     ...
     inline void project2D(ProjType p, const Point2i& view) const;
};

point3f.cpp

inline void Point3f::project2D(ProjType p, const Point2i& view) const {
    switch(p) {
        case PROJ_XY:
            glVertex2f(x * view.x, y * view.y);
            break;
        case PROJ_YZ:
            glVertex2f(y * view.x, z * view.y);
            break;
        case PROJ_XZ:
            glVertex2f(x * view.x, z * view.y);
            break;
        default:
            break;
    }
}

调用此函数会在编译时引发错误:

    undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'

我尝试了所有不带和带inline符号的案例:

inline 在标头中,不在 cpp 中:

 Warning: inline function ‘void Point3f::project2D(ProjType, const Point2i&) const’ used but never defined [enabled by default
 undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'|

inline 在标头中,也在 cpp 中:

 Warning: inline function ‘void Point3f::project2D(ProjType, const Point2i&) const’ used but never defined [enabled by default
 undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'|

inline 不在标头中,而是在 cpp 中:

 undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'|

inline 不在标头中,也不在 cpp 中:

 It works but that's not what I want

问题:

  1. const and inline member function 有意义吗?
  2. 如何声明const and inline member function

提前致谢。

【问题讨论】:

    标签: c++ methods


    【解决方案1】:

    const 的函数与它无关。如果你想要它inline,你必须在头文件而不是point3f.cpp 中定义它。示例:

    class Point3f {
        ...
        inline void project2D(ProjType p, const Point2i& view) const
        {
            switch(p) {
            case PROJ_XY:
                glVertex2f(x * view.x, y * view.y);
                break;
            case PROJ_YZ:
                glVertex2f(y * view.x, z * view.y);
                break;
            case PROJ_XZ:
                glVertex2f(x * view.x, z * view.y);
                break;
            default:
                break;
            }
        }
    };
    

    在这种情况下,inline 关键字根本不需要。如果在类定义中定义函数,inline 是默认值。但如果你愿意,你仍然可以指定它(就像我在上面的例子中所做的那样。)

    【讨论】:

    • 澄清一下,问题不在于constinline 的混合(我一直这样做),而是inline 必须在标题中文件。
    • 按照你所说的,g++ 只是提出另一个警告warning: inline function ‘void Point3f::project2D(ProjType, const Point2i&) const’ used but never defined [enabled by default]
    • @ComboZhc 根本不要将函数放在 .cpp 文件中。 only 放在标题中。
    【解决方案2】:

    我对此进行了测试并且工作正常! 这个例子可以查看:http://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap7.html

    示例 24:重载与 const 相关的运算符/函数

       #include <iostream.h>
       #include <string.h>
       static unsigned const cSize = 1024;
       class InternalData {};
    
       class Buffer
       {
          public:
             Buffer( char* cp );
    
             // Inline functions in this class are written compactly so the example
             // may fit on one page. THIS is NOT to be done in practice (See Rule 21).
    
             // A. non-const member functions: result is an lvalue
             char& operator[]( unsigned index ) { return buffer[index]; }
             InternalData& get() { return data; }
    
             // B. const member functions: result is not an lvalue
             char operator[]( unsigned index ) const { return buffer[index]; }
             const InternalData& get() const { return data; }
    
          private:
             char buffer[cSize];
             InternalData data;
       };
    
       inline Buffer::Buffer( char* cp )
       {
          strncpy( buffer , cp , sizeof( buffer ) );
       }
    
       main()
       {
          const Buffer cfoo = "peter";// This is a constant buffer
          Buffer foo = "mary";// This buffer can change
    
          foo[2]='c';// calls char& Buffer::operator[](unsigned)
          cfoo[2] = 'c' // ERROR: cfoo[2] is not an lvalue.
    
          // cfoo[2] means that Buffer::operator[](unsigned) const is called.
    
          cout << cfoo[2] << ":" << foo[2] << endl; // OK! Only rvalues are needed
    
          foo.get() = cfoo.get();
          cfoo.get() = foo.get(); // ERROR: cfoo.get() is not an lvalue
       }
    

    希望得到帮助!

    和平与光明!

    【讨论】:

      【解决方案3】:

      您在 cpp 文件中将其声明为内联,因此不会发出任何符号,在 point3f.cpp 中它始终是内联的。但是包含头文件的其他文件无法内联函数,它们需要发出这个符号。我猜这里就是这种情况。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        • 2015-03-19
        相关资源
        最近更新 更多