user63898
user63898

Reputation: 30885

can c++ macro set class pointer?

i have this situation i have map that i iterate : int counter = 1;

bool ReelGameObj::setInitWithSpriteFrameName(std::map<std::string,std::string> imageComponentMap)
{
    int counter = 1;
    for (auto keyvalue : imageComponentMap ) {
        std::string pngName = keyvalue.second ;
        SET_SPRITE(counter,pngName);
        counter++;
    }
}

in the header i have :

 private:


    Sprite* reel_1;
    Sprite* reel_2;
    Sprite* reel_3;
    Sprite* reel_4;
    Sprite* reel_5;

i was thinking about dynamclly set the pointers using MACRO to avoid if/else and to do something more generic but with no lack this macro fail

#define SET_SPRITE(__NUMBER__,spriteFrameName) this->reel_##__NUMBER__ = Sprite::createWithSpriteFrameName(spriteFrameName);

is there any better way ?

Upvotes: 1

Views: 140

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129314

You can't do exactly what you've written, because counter needs to be a pre-processor constant for this to work (That is, a macro).

Surely it would be better to use an array, e.g. Sprite* reel[5];, or even better, a std::vector reel;`?

Upvotes: 4

Related Questions