0

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 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?

(that code is just an EXAMPLE with the exact same logic, its not from my project)

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180

2 Answers2

2

There are several issues with your code:

  1. Your Pixel type requires explicit initalization. Is that wise? @perivesta suggests providing a default constructor; or a default parameter to your existing constructor.

    Another option would be reconsidering whether you even need a proper Pixel class at all. 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, i.e. using the rule of zero

  2. As @digito_evo explains - that's too large of an array to place on the stack; 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, e.g. via std::unique_ptr member, or an std::vector like @NathanOliver suggests (both of them get allocated on the heap).

    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. For more on this point, see the C++ coding guideline against magic numbers.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
1

Your 2D array screen is too big (about 8 MB!!) to fit on the stack. You certainly don't want a stack overflow in your program. Therefore use a vector instead.

Also, a color variable doesn't need to be of type int. What are you going to do with 32 bits really?? Usually, 8 bits is sufficient so I switched to unsigned char.

Since you want a for-loop, have a look at this:

#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;
}
digito_evo
  • 3,216
  • 2
  • 14
  • 42