Thread: Static Functions

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    31

    Static Functions

    Hello,

    I plan on making a D3DXVECTOR3 wrapper for my project and I was wondering if it was optimal to make math functions be static? Or wrap up the functionality in a class.

    #ifndef _Math_h_
    #define _Math_h_

    static float DotProduct(const D3DXVECTOR3 a, const D3DXVECTOR3 b)
    I would invoke the function by :
    Code:
    CVector3 vecA, vecB;
    float fDotResult = Math::DotProduct(vecA, vecB)
    OR

    Code:
    #ifndef _Math_h_
    #define _Math_h_
    
    class CVector3
    {
    public:
          D3DXVECTOR3 mVector3;
    
          float DotProduct(const D3DXVECTOR3 vec2);
     
    };
    I can call this by ...

    Code:
    CVector3 cVectorA, cVectorB;
    float fDotResult = cVectorA.DotProduct(cVectorB);
    From what I hear, static variables/functions are as slow as global variables/functions so I am curious as to which of the two cases above is better in terms of speed.
    Last edited by JohnLeeroy; 04-19-2011 at 06:55 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by JohnLeeroy View Post
    From what I hear, static variables/functions are as slow as global variables/functions so I am curious as to which of the two cases above is better in terms of speed.
    What? That is nothing but a false claim, if there is such a rumor.

    As I see it, you can do two things, which depends on what you like and what you need.
    Make a struct Vector3, and a namespace Math with vector operations.
    Create a class Vector3 and add math functions to it.
    Unless there is really any need for a class (ie some type of functionality that requires extra members as opposed to x, y, z), then I would suggest the first way, since it would allow for more flexibility (ie call them with derived classes, etc).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static functions
    By ashaikh432 in forum C# Programming
    Replies: 15
    Last Post: 08-27-2010, 03:22 PM
  2. static functions
    By ashaikh432 in forum Windows Programming
    Replies: 7
    Last Post: 08-27-2010, 01:03 AM
  3. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 03:07 AM
  4. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  5. Static functions
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 01-20-2003, 05:00 AM