473,395 Members | 1,464 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,395 software developers and data experts.

The dreaded question

I don't care if its bad C++, I really dont. I need to convert from
a member function pointer to a "void*" with zero computational
overhead.

I have the perfect way to do it right here, but i'm having troubles
because if the member function pointer i pass to the function is
not void with no params, I get a casting error.

Any ideas how to do this?

caustik

// ************************************************** ****************
// * Take THIS C++ !!
// ************************************************** ****************
template <class BaseClass> inline void *MFPtoFP( void
(BaseClass::*pMemFunc)(void) )
{
union
{
void BaseClass::*pMemFunc;
void *pFunc();
}
ThisConv;

ThisConv.pFunc = pMemFunc;

return ThisConv.pFunc;
}
Jul 19 '05 #1
28 2421
"caustik" <ca*****@nospam.com> wrote...
I don't care if its bad C++, I really dont. I need to convert from
a member function pointer to a "void*" with zero computational
overhead.
A pointer to member cannot be converted to void*. Period. It's
not "bad C++", it's "not C++". Such conversion does not exist
in C++.
I have the perfect way to do it right here, but i'm having troubles
So, if you're "having troubles", how can it be "perfect"?
because if the member function pointer i pass to the function is
not void with no params, I get a casting error.

Any ideas how to do this?
There is no way.

Now, let me ask you a question. Why do you think you need that?

caustik

// ************************************************** ****************
// * Take THIS C++ !!
// ************************************************** ****************
template <class BaseClass> inline void *MFPtoFP( void
(BaseClass::*pMemFunc)(void) )
{
union
{
void BaseClass::*pMemFunc;
void *pFunc();
You could declare this as

void (*pFunc)();

but it's _not_ going to solve the conversion problem, trust me.
It will probably get rid of the "casting error", as you put it,
but it's not going to give the right answer.
}
ThisConv;

ThisConv.pFunc = pMemFunc;

return ThisConv.pFunc;
}


Victor
Jul 19 '05 #2
"caustik" <ca*****@nospam.com> wrote in message
news:bd**********@eeyore.INS.cwru.edu...
I don't care if its bad C++, I really dont.


Well, then, I don't care if it works. I really don't.

lol

See Victor's answer.

Cy
Jul 19 '05 #3
A pointer to member cannot be converted to void*. Period. It's
not "bad C++", it's "not C++". Such conversion does not exist
in C++.
Damn, you know that I stated I dont care. There are tricks to do it,
and I dont give a flying crap if they arent technically legal.
I have the perfect way to do it right here, but i'm having troubles


So, if you're "having troubles", how can it be "perfect"?


Well, fool, the method is "perfect" because it works 100% of the time
in my program. I happen to know the system that my program is compiled
and executed on, so I can take advantages of facts about that system.

I suppose I am ignorant to dare to optimize my program.
because if the member function pointer i pass to the function is
not void with no params, I get a casting error.

Any ideas how to do this?
There is no way.


Bullshit, I did it already.

Now, let me ask you a question. Why do you think you need that?


Let me rephrase your question: Why do you [smart ass remark] need that?

The reason is that I have a massive array of generic pointers to functions,
and their respective "Wrapper" functions which are called when the binary
code for these intercepted functions is executed. The problem is that some
of these functions are "__thiscall", and the intercepted code must take this
into account.

Believe it or not, not everbody's C++ program can be efficient while obeying
every single anal object oriented design rule on the planet. I am
interfacing
with already-compiled C++ code, so it takes some "Hacks" to get it working
efficiently and without looking like utter garbage.

After several years of reading this newsgroup, I've noticed there is some
sort
of deep rooted psychological problem with some of you guys. You'd sooner
spend 30 minutes typing a lengthy "I'm above everybody" post to respond to
peoples slightly offtopic or anal-incomplatible questions, instead of
posting a
quick solution and saying "note that this is a hack". Its horrible.
Jul 19 '05 #4
P.S. This works *Pefectly* (on my x86/win32 system, which is all i'm
targetting) :

// ************************************************** ****************
// * Take THIS C++ !!
// ************************************************** ****************
template <class BaseClass, typename MFT> inline void *MFPtoFP( MFT pMemFunc)
{
union
{
MFT pMemFunc;
void (*pFunc)();
}
ThisConv;

ThisConv.pMemFunc = pMemFunc;

return ThisConv.pFunc;
}
Jul 19 '05 #5
"caustik" <ca*****@nospam.com> wrote in
news:bd**********@eeyore.INS.cwru.edu:
P.S. This works *Pefectly* (on my x86/win32 system, which is all i'm
targetting) :


Since you know your target platform, why don't you take your question over
to a newsgroup that caters to your target platform (and since you
apparently already know that it cannot be done in Standard C++)? I'd
suggest one of the newsgroups on Microsoft's news server probably being the
most topical.
Jul 19 '05 #6

"Howard Hinnant" <hi*****@metrowerks.com> wrote in message
news:020720031926481030%hi*****@metrowerks.com...
In article <bd**********@eeyore.INS.cwru.edu>, caustik
<ca*****@nospam.com> wrote:
[...]
If you really want to do this, C++ offers a far easier route. You can
convert anything to anything, with zero computational cost:

#include <vector>
#include <list>
#include <string>

template <class T, class U>
inline
T just_do_it(const U& u)
{
return *(T*)(&u);
}

int main()
{
std::vector<int> v =
just_do_it<std::vector<int> >(std::list<std::string>());
}

... not that I'm recommending it mind you. Here's the rope ...


I would recomment much more safe and effective solution to "convert"
anything
to void pointer:

#define convert_to_pointer(argument) ((void *)0)

regards,
Michael Furman


Jul 19 '05 #7
Something that calls itself caustik wrote:
I don't care if its bad C++, I really don't. I need to convert from
a member function pointer to a "void*" with zero computational
overhead.

I have the perfect way to do it right here, but I'm having troubles
because if the member function pointer i pass to the function is
not void with no params, I get a casting error.

Any ideas how to do this?

caustik

// ************************************************** ****************
// * Take THIS C++ !!
// ************************************************** ****************
template <class BaseClass> inline void *MFPtoFP( void
(BaseClass::*pMemFunc)(void) )
{
union
{
void BaseClass::*pMemFunc;
void *pFunc();
}
ThisConv;

ThisConv.pFunc = pMemFunc;

return ThisConv.pFunc;
}


This is an obvious troll. Please ignore it.

Jul 19 '05 #8
E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:3F**************@jpl.nasa.gov...
This is an obvious troll. Please ignore it.


I don't know if it's a troll or not, but it clearly wasn't obvious to all
those who answered the question.

Is it just the email address that makes it a troll?

Just wondering.

David

Jul 19 '05 #9
On Wed, 2 Jul 2003 15:48:37 -0700, "caustik" <ca*****@nospam.com>
wrote in comp.lang.c++:
A pointer to member cannot be converted to void*. Period. It's
not "bad C++", it's "not C++". Such conversion does not exist
in C++.


Damn, you know that I stated I dont care. There are tricks to do it,
and I dont give a flying crap if they arent technically legal.
I have the perfect way to do it right here, but i'm having troubles


So, if you're "having troubles", how can it be "perfect"?


Well, fool, the method is "perfect" because it works 100% of the time
in my program. I happen to know the system that my program is compiled
and executed on, so I can take advantages of facts about that system.


If you know, and don't care, that's is not legal C++, and yet you post
it in comp.lang.c++ and display an insulting attitude to those who
tell you is it not legal C++, you are the one with the psychological
problem.

*PLONK*

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #10
David White wrote:
E. Robert Tisdale wrote:
This is an obvious troll. Please ignore it.


I don't know if it's a troll or not,
but it clearly wasn't obvious to all those who answered the question.

Is it just the email address that makes it a troll?

Just wondering.


I was simply expressing my opinion.
I look for the typical signs:

1. a troll handle
caustik,
2. a forged or disposable email account
nospam.com,
3. an emotionally provocative subject
The dreaded question
4. the original poster does not participate in the discussion
(except, possibly, to abuse the respondents)

There is seldom a good reason why a legitimate post
to a technical newsgroup like comp.lang.c++
should evoke a strong emotional response in any subscriber.
If it does, you should suspect a troll.
Real people use real names and post from legitimate ISPs.
Trolls use handles and post from disposable email accounts
or just forge the the address so you can't trace them.
Be vigilant. Learn to recognize trolls
and don't respond to them.

Jul 19 '05 #11


caustik wrote:
Damn, you know that I stated I dont care. There are tricks to do it,
and I dont give a flying crap if they arent technically legal.

Yeah . . . I going to need you to go into my killfile. Yeah . . . move
all the way to the back. Thanks, that'll be great.


Brian Rodenborn
Jul 19 '05 #12
On Wed, 2 Jul 2003 15:11:09 -0700, "caustik" <ca*****@nospam.com>
wrote:
I don't care if its bad C++, I really dont. I need to convert from
a member function pointer to a "void*" with zero computational
overhead.
sizeof memfunptr on VC++ can be 4, 8 or 12 (and possibly bigger?),
depending on the function and class in question. Size of void(*)() is
4. As you can see, a non-lossy conversion is impossible in general.

I have the perfect way to do it right here, but i'm having troubles
because if the member function pointer i pass to the function is
not void with no params, I get a casting error.


It isn't perfect even if you fix it - if the classes involved have
virtual base classes, or you use multiple inheritence, then you'll get
a crash. If they don't, you may be ok with your current version, but
it may of course start crashing next time you upgrade, or if you
switch to another compiler.

Tom
Jul 19 '05 #13

caustik wrote:

I don't care if its bad C++, I really dont.
Yeah. But it *might* not work even on "x86/win32 system", believe me.
I need to convert from
a member function pointer to a "void*" with zero computational
overhead.
Stay away from "void*".

I have the perfect way to do it right here, but i'm having troubles
because if the member function pointer i pass to the function is
not void with no params, I get a casting error.

Any ideas how to do this?


Sure. http://groups.google.com/groups?selm...5B66D%40web.de

regards,
alexander.
Jul 19 '05 #14

Default User wrote:
[...]
Yeah . . . I going to need you to go into my killfile. Yeah . . . move
all the way to the back. Thanks, that'll be great.


I'm just curious: how BIG is your killfile? Well, I'm asking because
I'd be interested to buy a copy of it... if it's "big enough", so to
speak. ATTENTION: this "offer" is valid for all c.l.c++'s "plonkers"
from The United States (only) and will last until the end of The 226th
Independence Day (in all US time zones... uhmm, except Alaska, Hawaii,
and Samoa, sorry for that). Hurry up and act NOW! I need _only_one_
copy -- the BIGGEST one.

regards,
alexander.

P.S. Default, did you appreciate "the e-greetings" from Berlin City
Dump... did it arrive yet?
Jul 19 '05 #15
caustik wrote:
I don't care if its bad C++, I really dont. I need to convert from
a member function pointer to a "void*" with zero computational
overhead.


Why?

Answering why might lead us to a suggestion that's good for your code.

(BTW the answer is: put the member function pointer into a structure, take
the address of the structure, and cast away. Good luck not crashing.)

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces

Jul 19 '05 #16

"caustik" <ca*****@nospam.com> wrote in message news:bd**********@eeyore.INS.cwru.edu...
P.S. This works *Pefectly* (on my x86/win32 system, which is all i'm
targetting) :

How can it work perfectly? On VC++ the sizeof(MPT) is some where
between 2 and 4 times the sizeof (void*)?
Jul 19 '05 #17
E. Robert Tisdale wrote:
I was simply expressing my opinion.
I look for the typical signs:

1. a troll handle
caustik,
2. a forged or disposable email account
nospam.com,


Well, they can try to anyway... ;-)

NNTP-Posting-Host: sandiego.divxnetworks.com
X-Trace: eeyore.INS.cwru.edu 1057183867 12825
207.67.92.110 (2 Jul 2003 22:11:07 GMT)
X-Complaints-To: ab***@po.cwru.edu
NNTP-Posting-Date: 2 Jul 2003 22:11:07 GMT
NNTP-Posting-User: ac***@sandiego.divxnetworks.com

In answer to the actual question...

If performance is so much of an issue that it becomes socially
acceptable to insult those that might try to help (even before asking
the question), then what's wrong with just coding an ASM var push/pop or
register move and be done with it? *That* will convert most anything! ..
and also ignore all language usage restrictions (aka. protections) in
the process. IMHO one beauty in the C++ language is that it _has_ these
restrictions, and all for good reason! To prevent someone from abusing
the usage of known types in a manner which is undefined or incorrect.

On the other hand my glass is always half full, and given the latest
description of the problem from the original poster perhaps we should
discuss the correct manner to address his issues rather than getting so
wrapped up in the emotionally charged aspects of it. Do we have enough
to go on for a C++ design discussion here? Can anyone out there suggest
a *better solution*[tm] USING the C++ language instead of trying to
fight against it(e.g. unions of pointers)? I'm sure that others out
there who are learning C++ might benefit from a discussion of C++ design
rather than a food fight (lol).

Jul 19 '05 #18


Alexander Terekhov wrote:

Default User wrote:
[...]
Yeah . . . I going to need you to go into my killfile. Yeah . . . move
all the way to the back. Thanks, that'll be great.


I'm just curious: how BIG is your killfile?


Don't worry Al, there's a place for you if you really need it.

Brian Rodenborn
Jul 19 '05 #19


"E. Robert Tisdale" wrote:
There is seldom a good reason why a legitimate post
to a technical newsgroup like comp.lang.c++
should evoke a strong emotional response in any subscriber.
If it does, you should suspect a troll.

Well, I feel that way about many of your posts, especially on
comp.lang.c.


Brian Rodenborn
Jul 19 '05 #20

Default User wrote:

Alexander Terekhov wrote:

Default User wrote:
[...]
Yeah . . . I going to need you to go into my killfile. Yeah . . . move
all the way to the back. Thanks, that'll be great.


I'm just curious: how BIG is your killfile?


Don't worry Al, there's a place for you if you really need it.


For free?

regards,
alexander.
Jul 19 '05 #21
template <class T> inline void* MFPtoFP (T p) { return
reinterpret_cast<void*>(p); }

or maybe even do away with type-foo like so:

#define MFPtoFP(p) reinterpret_cast<void*>(p)

or some such variant seems to be what you want. Just make sure only to
instantiate it with pointer types. You could be losing information, though,
because pointers to member functions (or even to normal functions) can be
larger than pointers to data.

- Jeff.

"caustik" <ca*****@nospam.com> wrote in message
news:bd**********@eeyore.INS.cwru.edu...
I don't care if its bad C++, I really dont. I need to convert from
a member function pointer to a "void*" with zero computational
overhead.

I have the perfect way to do it right here, but i'm having troubles
because if the member function pointer i pass to the function is
not void with no params, I get a casting error.

Any ideas how to do this?

caustik

// ************************************************** ****************
// * Take THIS C++ !!
// ************************************************** ****************
template <class BaseClass> inline void *MFPtoFP( void
(BaseClass::*pMemFunc)(void) )
{
union
{
void BaseClass::*pMemFunc;
void *pFunc();
}
ThisConv;

ThisConv.pFunc = pMemFunc;

return ThisConv.pFunc;
}

Jul 19 '05 #22

Default User wrote:

Alexander Terekhov wrote:

Default User wrote:

Don't worry Al, there's a place for you if you really need it.


For free?


I didn't charge you for the cheese, did I?


Cheese? I had to pay for garbage recycling.

regards,
alexander.
Jul 19 '05 #23

E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote in message
news:3F**************@jpl.nasa.gov...
Something that calls itself caustik wrote:
I don't care if its bad C++, I really don't. I need to convert from
a member function pointer to a "void*" with zero computational
overhead.

I have the perfect way to do it ^^^^^

right here, but I'm having troubles
^^^^^^^
because if the member function pointer i pass to the function is
not void with no params, I get a casting error.
Then is your way really 'perfect' after all?

Any ideas how to do this?


How about your 'perfect' way?

-Mike

Jul 19 '05 #24

Mike Wahler wrote: [...]

Whaler, you did it much better last year. Gee, that was fun...
can we do this again?

http://google.com/groups?selm=ag217d...mindspring.net

**********
Just a quick note to wish all my American friends and
acquaintances here a happy, safe Independence Day.

Between the festivites, fireworks, backyard barbeques, etc.,,
please take a moment to acknowledge the extraordinary integrity,
courage, and achievements of those who risked and gave their
lives so long ago in order to enable us to live as free people.

And to those of you from other various places around
the globe, a pleasant day and a happy, prosperous life
to you as well.

It's an honor and a pleasure to know and interact with
all of you.

-Mike

"I have never been able to conceive how any rational being could
propose happiness to himself from the exercise of power over
others." --Thomas Jefferson to A. L. C. Destutt de Tracy, 1811
**********

regards,
alexander.

P.S. Go and celebrate, chap. Don't waste your time here.
Jul 19 '05 #25


Alexander Terekhov wrote:

Default User wrote:

I didn't charge you for the cheese, did I?


Cheese? I had to pay for garbage recycling.

What?! That was the finest genetically engineered Merican cheese food
substitute. All you had to do was unwrap it and place it on the ground,
it would have dissolved a tunnel straight down at least 500 feet (that's
like 29657.222 kizzometrics or whatever weird measurements you use over
there). No disposal costs were required at all.

You don't know much about cheese.


Brian Rodenborn
Jul 19 '05 #26

Default User wrote:

Alexander Terekhov wrote:

Default User wrote:

I didn't charge you for the cheese, did I?


Cheese? I had to pay for garbage recycling.


What?! That was the finest genetically engineered Merican cheese food
substitute. All you had to do was unwrap it and place it on the ground,
it would have dissolved a tunnel straight down at least 500 feet (that's
like 29657.222 kizzometrics or whatever weird measurements you use over
there). No disposal costs were required at all.

You don't know much about cheese.


I don't pretend to know much about it. I've just heard that
cheese was invented by a wandering slave who was carrying
milk across the Great American Desert in a saddlebag made
from an Alligator's stomach. American product.

regards,
alexander.
Jul 19 '05 #27
On Mon, 7 Jul 2003 16:29:01 -0400, "Marduk"
<gu************@sympatico.ca> wrote:
Yup, the same guy invented fire in the early 1920's


Al Gore was alive and walking then?

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
Jul 19 '05 #28
>>Yup, the same guy invented fire in the early 1920's

Al Gore was alive and walking then?


Oh yah. But these days he's just walking.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
Jul 19 '05 #29

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

Similar topics

4
by: Meyer1228 | last post by:
You all helped me with function keys last week. Thanks so much; your advice was right on. My question now - does anyone have a function that I can use to test my sql statements for the...
0
by: Alexander Stippler | last post by:
I've got an inheritance structure with two coupled "dreaded diamonds" like shown below: A / \ / \ B C \ / \ \ / \ D E \ /
1
by: Alexander Stippler | last post by:
Hello, I want to build up an inheritance structure looking like two coupled dreaded diamonds. It's similar to an old posting of me. Now my question is, where to use the keyword virtual. CV /...
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
24
by: songie D | last post by:
Can managed C++ be trusted to handle the garbage collector correctly in the right bit if I have a project with unmanaged and managed parts in it?
8
by: Dynamo | last post by:
Hi again, I am constructing a site on a small scale where people can buy nik naks and pay for them via paypal. Everything works fine unless there is only one of an item left for sale. What if 2...
2
SamKL
by: SamKL | last post by:
Fairly new at converting over to using non-table layouts, but I've been working at this one particular issue for over 6 hours now with no real perceivable results... The page in question is W3C...
36
by: bmyers | last post by:
Good afternoon, I am attempting to count only those records within a report, which is based on a query, where Status is equal to Closed. I have tried multiple variations of DCOUNT but am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.