在 C++ 中按值传递临时结构的简单方法?

发布于 2024-08-20 06:55:27 字数 936 浏览 7 评论 0原文

假设我想将一个临时对象传递给一个函数。有没有办法用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

墨离汐 2024-08-27 06:55:27

在结构中提供构造函数的另一种方法是提供 make_xxx 自由函数:

struct Point {int x; int y;};

Point makePoint(int x, int y) {Point p = {x, y}; return p;}

plot(makePoint(12, 34));

您可能希望避免在结构中使用构造函数的一个原因是允许在结构数组中进行大括号初始化:

// Not allowed when constructor is defined
const Point points[] = {{12,34}, {23,45}, {34,56}};

const Point points[] = {Point(12,34), Point(23,45), Point(34,56)};

An alternative to providing a constructor in your struct, would be to provide a make_xxx free function:

struct Point {int x; int y;};

Point makePoint(int x, int y) {Point p = {x, y}; return p;}

plot(makePoint(12, 34));

One reason why you might want to avoid constructors in structs is to allow brace-initialization in arrays of structs:

// Not allowed when constructor is defined
const Point points[] = {{12,34}, {23,45}, {34,56}};

vs

const Point points[] = {Point(12,34), Point(23,45), Point(34,56)};
凉月流沐 2024-08-27 06:55:27

这在 C++11 标准中是可能的。基本上,您可以这样做:

struct_func(test_struct{5, 7});

从 4.4 版开始,这已经在 GCC 中可用。

This is possible in the C++11 standard. Basically, you are able to do this:

struct_func(test_struct{5, 7});

This is already available in GCC as of version 4.4.

找个人就嫁了吧 2024-08-27 06:55:27

您可以像在课堂上一样进行操作。只需给你的结构一个构造函数,你就可以像结构一样内联创建它。您对使用构造函数的反对是没有根据的。类和结构之间的主要区别是与其关联的默认可见性。对于课程来说,它是私人的;对于结构,公共。没有“样板”,您不必遵守任何您不想遵守的“惯例”。

struct test_struct
{
    int a;
    short b;

    test_struct(int a_, int b_): a(a_), b(b_) { }
};

struct_func(test_struct(5, 7));

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.

struct test_struct
{
    int a;
    short b;

    test_struct(int a_, int b_): a(a_), b(b_) { }
};

struct_func(test_struct(5, 7));
§普罗旺斯的薰衣草 2024-08-27 06:55:27

我不是 C++ 专家,但是您不能将创建语句放在函数调用的参数列表中吗?

I'm not c++ expert, but couldn't you just place the creation statement inside the argument list of your function call?

七堇年 2024-08-27 06:55:27

结构体也可以有构造函数,因此您可以对它们执行与类示例相同的操作。

Structs can also have constructors, so you can do the same thing with them you do with the class example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文