473,246 Members | 1,627 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Pointer to Function Inside Struct using Static

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
Jul 23 '05 #1
1 3919
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
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to...
3
by: Jeremy Beasley | last post by:
I'm having a problem using pointers to structures. One method of declaring point and then referencing a structure works, while the other generates an error at compile time. -----------------...
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
1
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this...
9
by: mailtogops | last post by:
Hi, I have this question in mind for many years.. C++ class provides encapsulation which encapsulate its members and member function. class Experiment { private:
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.