Classes

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Just got through most of the learning material, is it really better to use inline functions in classes for performance?

      Source Code

      1. class Box
      2. {
      3. public:
      4. // Constructor
      5. Box(m_length = 0, m_width = 0, m_height = 0)
      6. {
      7. }
      8. // Member variables
      9. double m_length;
      10. double m_width;
      11. double m_height;
      12. // Function prototypes
      13. double Volume(void) const;
      14. }
      15. double CBox::Volume(void) const
      16. {
      17. return m_Length*m_Width*m_Height;
      18. }
      Display All


      as opposed to:

      Source Code

      1. class Box
      2. {
      3. public:
      4. // Constructor
      5. Box(m_length = 0, m_width = 0, m_height = 0)
      6. {
      7. }
      8. // Member variables
      9. double m_length;
      10. double m_width;
      11. double m_height;
      12. // Member Functions
      13. double Volume(void) const
      14. {
      15. return m_Length*m_Width*m_Height;
      16. }
      17. };
      Display All


      The book states that declaring a function prototype and then stating the function outside of the class treats it as an inline function, and that you can get better performance this way.

      I like the first one as it separates it out of the class and makes it appear cleaner to me. I'm really enjoying this part so far, as I've written a records class that just stores a name and number, and then prints it out.

      I'm also discovering that while doing the exercises in the book, I'm making things more complicated then they need to be.
      You may call me char 71 97 100 100 97 109.
    • Inline functions are an optimization done by the compiler. It allows the compiler to replace a class method call with the body of that method, wherever it is used in your code. Essentially it reduces function calling overhead. But it also increases your code size, meaning bigger compiled files (exe's, lib's, dll's).

      As a general rule of thumb, inline methods should never be complex methods, only simple methods such as getters that return a variable or simple calculations such as the Volume example shown in your method.

      In C++ you can foce a method to be inline optimized by using the inline keyword. Example:

      Source Code

      1. inline double GetLength() const { return m_length; }

      This would be perfectly fine to stick in the box class, and would be inline.

      That being said, I think you are missreading what is in the book, or the author is wrong. By default C++ will inline a method that is declared AND defined inside the class definition. If the method is defined outside of the class, it will not be inline, unless you attach the inline keyword to it.

      FOr a good example of inline usage, take a look at the Vec3, Vec4, and Mat4x4 classes in the GCC4 engine.
    • I only declare the function body inside of a class if it is either

      a) a templated function which is required
      b) a simple get function ie. Vector3f GetTranslation(){ return m_vecTranslation; }
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz
    • Originally posted by Kl1X
      In C++ you can foce a method to be inline optimized by using the inline keyword. Example:

      Source Code

      1. inline double GetLength() const { return m_length; }

      This would be perfectly fine to stick in the box class, and would be inline.

      This is not true in Visual Studio. The inline keyword just suggests to the compiler that it be inline. It's free to ignore you. You have to use the special __forceinline keyword to override this (but you never should since the compiler knows best).

      I once created a simple getter that I swore would be better inline so I used __forceinline and measure the difference. My inline function was slower.

      In fact, most compilers (including visual studio) will happily inline functions, remove useless function calls, or turn inlined functions into non-inlined functions.

      So the bottom line is: you should do whatever is the most readable. Code is for the programmer, not the compiler.


      I only declare the function body inside of a class if it is either

      a) a templated function which is required

      This is not true. You have to define the function in the header file, but you don't have to define it inside the class body. This is how I write my template classes because I like having a clean class definition where I can glance at the interface rather than the C# or Java style of function definitions where the definitions are always in the class body.


      b) a simple get function ie. Vector3f GetTranslation(){ return m_vecTranslation; }

      That should be a const function, and probably return a const reference if possible. ;)

      Source Code

      1. // best for performance
      2. const Vector3f& GetTranslation() const { return m_vecTranslation; }
      3. // not as good, but better than non-const
      4. Vector3f GetTranslation() const { return m_vecTranslation; }

      -Rez
    • Originally posted by rezination
      So the bottom line is: you should do whatever is the most readable. Code is for the programmer, not the compiler.
      -Rez


      This is what I strive for, and thanks everyone for the replies. The book is not extremely clear as the author bounces around way to much imo.
      You may call me char 71 97 100 100 97 109.
    • Rez, I am glad to hear that templated function bodies can be outside of the class header, I wanted to when I was learning about templates, I tried to do it, but I got all sorts of errors, then looking online the first source I found said that templated functions cannot be implemented seperately from the definition.

      If I try to do this what is the proper syntax given the situation;

      Source Code

      1. class SomeClas
      2. {
      3. public:
      4. template <typename T>
      5. void SomeFunc();
      6. };


      usually I would do it this way as I was under the impression it could not be done any other way

      Source Code

      1. class SomeClass
      2. {
      3. template <typename T>
      4. void SomeFunction()
      5. {
      6. //Use T somehow here
      7. }
      8. };


      I believe I have tried it this way and failed

      Source Code

      1. template <typename T>
      2. void SomeClass::SomeFunction()
      3. {
      4. //Use T somehow here
      5. }
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz

    • I believe I have tried it this way and failed

      Source Code

      1. template <typename T>
      2. void SomeClass::SomeFunction()
      3. {
      4. //Use T somehow here
      5. }


      I've been writing my templated member function definitions that way as far as i can remember and it works. I even tested it right now and it compiles fine. Maybe you just forgot one little syntax when you did that before? Sometimes i also place my definitions in a separate file. You'll see this in some SDKs or in the STL (i think) defined as *.inl or *.hpp and others. The way to do this is just place all your definitions in a separate file and just include that file in the bottom part of your header file. Something like :

      C Source Code

      1. // TemplateFile.h
      2. template < typename T >
      3. class TemplateClass
      4. {
      5. void SomeFunkyFunc( void );
      6. };
      7. // ADD DEFINITION FILE HERE
      8. #include "TempleteFile.inl"


      Then your TemplateFile.inl would simply look like this

      Source Code

      1. // NOTE: Nothing to include here...
      2. template < typename T >
      3. void TemplateClass<T>::SomeFunkyFunc( void )
      4. {
      5. // Do stuff here.
      6. }


      Also there's A LOT MORE THINGS you can do with templates besides simple data type replacements. I read a book sometime ago about template classes and really opened my eyes to the power of these templates. After reading the book, i've developed a bunch of template utility classes that I (never really) used and (didn't really) greatly helped when i was working on my template class library. It did made me very comfortable while i was working on it though. hehe

      Here's the template utility class i wrote: pulsetec.codeplex.com/SourceCo…angeset/view/15422#429942

      I'll post the book later when i get back home. I have to go for now.
    • I'll give you an example from my Quadtree class. Here's how I declared and defined the Remove() function on a node:

      Source Code

      1. // here's the partial class with the function declaration
      2. template < class Type>
      3. class QuadTreeNode
      4. {
      5. public:
      6. bool Remove(Type& data, Rect& insertRect);
      7. };
      8. // here's the function, defined in the header but outside of the class
      9. template <class Type>
      10. bool QuadTreeNode<Type>::Remove(Type& data, Rect& insertRect)
      11. {
      12. // code goes here....
      13. }
      Display All


      It's pretty simple once you know the syntax that C++ is looking for. It took me a while to figure out exactly what it wanted.

      -Rez