简体   繁体   English

如何使用构造函数 C++ 初始化数组 class

[英]How to initialize an array class with constructor C++

i'm trying to initialize an array of a class, but i can only do this if i specify every object of the array, for example:我正在尝试初始化 class 的数组,但我只能在指定数组的每个 object 的情况下执行此操作,例如:

class Pixel{
private:
    int color;
public:
    Pixel(int color):color(color){}
};

class Screen{
private:
    Pixel screen[1920][1080];   
public:
    Screen(){
    //code//
    //i was trying to use for, to determinate each pixel color from a certain area
    //but i cannot, bc i would have to do something like this:
    screen[1920][1080] = {{Pixel(1),Pixel(1),Pixel(1),Pixel(1)...1080 times},{Pixel(2)
    ,Pixel(2), Pixel(2)...1080 times}...1918 times}
    //as you can see, is nearly impossible to do this in this way.
    }
};

there is a native way to initialize this array with fors or something like this?有一种本机方法可以用 fors 或类似的东西初始化这个数组吗?

(that code is just an EXAMPLE with the exact same logic, its not from my project) (该代码只是一个具有完全相同逻辑的示例,它不是来自我的项目)

There are several issues with your code:您的代码有几个问题:

  1. Your Pixel type requires explicit initalization.您的 Pixel 类型需要显式初始化。 Is that wise?那是明智的吗? @perivesta suggests providing a default constructor; @perivesta 建议提供默认构造函数; or a default parameter to your existing constructor.或现有构造函数的默认参数。

    Another option would be reconsidering whether you even need a proper Pixel class at all.另一种选择是重新考虑您是否甚至需要合适的Pixel class。 Perhaps it might be enough to just have也许仅仅拥有就足够了

    using Pixel = std::int32_t;

    or whichever size a pixel is supposed to have.或像素应该具有的任何大小。 If a pixel needs fancy methods, then a struct wrapping the basic value, but without specifying any constructors or destructors, ie using the rule of zero如果一个像素需要花哨的方法,那么一个包装基本值的结构,但不指定任何构造函数或析构函数,即使用零规则

  2. As @digito_evo explains - that's too large of an array to place on the stack;正如@digito_evo 解释的那样 - 数组太大而无法放在堆栈上; your program might crash for this reason alone, even if you were able to initialize your array.即使您能够初始化数组,您的程序也可能会因为这个原因而崩溃。 Consider having your Screen class allocate space on the heap, eg via std::unique_ptr member, or an std::vector like @NathanOliver suggests (both of them get allocated on the heap).考虑让您的Screen class 在堆上分配空间,例如通过std::unique_ptr成员或像@NathanOliver 建议的std::vector (它们都在堆上分配)。

    Read more about the heap and the stack: What and where are the stack and heap?阅读有关堆和堆栈的更多信息:堆栈和堆是什么以及在哪里?

  3. Use of magic numbers: 1920, 1080 - don't just type them in, say what they mean... have static class (constexpr) constants for these dimensions.使用幻数:1920、1080 - 不要只是输入它们,说出它们的意思......这些尺寸有 static class (constexpr) 常量。 For more on this point, see the C++ coding guideline against magic numbers .有关这一点的更多信息,请参阅C++ 编码指南反对幻数

Your 2D array screen is too big (about 8 MB ..) to fit on the stack.您的 2D 阵列screen太大(大约8 MB ..)无法放入堆栈。 You certainly don't want a stack overflow in your program.您当然不希望程序中出现堆栈溢出。 Therefore use a vector instead.因此,请改用vector

Also, a color variable doesn't need to be of type int .此外,颜色变量不需要是int类型。 What are you going to do with 32 bits really??你真的打算用32位做什么? Usually, 8 bits is sufficient so I switched to unsigned char .通常, 8位就足够了,所以我切换到unsigned char

Since you want a for-loop, have a look at this:因为你想要一个 for 循环,所以看看这个:

#include <vector>


class Pixel
{
public:
    Pixel( const unsigned char color = 0 )
    : m_color( color )
    {
    }

private:
    unsigned char m_color;
};

class Screen
{
public:
    Screen( const std::size_t rowCount = 1920, const std::size_t colCount = 1080 )
    : screen( rowCount, std::vector<Pixel>( colCount ) )
    {
        for ( std::size_t row { }; row < rowCount; ++row )
        {
            for ( std::size_t col { }; col < colCount; ++col )
            {
                screen[ row ][ col ] = Pixel( static_cast<unsigned char>( row ) + 1 );
            }
        }
    }

private:
    std::vector< std::vector<Pixel> > screen; // a vector of vectors
};

int main( )
{
    Screen scrn;
}

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

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