How to declare a pointer to a member function of a class?

Hi to all,

How can I declare a pointer to a method of a class? I'm trying to do the 
following but the compiler 

doesn't like it and gives me the following error:

*****
error C2440: '=' : cannot convert from 'void (__thiscall CMyDlg::*)(class 
CString &)' to 'void 

(__cdecl *)(class CString &)'
        There is no context in which this conversion is possible
*****


What would be the right way to declare the function pointer?

void CMyDlg::funct1(CString &s)
{
    s = "Funct One";
}

void CMyDlg::funct2(CString &s)
{
    s = "Funct Two";
}

void CMyDlg::Test(bool bCondition)
{
    void (* pFunct)(CString &);
    CString s;

    pFunct = (bCondition == true) ? funct1 : funct2;
    pFunct(s);
}

Thank you,
Geo
0
Geo (129)
11/29/2005 4:04:03 PM
vc.mfc 33608 articles. 0 followers. Follow

2 Replies
1159 Views

Similar Articles

[PageSpeed] 50

void CMyDlg::Test(bool bCondition)
{
    void (CMyDlg::* pFunct)(CString &);
    CString s;

    pFunct = (bCondition == true) ? &CMyDlg::funct1 : &CMyDlg::funct2;
    (this->*pFunct)(s);
}

Although in this case, you better just write as:

void CMyDlg::Test(bool bCondition)
{
    CString s;

    if (bCondition)
    {
        funct1(s);
    }
    else
    {
        funct2(s);
    }
}

"Geo" <Geo@discussions.microsoft.com> wrote in message 
news:4920EEC5-BA4C-4061-883D-B966F9BC485C@microsoft.com...
> Hi to all,
>
> How can I declare a pointer to a method of a class? I'm trying to do the
> following but the compiler
>
> doesn't like it and gives me the following error:
>
> *****
> error C2440: '=' : cannot convert from 'void (__thiscall CMyDlg::*)(class
> CString &)' to 'void
>
> (__cdecl *)(class CString &)'
>        There is no context in which this conversion is possible
> *****
>
>
> What would be the right way to declare the function pointer?
>
> void CMyDlg::funct1(CString &s)
> {
>    s = "Funct One";
> }
>
> void CMyDlg::funct2(CString &s)
> {
>    s = "Funct Two";
> }
>
> void CMyDlg::Test(bool bCondition)
> {
>    void (* pFunct)(CString &);
>    CString s;
>
>    pFunct = (bCondition == true) ? funct1 : funct2;
>    pFunct(s);
> }
>
> Thank you,
> Geo 


0
alegr (1131)
11/29/2005 4:23:52 PM
Thank you Alexander. Actually, my Test() method is much more complicated than 
this :)

Thanks again,
Geo 

"Alexander Grigoriev" wrote:

> void CMyDlg::Test(bool bCondition)
> {
>     void (CMyDlg::* pFunct)(CString &);
>     CString s;
> 
>     pFunct = (bCondition == true) ? &CMyDlg::funct1 : &CMyDlg::funct2;
>     (this->*pFunct)(s);
> }
> 
> Although in this case, you better just write as:
> 
> void CMyDlg::Test(bool bCondition)
> {
>     CString s;
> 
>     if (bCondition)
>     {
>         funct1(s);
>     }
>     else
>     {
>         funct2(s);
>     }
> }
> 
> "Geo" <Geo@discussions.microsoft.com> wrote in message 
> news:4920EEC5-BA4C-4061-883D-B966F9BC485C@microsoft.com...
> > Hi to all,
> >
> > How can I declare a pointer to a method of a class? I'm trying to do the
> > following but the compiler
> >
> > doesn't like it and gives me the following error:
> >
> > *****
> > error C2440: '=' : cannot convert from 'void (__thiscall CMyDlg::*)(class
> > CString &)' to 'void
> >
> > (__cdecl *)(class CString &)'
> >        There is no context in which this conversion is possible
> > *****
> >
> >
> > What would be the right way to declare the function pointer?
> >
> > void CMyDlg::funct1(CString &s)
> > {
> >    s = "Funct One";
> > }
> >
> > void CMyDlg::funct2(CString &s)
> > {
> >    s = "Funct Two";
> > }
> >
> > void CMyDlg::Test(bool bCondition)
> > {
> >    void (* pFunct)(CString &);
> >    CString s;
> >
> >    pFunct = (bCondition == true) ? funct1 : funct2;
> >    pFunct(s);
> > }
> >
> > Thank you,
> > Geo 
> 
> 
> 
0
Geo (129)
11/29/2005 5:31:13 PM
Reply:

Similar Artilces:

Convert Function to Sub
I have the following function which is used to extract the numeric and alpha characters from a cell. Can someone help me 'convert' this to a sub, so that, for example, with whatever range the user selects, the code will look at each cell and basically remove everything that is not either alpha or numeric? Example: 45 BJ}!12T would be converted to 45BJ12T Public Function ExtractNT(TextString As String) As String Dim x As Long Dim sChar As String ExtractNT = vbNullString For x = 1 To Len(TextString) sChar = Mid(TextString, x, 1) If sCh...

calling function in another UI thread
I created another UI thread from the main thread using AfXBeginThread. I defined a new function in the new UI thread. Can I directly call this function in the main thread, or do I have to PostThreadMessage? If I can directly call the function, what kind of IPC mechanism is it? I am little confused. Thank you! class CNewUIThread : public CWinThread { ..... void MyNewFunc() { .... } ......... } a thread does not have functions, it is a sequence of instructions, usually including a loop that processes messages or does some other computation until it either finishes its work or is killed by...

"Change Case" function missing?
I had to translate part lists in Excel (lower case letters). Now I was told to change them all to upper case letters. Where does Excel 2003 have this function? Right now I need to copy consecutive blocks of text into MS Word, change the case, then copy these files back into Excel... I hope that this is possible since I seem to get these kind of work continuously. Typing in upper case letters is no fun and it's hard to read. Thanks, Murgi ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World!...

sum function error
Hi sorry if this is a repost. My last effort seems to not show in the list so here goes again. I have a main form and it contains a subform on the subforms footer it has a sum function to calculate the total. =Sum([SPQty]*[SPUnitPrice]) somtimes this produces an error and goes on endlessly calculating for some reason I cannot figure out. Mostly it works fine but once the error happens I have to close the program to make it work again. if I use the subform on its own and not within the main form the sum works fine every time. any help most apreciated as its so strange. IM using Vista and ac...

how to change a cell content in a visual basic function?
myfunction() is a function i called from, lets say, cell C3 as: = myfunction() in myfunction() i want to change a cells value: ...... Cells(8, 1) = 3 ( or Cells(8, 1).Value = 3 dunno which is true:) ..... myfunction= 123 ..... but i get nothing, cos this is a function but not a sub, i guess. well, how to do this? Indeed it is not possible to change worksheets from within a function. Use a SUB instead. And, before you ask, no, it is not possible to trick Excel and call a sub from the function to change a cell or anything like that. -- Kind Regar...

RPC over HTTP domain members only?
Does the XP machine have to be a domain member? I tell you, I have luck only 50% of the time with RPC over HTTP. I'm still looking for that one, golden document which covers any and all scenarios and makes it work every time! Thanks No it does not. "you know who maybe" <nguser2u@spamnotAOL.com> wrote in message news:110l7kjsmkf5d28@news.supernews.com... > Does the XP machine have to be a domain member? > > I tell you, I have luck only 50% of the time with RPC over HTTP. I'm > still looking for that one, golden document which covers any and all ...

Address Allow And Deny to certain members.
I have a series of address lists which i made up for each department in the school i.e. "English", "Maths". These are there for all member of staff to view. I have two Objects one is called "Students" and the other is "Teachers". I want to make sure that the students cant search in any of these Address Lists and i want to make sure that they cant search in the GAL. Is there a way of stopping this? i have tried to Deny Access to the GAL and the Address Lists but it has had no effect. Many Thanks Lee J ...

CRichEditCtrl derievd class
Dear All, I have derieved my own Class from CRichEditCtrl in an Dialog base Application ANd i was lookin to Draw an Tracker in this RichEditCtrl.. Which i have achieved through some GDI work in OnPaint()... But afdter these i aws not able to see my Text innthis Ctrl... And at last i found that whenever we override the WM_PAINT handler in CRichEditCtrl derieved class..the text written in this ctrl become invisible... even i have checked the foreGround Color which is black... and as i deleted this WM_PAINT it start visible... But in my requierment i need to override WM_PAINT... Looking fo...

Error Message R6025
I´m getting the error message (-R6025 - pure virtual function call-), and it shuts money down every time I try to import a statement downloaded from bank. It says the error is on the money.exe file. What do I do? Reinstall? In microsoft.public.money, Trevisan BR wrote: >I�m getting the error message (-R6025 - pure virtual function call-), and it >shuts money down every time I try to import a statement downloaded from bank. > >It says the error is on the money.exe file. > >What do I do? > >Reinstall? See if this helps: http://support.microsoft.com/kb/240437...

Custom Functions: Order Of Execution?
I finally figured out that the reason I couldn't find the source code in a .XLA file was that .XLA's are compiled versions of .XLS'. Now I've tracked down the .XLS file, but can't figure out how to follow the coding. Check out http://tinyurl.com/3obxl I'm 99.99% sure that the entry point is the cell "ROR"...but the rest escapes me. Could somebody offer a general view of what's going on here? Not necessarily the precise logic - more the order in which things happen so I can pick it apart on my own. -- PeteCresswell ...

class wizard belives group box is a button
I use MS Visual C++, Version 6.0. Since I tried to use a component one number control (ActiveX) in my project, which brought the whole Studio 6.0 to crash, i see a strange behaviour. Whenever I drag and drop a group box control from the controls toolbar onto a dialog resource and then try to define a control variable with ClassWizard, it is handled as a button control. How can I re-teach my MSVC++ that the group box is a static, not a button? Any help would be appreciated. How do i get rid of the component one control icon in the control toolbar? Where is the definition file of the stan...

Can't Change Vendor Class
Hello everyone On GP 10 when we try to change the Vendor Class in the Vendor Card, we receive the following error: [Microsoft][ODBC SQL Server Driver][SQL Server]You can't delete data from this table [Microsoft][ODBC SQL Server Driver][SQL Server]The transaction ended in the trigger. The batch has been aborted. We already try in a machine without 3rd part products installed and the result is the same. Someone has experienced this problem? Hi Nancy, It looks like a third-party product has added a trigger to SQL database itself, which is why the error still occurred on a workstation w...

Out of office is not functioning
We have Exchange2003 and Win2003 running. The client is using office2003. I just noticed that our "out of office" is not working for external email message, but works OK internally. Any idea? Thanks! It's turned off by default, else you'd be sending OOOFs to spammers. :) Turn on from Global Settings | Internet Message Formats | Default (*) | Properties | Advanced | Allow Out of Office Responses - to turn it on for the whole world. Ideally, you may want to create separate settings (containers) for specific domains (important customers, etc.?) and only turn on OOOFs f...

forecast function
Is there a way to use forecast function(x;y_known;x_known) defining x_known and y_known as two rows of values increasing from 0 until a maxvalue and then going back to 0, by stopping the forecasting in the interval 0,max? x_known =A1:Amax y_known B1:Bmax ...

How to get the main frame window from a doc class?
Hi, there, I have a SDI application, and would like to create a working thread when loading the doc class. To send message to the main frame window to show what is going on, I have to wrap the main frame window's pointer or handler to the working thread. I used following code to wrap the pointer: BOOL CMyDoc::OnNewDocument() { THREADPARMS* ptp = new THREADPARMS; ptp->hWnd = ::AfxGetMainWnd(); //* AfxBeginThread(Get_Init_Rects, ptp); } ptp->hWnd is a CWnd* data. It get always NULL here. I tried to use CMainFrame* pWnd = (CMainFrame*)AfxGetMainWnd(); // failed, get ...

Add custom User Defined Function to Report Writer report
Is it possible to do this? I have created a UDF in SQL2005 that returns the value that I want to bring onto a report in GP10's RW. Thanks Les ok.. I think I have learnt since posting that the UDF is a dexterity UDF, not a SQL UDF that Report Writer can reference. If so, forget it.. I'll use a Crystal Report or SQL RS report. Tx Les "LesWright" wrote: > Is it possible to do this? > > I have created a UDF in SQL2005 that returns the value that I want to bring > onto a report in GP10's RW. > > Thanks > > Les You could use VBA to call y...

" " means in IF function
I'm trying to understand what does this mark mean " " in IF function (Logical test). This has been done in many Excell worksheets but I could not find the author to ask him. If any one can understand the meaning from the examples below, please let me know or provide any helpfull material. =IF(B8-$D$4>0.05,B8-$D$4,IF(A8="","",0)) =IF(E8="","",IF(ABS(D8-E8)<0.05,0,D8-E8)) Thanks The test A8="" is simply asking whether or not there is anything in that particular cell. -- Andy Wiggins FCCA www.BygSoftware.com Excel, Acce...

Date Function Question
Anyone out there know how to program a date function which allows me to input a starting date and a number of days wherein the result of the ending date will be calculated? Example: 4/15/99 + 145 days = ____ X Date__. Thanks for any suggestions. JEFF ...

ProgID & CLSID API function
Hello, Is there a direct function (possibly Win API) to retrieve or check the existence of ProgID or CLSID from the registry? Thanx See ProgIDFromCLSID and CLSIDFromProgID. This makes me thankful I do not program on Unix - grep anyone? - Tim "ajak" <razak@mmsc.com.my> wrote in message news:eqE7ZNwQEHA.1312@TK2MSFTNGP12.phx.gbl... > Hello, > > Is there a direct function (possibly Win API) to retrieve or check the > existence of ProgID or CLSID from the registry? > > Thanx > > ok, thanks "Tim" <Tim@NoSpam> wrote in message ne...

Turning Off Error Reporting using API Functions and VB
We want to automatically turn off the error reporting options using Windows API functions for the following functions area Click on Start, Settings, Control Panel, System, the Advanced tab and Error Reporting (at the bottom of the screen), which launches the Error Reporting screen. Another way to get to that screen: Open My Computer, right-click on Properties, click on Advanced tab and Error Reporting. Any assistance would be welcome. It's not a program's call. It's the user's decision. --=20 -------------------------------------------------------------------------= --...

Excel function to do this?
Hi, Say I have this: A B 1 1 1 2 3 5 2 7 4 11.....and so on How can I produce column B (e.g., B5=A5+B1+B2+B3+B4)? Thanks, Mike In article <5ff86e7e-4215-4fde-85d4-9fe110a658e2 @l24g2000vby.googlegroups.com>, mas_it_2000@yahoo.com says... > > Hi, > Say I have this: > > > A B > 1 1 > 1 2 > 3 5 > 2 7 > 4 11.....and so on > > > How can I produce column B (e.g., B5=A5+B1+B2+B3+B4)? Type =SUM(A$1:A1) in cell B1, and then drag the formula down as far as you need. Dollar signs in formulae force absolu...

What's wrong with this function??
=IF(F8="",0,=F8-D8) Hi, change it to =IF(F8="",0,F8-D8) -- If this helps, please click the Yes button. Cheers, Shane Devenshire "GEM" wrote: > =IF(F8="",0,=F8-D8) Maybe... =IF(F8="",0,F8-D8) or =if(count(f8,d8)<2,0,f8-d8) GEM wrote: > > =IF(F8="",0,=F8-D8) -- Dave Peterson >=IF(F8="",0,=F8-D8) Remove the 3rd = sign: =IF(F8="",0,F8-D8) -- Biff Microsoft Excel MVP "GEM" <GEM@discussions.microsoft.com> wrote in message news:1131A0D6-1AE7-48F0-9B7F-D5A762545A56@...

'CObject::operator =' : cannot access private member declared in class 'CObject'
Hello! I'm trying to implement a simple representation of a graph. Here is the code: //--------vertex.h------------ #pragma once class CVertex : public CObject { public: CVertex(); virtual ~CVertex(); protected: int m_xPos; int m_yPos; CPtrList m_neigbors; public: virtual int const GetXPos(void); virtual int const GetYPos(void); virtual void SetXPos(int newXPos); virtual void SetYPos(int newYPos); virtual void AddNeighbor(CVertex* vertex); virtual CPtrList* const GetNeighbors(); }; //--------graph.h------------ #pragma once #include "vertex.h" class CGraph : publ...

FIND AND REPLACE FUNCTION CRASHES WORD
Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: intel Hello. New Mac User. Office: Mac 2008 Home and Student version When I use the find and replace in Word, Word closes, does not save changes and tells me to report the error to Microsoft. What do you suggest I do? I did report it each time it crashed. Thank you. Doesn't happen here. Diagnostic tricks to help locate the cause of the problem: 1) test in a variety of documents--new documents, old docs, long docs, short docs, to see if it happens in every document or just certain documents. 2) Log out of your user...

Disable Search Function In Excel Worksheet
Hi Everybody, Can anyone help me in disabling search function (Control + F ) in Excel 2003? You should be able to assign that key combination to a command macro, which will take precedence over the default action. But why? <dc.dileep@gmail.com> wrote in message news:1159525344.784761.98210@h48g2000cwc.googlegroups.com... > Hi Everybody, Can anyone help me in disabling search function (Control > + F ) in Excel 2003? > Can you pls tell me how? I juz wanted to disable the search function only for a particular work book. Thank you ! Stephen Bye wrote: > You should be ...