Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Using delegates:
I own class creates an instance of the library class on the heap and only stores its pointer.

C
JString& JString::format( const TCHAR* formatString, ... )
{
    m_Imp->format(formatString,...);//the compiler warning this!
    return *this;
}


m_imp is the library class pointer that delegate.
JString is the wrapper class.

How can i solve the compiler warning?
Posted
Comments
Sergey Alexandrovich Kryukov 16-Apr-13 23:39pm    
On unmanaged heap, I guess? Do you mean CLI delegates? I don't know what's a C++/CLI "delegate".
I know only C++/CLI delegates, heard of C++/CX delegates... Will you clarify it?
—SA
jiazhiqiang 16-Apr-13 23:49pm    
m_Imp is the pointer member of JString.it will Alloc on Heap in the constuctor of JString.
Now It just How to pass the outer JString format() "..." to inter m_Imp 's format().
Sergey Alexandrovich Kryukov 17-Apr-13 0:09am    
Sorry, I have no idea what JString is (and I hope this is totally irrelevant). You need to answer two of my question:
1) managed vs. unmanaged heap (C++, C++/CLI or mixed-mode); do you understand the question?
2) what is the delegate, in your case? .NET (CLI) delegate, or something else?
—SA
jiazhiqiang 17-Apr-13 0:21am    
I'm really not understand what's you mean.
I I just Want to ask "How to pass the outer JString format() "..." argument to inter m_Imp 's format() which also need the "..." argument ?"
thanks!
Sergey Alexandrovich Kryukov 17-Apr-13 0:31am    
I do understand about '...'.
Do you use .NET (CLI) or not? If not, please explain what do you mean by "delegate".
—SA

In addition to Solution 1:

Here is how '...' arguments are used (please see also my comments to Solution 1):
http://www.cplusplus.com/reference/cstdarg/va_arg/[^].

Using of such arguments is usually highly discourage. First of all, C calling convention is required, which is C/C++ only and not used in OP API, by performance, safety and other reasons. (Do I have to explain how why and how it works? A hint: a caller pops the stack.)

In principle, such arguments could be used in delegate pattern (please see: http://en.wikipedia.org/wiki/Delegation_pattern[^]; credited to Stephan Lang, please see his comments to the question) as well, but I don't think it is ever done, and I would not recommend it. There is an alternative method good for any calling conventions: an additional parameters are put in one array argument, or two: array length and array.

—SA
 
Share this answer
 
v3
I assume the term "delegate" in the title of your question is not related to what is generally understood as a "delegate". You just want to forward the function call via a pointer to a function.

The compiler is stumbling over the "..." in the argument list. In fact, there is no way of simply forwarding a variable argument list from one function to the next. The usual construct in C++ to deal with that problem is the following:

C++
JString& JString::format( const TCHAR* formatString, ... )
{
    va_list args;
    va_start (args, formatString);

    m_Imp->format(formatString, args); // the function called here must be aware
                                       // that args is of type va_list and can iterate
                                       // through the remaining arguments via the args pointer

    return *this;
}


That is how printf and the like are implemented in the C++ runtime library. This only works if the function you call knows that the second argument is in fact a pointer to the variable argument list.
 
Share this answer
 
Comments
Stefan_Lang 17-Apr-13 4:37am    
Great response, +5. I spend a couple of minutes trying to remember this va_* stuff but couldn't come up with a useful enough keyword to power a search. I should have immediately checked out your response ;)
nv3 17-Apr-13 5:02am    
Thanks, Stefan!
jsolutions_uk 17-Apr-13 7:05am    
5 from me, bring on variadic templates :)
nv3 17-Apr-13 7:35am    
Thanks for comment and your vote. If OP were using C++11 that would in fact be an option. However, variadic function templates don't make life easier in that case. So I'd stay away from recommending that approach in this particular case.
jsolutions_uk 17-Apr-13 7:41am    
Fair comment :)

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