Pointer to Function Inside Struct using Static

Discussion in 'C++' started by Bryan Parkoff, Mar 28, 2005.

  1. I know how to write "Pointer to Function" inside struct or class without
    using static, but I have decided to add static to all functions inside
    struct or class because I want member functions to be bound inside struct or
    class to become global functions. It makes easier for me to use
    "struct.memberfunction()" instead of "globalfunction()" when I have to use
    dot between struct and member function rather than global function.
    I do not have to depend on namespace to bound struct variables and
    global functions. The problem is that pointer to global function can't be
    converted to "this" to function array because static member function is
    considered the global function so "this" to function array is not supported
    by C++ Compiler.
    The reason is that I prefer to avoid member functions inside struct or
    class because optimization under C++ compiler does not use x86's 7 registers
    and it has to create many instructions to sustain only one to two registers.
    It would be very slow and degrade the performance. Global functions are
    highly recommended to access struct or class variable so they can access 7
    registers after optimization. It has reduced un-needed instructions. It
    increases best performance.
    If there is no way to fix my error in this code below, I would stick
    struct variable and global functions inside namespace.

    // Load.h
    class _MPU
    {
    public:
    static void (*Run2[2056])(void);

    unsigned int A;
    unsigned int B;
    unsigned int C;
    unsigned int D;
    unsigned int E;
    unsigned int F;
    unsigned int G;
    unsigned int Inc;

    static void AA(void);
    static void BB(void);
    static void CC(void);
    static void DD(void);
    static void EE(void);
    static void FF(void);
    static void GG(void);

    void Init(void);
    void Run(void);
    };
    extern _MPU MPU;

    // Load.cpp
    #include "Load.h"

    _MPU MPU;

    void (_MPU::*Run2[2056])(void) =
    {
    _MPU::AA,
    _MPU::BB,
    _MPU::CC,
    _MPU::DD,
    _MPU::EE,
    _MPU::FF,
    _MPU::GG,
    };

    void _MPU::AA(void)
    {
    MPU.A += 1;
    MPU.B += 1;
    MPU.C += 1;
    MPU.D += 1;
    MPU.E += 1;
    MPU.F += 1;
    MPU.G += 1;
    return;
    }

    void _MPU::BB(void)
    {
    MPU.A += 2;
    MPU.B += 2;
    MPU.C += 2;
    MPU.D += 2;
    MPU.E += 2;
    MPU.F += 2;
    MPU.G += 2;
    return;
    }

    void _MPU::CC(void)
    {
    MPU.A += 3;
    MPU.B += 3;
    MPU.C += 3;
    MPU.D += 3;
    MPU.E += 3;
    MPU.F += 3;
    MPU.G += 3;
    return;
    }

    void _MPU::DD(void)
    {
    MPU.A += 4;
    MPU.B += 4;
    MPU.C += 4;
    MPU.D += 4;
    MPU.E += 4;
    MPU.F += 4;
    MPU.G += 4;
    return;
    }

    void _MPU::EE(void)
    {
    MPU.A += 5;
    MPU.B += 5;
    MPU.C += 5;
    MPU.D += 5;
    MPU.E += 5;
    MPU.F += 5;
    MPU.G += 5;
    return;
    }

    void _MPU::FF(void)
    {
    MPU.A += 6;
    MPU.B += 6;
    MPU.C += 6;
    MPU.D += 6;
    MPU.E += 6;
    MPU.F += 6;
    MPU.G += 6;
    return;
    }

    void _MPU::GG(void)
    {
    MPU.A += 7;
    MPU.B += 7;
    MPU.C += 7;
    MPU.D += 7;
    MPU.E += 7;
    MPU.F += 7;
    MPU.G += 7;
    return;
    }

    void _MPU::Init(void)
    {
    Inc = 0;
    A = 0x41;
    B = 0x51;
    C = 0x61;
    D = 0x71;
    E = 0x81;
    F = 0x91;
    G = 0xA1;
    return;
    }

    void _MPU::Run(void)
    {
    // AA();
    // BB();
    // CC();
    // DD();
    // EE();
    // FF();
    // GG();
    }

    // Main.cpp
    #include <stdio.h>
    #include "Load.h"

    void main (void)
    {
    MPU.Init();

    MPU.Run();
    // MPU.Run2();

    printf("Hello...\n");
    }

    Error compiling
    Compiling...
    Load.cpp
    D:\Test\Load.cpp(7) : error C2440: 'initializing' : cannot convert from
    'void (__cdecl *)(void)' to 'void (__thiscall _MPU::*)(void)'
    There is no context in which this conversion is possible
    D:\Test\Load.cpp(8) : error C2440: 'initializing' : cannot convert from
    'void (__cdecl *)(void)' to 'void (__thiscall _MPU::*)(void)'
    There is no context in which this conversion is possible
    D:\Test\Load.cpp(9) : error C2440: 'initializing' : cannot convert from
    'void (__cdecl *)(void)' to 'void (__thiscall _MPU::*)(void)'
    There is no context in which this conversion is possible
    D:\Test\Load.cpp(10) : error C2440: 'initializing' : cannot convert from
    'void (__cdecl *)(void)' to 'void (__thiscall _MPU::*)(void)'
    There is no context in which this conversion is possible
    D:\Test\Load.cpp(11) : error C2440: 'initializing' : cannot convert from
    'void (__cdecl *)(void)' to 'void (__thiscall _MPU::*)(void)'
    There is no context in which this conversion is possible
    D:\Test\Load.cpp(12) : error C2440: 'initializing' : cannot convert from
    'void (__cdecl *)(void)' to 'void (__thiscall _MPU::*)(void)'
    There is no context in which this conversion is possible
    D:\Test\Load.cpp(13) : error C2440: 'initializing' : cannot convert from
    'void (__cdecl *)(void)' to 'void (__thiscall _MPU::*)(void)'
    There is no context in which this conversion is possible
    Main.cpp
    Error executing cl.exe.

    Test.exe - 7 error(s), 0 warning(s)

    ___________________________________________
    Note:
    Need to fix error like this below.
    void (_MPU::*Run2[2056])(void) =
    {
    _MPU::AA,
    _MPU::BB,
    _MPU::CC,
    _MPU::DD,
    _MPU::EE,
    _MPU::FF,
    _MPU::GG,
    };
    ______________________________________

    Bryan Parkoff
     
    Bryan Parkoff, Mar 28, 2005
    #1
    1. Advertising

  2. Bryan Parkoff wrote:
    > [...]
    > void (_MPU::*Run2[2056])(void) =


    Change it to

    void (*_MPU::Run2[2056])(void) =

    the difference is where the asterisk is placed. In your case it
    makes the declaration "an array of pointers to members of _MPU",
    in my it makes it "an array of pointers to functions".

    V
     
    Victor Bazarov, Mar 28, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ole
    Replies:
    4
    Views:
    623
    Michael Wojcik
    Oct 26, 2004
  2. beetle
    Replies:
    2
    Views:
    966
    beetle
    Jan 25, 2005
  3. Replies:
    7
    Views:
    435
    Old Wolf
    Feb 23, 2005
  4. Zero
    Replies:
    16
    Views:
    681
    Barry Schwarz
    Nov 19, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    509
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page