在 C++ 中按值传递临时结构的简单方法?
假设我想将一个临时对象传递给一个函数。有没有办法用 1 行代码和 2 行代码来使用结构来做到这一点?
对于一个类,我可以这样做:
class_func(TestClass(5, 7));
鉴于:
class TestClass
{
private:
int a;
short b;
public:
TestClass(int a_a, short a_b) : a(a_a), b(a_b)
{
}
int A() const
{
return a;
}
short B() const
{
return b;
}
};
void class_func(const TestClass & a_class)
{
printf("%d %d\n", a_class.A(), a_class.B());
}
现在,我如何使用结构来做到这一点?我得到的最接近的是:
test_struct new_struct = { 5, 7 };
struct_func(new_struct);
给定:
struct test_struct
{
int a;
short b;
};
void struct_func(const test_struct & a_struct)
{
printf("%d %d\n", a_struct.a, a_struct.b);
}
对象更简单,但我想知道是否有一种方法可以根据函数调用正确执行结构成员初始化,而无需为结构提供构造函数。 (我不需要构造函数。我使用结构体的全部原因是为了避免在这种孤立的情况下使用样板 get/set 类约定。)
Suppose I want to pass a temporary object into a function. Is there a way to do that in 1 line of code vs. 2, with a struct?
With a class, I can do:
class_func(TestClass(5, 7));
given:
class TestClass
{
private:
int a;
short b;
public:
TestClass(int a_a, short a_b) : a(a_a), b(a_b)
{
}
int A() const
{
return a;
}
short B() const
{
return b;
}
};
void class_func(const TestClass & a_class)
{
printf("%d %d\n", a_class.A(), a_class.B());
}
Now, how do I do that with a struct? The closest I've got is:
test_struct new_struct = { 5, 7 };
struct_func(new_struct);
given:
struct test_struct
{
int a;
short b;
};
void struct_func(const test_struct & a_struct)
{
printf("%d %d\n", a_struct.a, a_struct.b);
}
The object is more simple, but I wonder if there's a way to do the struct member initialization right in line with the function call, without giving the struct a constructor. (I don't want a constructor. The whole reason I'm using a struct is to avoid the boilerplate get/set class conventions in this isolated case.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在结构中提供构造函数的另一种方法是提供 make_xxx 自由函数:
您可能希望避免在结构中使用构造函数的一个原因是允许在结构数组中进行大括号初始化:
与
An alternative to providing a constructor in your struct, would be to provide a make_xxx free function:
One reason why you might want to avoid constructors in structs is to allow brace-initialization in arrays of structs:
vs
这在 C++11 标准中是可能的。基本上,您可以这样做:
从 4.4 版开始,这已经在 GCC 中可用。
This is possible in the C++11 standard. Basically, you are able to do this:
This is already available in GCC as of version 4.4.
您可以像在课堂上一样进行操作。只需给你的结构一个构造函数,你就可以像结构一样内联创建它。您对使用构造函数的反对是没有根据的。类和结构之间的主要区别是与其关联的默认可见性。对于课程来说,它是私人的;对于结构,公共。没有“样板”,您不必遵守任何您不想遵守的“惯例”。
You can do it the same way as you do with the class. Just give your struct a constructor and you can create it inline just like the struct. Your objections about using a constructor are unfounded. The primary difference between a class and a struct is the default visibility associated with it. For classes, it's private; for structs, public. There's no "boilerplate," and you don't have to obey any "conventions" that you don't want to.
我不是 C++ 专家,但是您不能将创建语句放在函数调用的参数列表中吗?
I'm not c++ expert, but couldn't you just place the creation statement inside the argument list of your function call?
结构体也可以有构造函数,因此您可以对它们执行与类示例相同的操作。
Structs can also have constructors, so you can do the same thing with them you do with the class example.