473,480 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3942
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
8683
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
1613
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
2514
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
9479
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
5747
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
6931
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
3214
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
2245
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
3787
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
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7087
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.