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

How to declare and implement a const and inline member function?

本文关键字:const 成员 函数 声明 何声明      更新时间:2023-10-16

代码:

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

预先感谢。

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

im测试此问题并正常工作!可以在以下网址查看此示例: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
   }

希望!

和平与光!

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