简体   繁体   English

返回结构而不初始化它

[英]Returning a struct without initializing it

I have a very simple color struct like this 我有一个非常简单的颜色结构

struct {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;
} RGBA;

Now in some function, I would like to return a structure like this. 现在在某个函数中,我想返回这样的结构。

But I can't figure out how to do that. 但我无法弄清楚如何做到这一点。 All I can think of is creating a local structure, filling each field separately, and then returning it. 我能想到的只是创建一个局部结构,分别填充每个字段,然后返回它。

RGBA localStructure;
localStructure.red = r;
localStructure.green = g;
localStructure.blue = b;
localStructure.alpha = a;
return localStructure;

Where I really would like to do something like this 我真的想做这样的事情

return RGBA(r,g,b,a);

is there a way to achieve this in C/C++? 有没有办法在C / C ++中实现这一目标?

First, in your first snippet, RGBA is an object whose type is an unnamed struct I don't think that's what you intended. 首先,在你的第一个片段中,RGBA是一个对象,其类型是一个未命名的结构,我认为这不是你想要的。 You probably meant either: 你可能意味着:

typedef struct { ... } RGBA; /* what a lot of C programmers do */

or 要么

struct RGBA { ... }; /* probably what you meant */

Anyway, to answer your question, give it a constructor! 无论如何,要回答你的问题,给它一个构造函数!

struct RGBA {
    RGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a) : red(r), green(g), blue(b), alpha(a) {
    }

    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;
};

Then you can write: 然后你可以写:

return RGBA(r,g,b,a);

In C you can initialize structures with this shortcut : 在C中,您可以使用此快捷方式初始化结构:

RGBA x = { r, g, b, a };

So a quick way to do what you want could be this one: 所以快速做你想做的事就是这样:

return (RGBA) { r, g, b, a };

In C++ a struct can have a constructor. 在C ++中,struct可以有一个构造函数。

struct RGBA{
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;

    RGBA( unsigned char _red, unsigned char _green, unsigned char _blue, unsigned char _alpha ) : red( _red ), green( _green ), blue( _blue ), alpha( _alpha )
    {}

};

struct { ... } RGBA defines an object RGBA of an unnamed struct. struct { ... } RGBA定义未命名结构的对象RGBA。 What you probably mean is typedef struct { ... } RGBA; 你可能的意思是typedef struct { ... } RGBA; . Then you can do: 然后你可以这样做:

RGBA fun() {
    RGBA obj = { 0xff, 0xff, 0xff, 0 };
    return obj;
}

GCC supports compound C99 literals in C90 mode and in C++ , so than you can do: GCC支持C90模式和C ++模式下的复合C99文字 ,所以你可以这样做:

RGBA fun() {
    return ((RGBA){ 0xff, 0xff, 0xff, 0 });
}

Yes, declare a constructor. 是的,声明一个构造函数。

struct RGBA { 

   RGBA(unsigned char r, unsigned char g, unsigned b, unsigned char a = 0xff)
     : red(r)
     , green(g)
     , blue(b)
     , alpha(a)
   {
   }

   ...
};

Then use it like this: 然后像这样使用它:

 RGBA t = RGBA(0xff,0xff,0);

or this 或这个

 return RGBA(0,0,0,0xff);

Note: constructors are an essential C++ feature. 注意:构造函数是必不可少的C ++特性。 You should definitely read up on object orientation in C++. 你绝对应该在C ++中阅读面向对象。 It will make your life much happier. 它会让你的生活更幸福。

Supposing your RGBA struct is unable to be changed, eg 3rd party or needs to be in C, and your wish to simplify your return in C++, then you can define an initialisor type to do the work: 假设您的RGBA结构无法更改,例如第三方或需要在C中,并且您希望简化C ++中的返回,那么您可以定义初始类型来完成工作:

struct RGBAInitialisor
{
    RGBAInitialisor(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
        impl_.alpha = a;
        impl_.blue = b;
        impl_.green = g;
        impl_.red = r;
    }
    // This converts back to the RGBA on the fly.
    operator RGBA ()
    {
        return impl_;
    }
    RGBA impl_;

};

RGBA f()
{
    return RGBAInitialisor(1,2,3,4);
}

您可以创建一个类而不是一个结构(或保持结构),并为它提供一个带有4个参数的构造函数。

A C++ struct is really a class, so you can add a constructor to it, and then provide code to initialize the fields in the constructor. C ++结构实际上是一个类,因此您可以向其中添加构造函数,然后提供代码来初始化构造函数中的字段。

AC struct has a size, so if you want to initialize all of the fields, you can generally AC结构具有大小,因此如果要初始化所有字段,通常可以

RGBA localStructure;
memset(&localStructure, 0, sizeof(localStructure));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM