如何声明和实现const和内联成员函数?

时间:2012-10-27 21:14:44

标签: c++ methods

代码:

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
  3. 提前致谢。

3 个答案:

答案 0 :(得分:5)

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是默认值。但是如果你愿意,你仍然可以指定它(正如我在上面的例子中所做的那样。)

答案 1 :(得分:1)

我试试这个并且工作正常! 可以在以下位置查看此示例:http://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap7.html

示例24:关于const-ness

重载运算符/函数
   #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
   }
希望得到帮助!

和平与光明!

答案 2 :(得分:0)

你在cpp文件中将其声明为内联,因此没有发出符号,在point3f.cpp中它始终是内联的。但是包含标题的其他文件无法内联函数,它们需要发出此符号。我想这就是这种情况。