例如,我有一个类Point
并且有一个函数
void foo(Point pt);
将其称为
foo({1, 2, 3});
答案 0 :(得分:3)
#include <initializer_list>
#include <cassert>
struct bar
{
void foo(const std::initializer_list<int>& bits)
{
assert(bits.size() == 3);
auto i = bits.begin();
x = *i++;
y = *i++;
z = *i++;
}
int x, y, z;
};
int main()
{
bar b;
b.foo({0, 1, 2});
}
答案 1 :(得分:-2)
你必须有三个整数的构造函数。考虑:
struct Point {
Point (int p1_, int p2_, int p3_) : p1 {p1_}, p2 {p2_}, p3 {p3_} {}
int p1;
int p2;
int p3;
};
void foo (Point pt) {
std::cout << pt.p1 << std::endl;
std::cout << pt.p2 << std::endl;
std::cout << pt.p3 << std::endl;
}
和用法:
foo ({1, 2, 3});
std::cout << std::endl;
foo ({4, 5, 6});