473,883 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
+ 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.memberf unction()" 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
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"


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

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;

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;

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;

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;

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;

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;

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;

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

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

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

void main (void)

// MPU.Run2();

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

Error compiling
D:\Test\Load.cp p(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.cp p(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.cp p(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.cp p(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.cp p(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.cp p(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.cp p(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
Error executing cl.exe.

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

_______________ _______________ _____________
Need to fix error like this below.
void (_MPU::*Run2[2056])(void) =
_______________ _______________ ________

Bryan Parkoff
Jul 23 '05 #1
1 3986
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".

Jul 23 '05 #2

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

Similar topics

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 use it with. Now, if that T happens to be some subclass of a known base class (object, in this case), I want to perform some extra stuff ... I've read Faq#35, and the most natural solution would have
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. ----------------- ex1. (this method works) struct data { int a,b; } myData;
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 going to hell just by using dlopen()/LoadLibrary() respectively, I'm still trying to get it as clean as possible. I have a number of different quantums of data and a number of plugins. Since any plugin can (and possibly will) touch any quantum...
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 worked pretty well, accepting to write unsafe code ;-) But my next struct has an array inside and I don't get it passed over to the DLL correctly. Here my struct in c#:
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:
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 I'm defining the functions as void*, and then casting when I use them. This code is going into a general purpose framework, and it would be much nicer if the user didn't need to do any casting. Can someone tell me how to set up those typedefs...
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: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
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 to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
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 event); so I declared a function pointer like typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.