`static color` is weired

I’ve got class FieldBackground with

static const cocos2d::Color4F emptyColor;

in .hpp file. In .cpp file:

const Color4F FieldBackground::emptyColor(Color4F::WHITE);

But color is BLACK, and not WHITE. What is wrong?

Const promises the compiler that it won’t change

I know that, but it isn’t changing (in this case, it wouldn’t compile), it is initialisation for any static variable.

It works (at least) for int, float and Vec2.

You are using const here

and then trying to change it

const Color4F FieldBackground::emptyColor(Color4F::WHITE);

and you dont need static unless you are calling this class statically and I don’t see a reason to do so.

I don’t change it there, it is initialisation in global scope (not in function) and it is only one possible and allowed way to do it with static variable. Also, I don’t need copy for every object.

Color assignment works. Consider this:

 cocos2d::Color4F red = cocos2d::Color4F(1.0f, 0.0f, 0.0f, 1);
    cocos2d::Color4F white = cocos2d::Color4F(1.0f, 1.0f, 1.0f, 1);
    
    cocos2d::Color4F anothercolor = cocos2d::Color4F(0.5f, 0.5f, 0.5f, 1);
    
    cocos2d::Color4F emptyColor;
    
    emptyColor = anothercolor;
    
    cocos2d::Point stripe1[] = {cocos2d::Point(startXPos,startYPos),
        cocos2d::Point(startXPos,stripeHeight),
        cocos2d::Point(stripeWidth,stripeHeight),
        cocos2d::Point(stripeWidth,startYPos)};
    cocos2d::DrawNode* dotNode = cocos2d::DrawNode::create();
    dotNode->drawPolygon(stripe1, verts, red, 0, red);
    addChild(dotNode, 0);
    
    startXPos += stripeWidth;
    
    cocos2d::Point stripe2[] = { cocos2d::Point(startXPos,startYPos),
        cocos2d::Point(startXPos,stripeHeight),
        cocos2d::Point((startXPos + stripeWidth),stripeHeight),
        cocos2d::Point((startXPos + stripeWidth),startYPos)};
    cocos2d::DrawNode* dotNode2 = cocos2d::DrawNode::create();
    dotNode2->drawPolygon(stripe2, verts, white, 0, white);
    addChild(dotNode2, 0);
    
    startXPos += stripeWidth;
    
    cocos2d::Point stripe3[] = { cocos2d::Point(startXPos,startYPos),
        cocos2d::Point(startXPos,stripeHeight),
        cocos2d::Point((startXPos + stripeWidth),stripeHeight),
        cocos2d::Point((startXPos + stripeWidth),startYPos)};
    cocos2d::DrawNode* dotNode3 = cocos2d::DrawNode::create();
    dotNode3->drawPolygon(stripe3, verts, anothercolor, 0, anothercolor);
    addChild(dotNode3, 0);

const promises the compiler it wont change. You can’t assign to a const it should produce a compiler error.

Also cocos2d::Color4F doesn’t accept assignment like this: FieldBackground::emptyColor(Color4F::WHITE);

It’s actually a C++ (compiler) thing. The order of static initialization is undefined in C++, and in cocos2d-x, the color constants are statics. So when you make a new static ‘emptyColor’, it may get initialized before the compiler knows about, e.g., Color4F::White. It’s undefined. Make your static initialization, if you really need them static, happen in an init() method which will get called after everything else.