C++ va_list 返回奇数

标签 c++

我正尝试在 C++ 中为一个项目实现一种反射形式。这个想法是,您将带有标签的类作为一种模板注册到映射中,然后调用共享基类的 cloneNew 方法来实际创建您想要的对象。但是,当我尝试使用 va_list 来实现此功能时,我得到了奇怪的结果。问题代码为:

GameObject* SphereObstacle::cloneNew(const Vector& position, double charge, const Vector&         dipole, ...)
{
    va_list v1;
    va_start(v1, dipole);
    double radius = va_arg(v1, double);
    va_end(v1);
    return new SphereObstacle(position, charge, dipole, radius);
}

每次我尝试从 va_list 中读取时,它都会返回一个巨大的值。它几乎看起来像是一个指针。我唯一的想法是,问题是由于这是基类中虚方法的实现这一事实引起的,但我还没有在网上找到任何表明这是问题所在的信息。我做错了什么?

最佳答案

问题在于 dipole 是引用类型。关于 va_start,引用 [support.runtime]/3:

The parameter parmN is the identifier of the rightmost parameter in the variable parameter list of the function definition (the one just before the ...). If the parameter parmN is declared with a function, array, or reference type, or with a type that is not compatible with the type that results when passing an argument for which there is no parameter, the behavior is undefined.

关于C++ va_list 返回奇数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13223486/

相关文章:

c++ - 在 C++ 中将对象 vector 保存到文件

c++ - 为什么在此 constexpr 函数中允许使用 std::swap?

c++ - 如何在 C++ 中进行自动机/状态机编码?

C++ |使用 setTo 更改 cv::mat 中的颜色

c++ - 转发声明一个 constexpr 变量模板

c++ - Qt 资源文件解决方法

c++ - STL 映射是否在插入时初始化基本类型?

c++ - Eclipse CDT 有没有办法自动从 .到->

c++ - 使用 libxml2 (c++) 创建字符串

C++ ASM Inline如何在ASM中使用struct成员?