Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

methods

Code:

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;
    }
}

Calling this function raises an error in compile time:

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

I tried every case without and with inline symbol:

inline in header, not in 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 in header, also in 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 not in header, but in cpp:

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

inline not in header, neither in cpp:

 It works but that's not what I want

Question:

  1. Does const and inline member function make sense?
  2. How to declare a const and inline member function?

Thanks in advance.

like image 533
Chao Zhang Avatar asked Oct 27 '12 21:10

Chao Zhang


2 Answers

The function being const has nothing to do with it. If you want it inline, you must define it in the header file instead of in point3f.cpp. Example:

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;
        }
    }
};

In this case, the inline keyword is not needed at all. If you define the function inside the class definition, inline is the default. But you can still specify it, if you want (as I've done in the above example.)

like image 184
Nikos C. Avatar answered Nov 01 '22 09:11

Nikos C.


Im teste this and work fine! this example can be viewd at: http://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap7.html

Example 24: Overloading an operator/function with respect to 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
   }

hope with help!

peace and light!

like image 32
Armando Marques da S Sobrinho Avatar answered Nov 01 '22 09:11

Armando Marques da S Sobrinho