将对象作为参数传递给C++中的函数

Pass an object as parameters to a function in C++

本文关键字:C++ 函数 参数传递 对象      更新时间:2023-10-16

在Javascript或ActionScript中,可以将对象传递给函数或构造函数。

myfunction({myParam:1, myOtherParam:"hello"});

当你有很多参数要通过时,它非常有用,IMO它使使用函数比使用经典方法更容易。

myFunction(1,"hello");

C++中有类似的东西吗?

TIA

boost参数http://www.boost.org/doc/libs/release/libs/parameter/做你想做的事。

从其摘要来看:

使用此库可以编写函数和类模板按名称接受参数

小示例:

// include boost parameter
#include <boost/parameter/keyword.hpp>
namespace parameter = boost::parameter;
// first declare named parameters
BOOST_PARAMETER_NAME(a)      // Note: no semicolon
BOOST_PARAMETER_NAME(b)
// then declare your function
BOOST_PARAMETER_FUNCTION(
      (int),                 // 1. parenthesized return type
      my_function,           // 2. name of the function
      tag,                   // 3. namespace of tag types (don't change!)
      (required (a, int) )   // 4. one required parameter of type int
      (optional              //    and an optional parameters, with default value
        (b, int, 0)
      )
)
{
  return a + b;
}
// then you can call it with no named parameters
int result = my_function(1, 2);
// or with named parameters
int result = my_function(_a=1, _b=2);
// also with optional parameters omitted
int result = my_function(_a=3);

您可以使用"命名参数习惯用法":http://www.parashift.com/c++-faq-lite/ects.html#faq-10.20

这会让你的电话看起来像:

f(1).myOtherParam("Hello")

请参阅此处以获取与Boost参数的比较。

一个可以使用结构:

struct foo {
   int a,b,c;
   float f;
}

遗憾的是,你需要在某个地方定义它们,而接收功能需要接受它们,你不能只是在原地编造它们。

归根结底,C++中没有任何语言工具可以模仿JavaScript的"习语"。这些东西是动态语言的本质,而C++不是。

是。

在C++中,对象是一个类的实例。您可以创建一个包含所有可能参数的类,实例化它并用您的特定值填充它,然后将该实例传递给函数。

不能只传递任意对象而不是函数的参数,因为C++没有命名的参数-只有位置很重要,并且在某种哈希结构中的位置会丢失。因此,当你有一个函数foo(int x, int y)时,你不能说foo(y=3),因为名称xy只在函数中有意义,而对它的调用者没有意义。

不,不是真的。标准库中有一个map类,当然,您可以编写一个函数,接受一个充满命名值的map作为输入,但没有任何语言功能可以直接与您所展示的功能等效。

正如其他几位海报所指出的,你当然可以定义一个类来保存所有的数据值,但这只是将有序的参数列表从函数推送到该对象的构造函数,所以它根本不会给你带来任何好处。

使用结构或类对数据进行分组。

如果您正在寻找传递键值对的方法,请使用STL映射

不是在语言级别,而是:

当你有很多参数要传递时,它非常有用,IMO它使使用函数比使用经典方法更容易

如果可以的话,你真的应该考虑减少你的参数。这通常可以通过使用聚集体来实现:

Example: 
    draw_rectangle(x1, y1, x2, y2, r, g, b, a)
Could be reduced to:
    draw_rectangle(p1, p2, color)
Or even further:
    draw_rectangle(rect, color)

如果您这样做,那么命名参数的诱惑应该会大大减轻。

您还可以使用其他方法,如Boost Parameter,通过大量的模板魔法,可以实现这样的功能。然而,它要求您将使用此技术的所有函数转换为以非常精确的方式编写的模板,并且如果您要求我,这是一种非常庞大和繁重的尝试,以迫使语言做一些它没有设计好要做的事情。

您可以执行以下操作,它支持有限数量的默认值(受C++模板系统限制)。您需要使用最新标准(C++11)进行编译

// used to skip arguments and default to the previously specified value
struct UseDefault {
  // empty intentionally!
};
// using this is optional, and only required if you want to provide default values by the CALLED function
template <typename T, T def> struct Default {
  T value;
  operator T() {
    return value;
  }
  Default(T value) : value(value) {}
  Default(UseDefault ignore) : value(def) {(void)ignore;}
};
// using tuple, you can bundle any number of parameters to one
int myFunc(std::tuple<Default<int,7>,Default<char,'x'>,std::string> parameters) {
  std::cout << std::get<0>(parameters) << ", " << std::get<1>(parameters) << std::endl;
  return 0;
}

然后,您可以像这样调用myFunc

func(std::make_tuple(6,UseDefault(),"hi"));

但是,请注意,模板参数类型有一些限制,例如,不能将字符串(或char*)作为模板参数传递。然而,如果您只需要合理的基本默认值,这是可行的。

当然,如果您放弃了指定默认值的要求,您可以简单地使用std::tuple而不使用我的Default类型。