简体   繁体   English

C++ 返回一个 const unsigned char

[英]C++ Returning a const unsigned char

So, I've been struggling with this bit.所以,我一直在努力解决这个问题。 I have a simple function, that is defined as follows:我有一个简单的 function,定义如下:

const unsigned char GetColor(unsigned int x, unsigned int y)
{
    const unsigned char color[3] = {0, 1, 0};
    return color;
} 

Yet, I am not allowed to do this and the compiler returns with the following error:然而,我不允许这样做,编译器返回以下错误:

Error: cannot initialize return object of type 'const unsigned char' with an lvalue of type 'const unsigned char [3]'

Should I convert the array into a pointer to an array, which I should return?我应该将数组转换为指向数组的指针,我应该返回吗? If I do this, don't I risk that it may be deleted after going "out of scope" - aka out of the function?如果我这样做,难道我不会冒着在“超出范围”之后将其删除的风险——也就是超出 function 的风险吗?

Any help in understanding this problem will be greatly appreciated:)任何有助于理解这个问题的帮助将不胜感激:)

EDIT: The end goal is to have a function, that can return a color at a pixel location, given an x- and y value.编辑:最终目标是拥有一个 function,它可以在给定 x 和 y 值的情况下在像素位置返回颜色。 That means that color changes, depending on the value of x- and y.这意味着颜色会根据 x- 和 y 的值发生变化。 The example above is a simplification.上面的例子是一个简化。

It should return an const unsigned char array of size 3, with a value in each.它应该返回一个大小为 3 的 const unsigned char 数组,每个数组都有一个值。

You want to return an array of numbers.你想返回一个数字数组。 You are right that trying to pass some kind of reference to a local variable would create a problem with scope (technically called a dangling pointer / dangling reference), although for different reasons than indicated.您是对的,尝试将某种引用传递给局部变量会导致 scope 出现问题(技术上称为悬空指针/悬空引用),尽管原因与所示不同。 (The compiler simply complains about the type disagreement between a single char and an array of char s.) (编译器只是抱怨单个charchar数组之间的类型不一致。)

C++ gives you several safe ways of doing so. C++ 为您提供了几种安全的方法。 For example, you can return an array type:例如,您可以返回一个array类型:

#include <array>

std::array<unsigned char, 3> GetColor(unsigned int x, unsigned int y)
{
    return {0, 1, 0};
} 

This makes the "three characters" a single variable of a special type that holds three characters, just like a struct { unsigned char a, b, c; }这使得“三个字符”成为包含三个字符的特殊类型的单个变量,就像struct { unsigned char a, b, c; } struct { unsigned char a, b, c; } . struct { unsigned char a, b, c; } Or you can make your own type that does something similar.或者你可以制作自己的类型来做类似的事情。

In the above, you can imagine the "return variable" something invisibly declared by the compiler in a way as if its scope was the statement where you are calling the function.在上面,您可以想象编译器以某种方式隐式声明的“返回变量”,就好像它的 scope 是您调用function 的语句。 The return statement writes to this variable, rather than to an array local to this function. return语句写入这个变量,而不是这个 function 的本地数组。

Obviously you'd need to change the calling code such that it expects std::array<unsigned char, 3> rather than unsigned char[3] , but a) the former version couldn't have worked anyway, so we're speaking of new code rather than trying to change an existing codebase, and b) std::array s are usually preferred to C-style arrays anyway in new code and it's worth learning to work with them.显然,您需要更改调用代码,使其期望std::array<unsigned char, 3>而不是unsigned char[3] ,但是a)以前的版本无论如何都无法正常工作,所以我们在说新代码而不是尝试更改现有代码库,并且 b) std::array通常在新代码中优于 C 风格的 arrays 并且值得学习使用它们。

Of course, there's always the unsafe way, for example:当然,总是有不安全的方式,例如:

unsigned char* GetColor(unsigned int x, unsigned int y)
{
    unsigned char* ret = new unsigned char[3] { 0, 1, 0 };
    return ret;
} 

But then you must not forget to delete[] the returned pointed once you no longer need it.但是,一旦不再需要它,您就不能忘记delete[]返回的指针。

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

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