Re: confusion with copy constructor and operator overloading

From: E. Robert Tisdale (E.Robert.Tisdale_at_jpl.nasa.gov)
Date: 02/09/05


Date: Wed, 09 Feb 2005 12:38:38 -0800

Kelly Mandrake wrote:

> Ok I see now what you mean that you simply added the keywords inline.
> I've been putting the definitions inside my class alot too.

So do I if the class definition is short (less than a page).
But, if the class definition becomes very long,
I move constructor, operator and member and frient function definitions
(usually all of them) out of the class definition.
This also allows me to reorder these definitions
so that inline member functions are defined
before other member functions that use them.

> So these are considered light weight
> because they only have a few lines. Or is there a rule of thumb
> to determine weather to make them inline or not.

Actually, I don't worry about this.
inline function slow down compilation
and, consequently, the code-compile-test-debug development cycle
so I arrange my code so that I can build with either inline or external
function definitions:

> cat file.h
          #ifndef GUARD_FILE_H
          #define GUARD_FILE_H 1

          #ifdef EFINE_INLINE
          inline
          double dot(const double x[], const double y[], int n) {
            double t = 0.0;
            for (int j = 0; j < n; ++j)
              t += x[j]*y[j];
            return t;
            }
          #else //EFINE_INLINE
          double dot(const double x[], const double y[], int n);
          #endif//EFINE_INLINE

          #endif//GUARD_FILE_H

> cat file.cc
          #undef EFINE_INLINE
          #include "file.h"

          double dot(const double x[], const double y[], int n) {
            double t = 0.0;
            for (int j = 0; j < n; ++j)
              t += x[j]*y[j];
            return t;
            }

> cat main.cc
          #include "file.h"

          int main (int argc, char* argv[]) {
            // code to invoke dot
            return 0;
            }

> g++ -Wall -ansi -pedantic -c file.cc

Now, for all of your development, testing and debugging, you invoke

> g++ -Wall -ansi -pedantic -o main main.cc file.o

This compiles and links quickly but may run slowly.

Then, just before you are ready to release and distribute your code,
you invoke

> g++ -Wall -ansi -pedantic -DEFINE_INLINE -O2 -o main main.cc

This may take much longer to compile
but the optimized inline'd code may run much faster.



Relevant Pages

  • Re: Templates and Non-Inline Functions
    ... SomethingElse is not marked as inline and the body is not ... within the class definition, so it's not going to be inlined. ... both the definition and the decleration at the same time. ...
    (microsoft.public.vc.mfc)
  • Re: VC++2003: Methods implemented outside class body are never inlined for templates that are instan
    ... > ignore any subsequent request to inline it. ... "A member function may be defined in its class definition, ... > be classified as a compiler bug. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Which is the better way to define methods?
    ... >>within the class definition, they are inline functions. ... > Should I declare private methods in the .h file? ...
    (comp.lang.cpp)
  • Re: Why cant the linker find the definitions?
    ... > functions defined in the implementation file to be ... in other translation units and inline them automatically. ... > Inside the class declaration itself? ... If you define the function inside the class definition, ...
    (comp.lang.cpp)
  • Re: struct problem
    ... Defining a constructor in the ... Note that the implementation of all member functions can be included ... are then *implicitly* declared as 'inline'. ... functions should be inlined within the class definition. ...
    (comp.lang.cpp)

Loading