Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2006, 08:34 PM   #1 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 735
DJMaze is on a distinguished road
C++ Trick: pointer to dynamic class method

By default you can only make a pointer to a function, like in C:

PHP Code:
typedef float (*MyFuncPtrType)(intchar *);

MyFuncPtrType my_func_ptr some_func;

// call it like some_func(7, "Arbitrary String");
(*my_func_ptr)(7"Arbitrary String"); 
Borland C++ Builder allowed me to also use any class in there using __closure, but now that I moved to ISO C++ this is not available anymore unless i explicity name the class inside the typedef like:
PHP Code:
typedef float (SomeClass::*my_memfunc_ptr)(intchar *); 
After some googling i found several solutions to this problem in toolkits like Boost but also in some documentations.

The bug exists still as of today although several compilers have a solution and others live with the bug for over 15 years now, since the original author already pointed a solution more then 15 years ago.

But now you want to know "what is the solution?". Well i can tell you that in one simple line:

http://www.codeproject.com/cpp/FastDelegate.asp

I found this nifty pack after reading many google results and it seems to comply with most common used compilers.
C++ will now even more rock your world

How i use this functor? Well some functions need to be private or protected BUT have to be called thru a Windows procedure like the ThreadProc or WindowProc.

Why? Well simple, Windows procedures are C so they can't deal with C++ nor does the X system.

A good example is multi-threaded applications, each new thread calls a procedure to run inside the thread. In my case this is inside a class and the class function that starts it is "run()" and is protected so that you can't run the class outside the thread (talking fail-safe or application locking prevention here)

I will post my Thread class for Windows/X when i get them properly working as i want it including synchronization
DJMaze is offline   Reply With Quote
Old 03-20-2006, 03:22 AM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
In other words, "In your face, Compiler!"

Seems like you could also define a Template that did the right thing with regards to the proper class to do this as well.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class using a specific instance of another class? is this possible? abs Standard C, C++ 5 02-08-2005 04:12 PM
pointer to function with class? Kportertx Standard C, C++ 5 04-11-2003 06:12 AM


All times are GMT -8. The time now is 04:40 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting