Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am implementing something similar to the MFC Runtime Class.
My aim is to create an instance of a class, at runtime, using this similar mechanism. I have stored the 'CreateObject' function in a structure, but, I'm stuck on the syntax to call it!

The synopsis of the Code is as follows:-
C++
class CSgObj;
typedef CSgObj*(CSgObj::*PFNCREATE)(void);


struct SG_SRVR_RTC{

	PFNCREATE pfnCreate;
};


CSgObj* CSgObj::_CreateNew(){\
	return(CSgObj*)new CSgObj;\
}

// Initialize an Instance of an SG_SRVR_RTC
static SG_SRVR_RTC classCSgObj=
	(PFNCREATE)(CSgObj::_CreateNew),
};

// Create a New object from the SG_SRVR_RTC
CSgObj* CSgObj::CreateNewObject(SG_SRVR_RTC * pRTC)
{
	ASSERT(pRTC);
	if(pRTC==NULL)return NULL;
	try{
		return this->*(pRTC->pfnCreate)();
	}
	catch(...){
		ASSERT(0);
		return NULL;
	}

}

The Call:
C++
return this->*(pRTC->pfnCreate)();

does not cut the mustard. What should I write there to call this function.

Regards,
:)
Thanks,
I Know the cast is not required, I got there by debugging step by step,the MACRO that contains it.
C++
return (this->*(pRTC->pfnCreate))();

does the trick. The question is where to put the stars, arrows and stripes.
Thanks!
Posted
Updated 6-Apr-12 14:33pm
v2
Comments
Philippe Mori 7-Apr-12 8:03am    
Since your latest modification apply to my solution (comments on it), you should have put that as a comment to my solution and also vote for my solution.

In that code fragment:

C++
static SG_SRVR_RTC classCSgObj=
    (PFNCREATE)(CSgObj::_CreateNew),
};


a { and an & are missing and the cast is not required. It could be as simple as:

C++
static SG_SRVR_RTC classCSgObj= 
{
    &CSgObj::_CreateNew,
};


And for the call itself, most important parenthesis are missing. It should be:

C++
return (this->*(pRTC->pfnCreate))();


Or if you prefer, they are badly placed as the following seems to also compile:

C++
return (this->*pRTC->pfnCreate)();


It make sense are () have higher priority and without parenthesis around the whole this->*pRTC->pfnCreate expression, the parenthesis would be applied only to it immediate left sub expression. That is the compiler would try to evaluate (pRTC->pfnCreate)() first and then this->*result_of_ previous_expression.

In fact it must first get the pointer to the function this->*pRTC->pfnCreate (this is why it must be in parenthesis) and then call the function on that.
 
Share this answer
 
v5
Well, Not a Solution, but a Cry for Help!
I frankly do Not understand how to manage this forum format At All! I Ask a question, get a number of emails, and only the last (most improved)answer sticks on the site! I am supposed to vote for people (how?) if their answer gets overwritten by a better answer in my absence. The answers I get, may often make me re-think the question asked. The route to any answer often follows an hiarchie of questions. When I improve a question, or Improve an answer, the whole thread of thought of arriving at something gets lost!
The Only use of a forum format of this kind is, where I need a reminder of something I once learned, but have since forgotten about.

Regards :)
 
Share this answer
 
v2
Comments
Richard MacCutchan 10-Apr-12 4:06am    
It's quite easy:
If you have a solution to the original question then you use the "Add a solution" button.
If you have a comment or question on the original question or one of the solutions then you use the appropriate "Have a Question or Comment?" buttons.
If you need to edit the original question or one of the solutions then you use the "Improve question" or "Improve solution" link as appropriate.
Why you used a "Solution" to ask this question/post this rant is a bit beyond me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900