我如何用“...”代理该功能?争论? [英] how Can I delegate the function with "..." argument?

查看:65
本文介绍了我如何用“...”代理该功能?争论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用委托:

我自己的类在堆上创建一个库类的实例,只存储它的指针。



< pre lang =c> JString& JString :: format( const TCHAR * formatString,...)
{
m_Imp-> format(formatString,...); // 编译器警告这个!
return * ;
}





m_imp是委托的库类指针。

JString是包装类。



如何解决编译器警告?

解决方案

除了解决方案1:



以下是''''参数的使用方法(请参阅我对解决方案1的评论):

http://www.cplusplus.com/reference/cstdarg/va_arg/ [ ^ ]。



使用此类争论通常是非常沮丧的。首先,由于性能,安全性和其他原因,需要C调用约定,它只是C / C ++而不是在OP API中使用。 (我是否必须解释它为何以及如何工作?一个提示:调用者弹出堆栈。)



原则上,这些参数可用于委托模式(请参阅: http://en.wikipedia.org/wiki/Delegation_pattern [< a href =http://en.wikipedia.org/wiki/Delegation_patterntarget =_ blanktitle =New Window> ^ ];记入Stephan Lang,请看他对问题的评论)同样,但我不认为它曾经完成,我不会推荐它。有一种替代方法适用于任何调用约定:附加参数放在一个数组参数中,或者两个:数组长度和数组。



-SA


我认为问题标题中的委托一词与通常被理解为委托的内容无关。您只想通过指向函数的指针转发函数调用。



编译器在参数列表中绊倒了...。实际上,没有办法简单地将变量参数列表从一个函数转发到下一个函数。用于处理该问题的C ++中常用的构造如下:



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

m_Imp-> format(formatString,args); // 此处调用的函数必须知道
// args的类型为va_list,可以迭代
// 通过args指针的剩余参数

return * ;
}





这就是在C ++运行时库中实现printf等的方法。这只适用于您调用的函数知道第二个参数实际上是指向变量参数列表的指针。


Using delegates:
I own class creates an instance of the library class on the heap and only stores its pointer.

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?

解决方案

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


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:

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.


这篇关于我如何用“...”代理该功能?争论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆