开发者

Assigning rows of a static 2D array to another's rows

开发者 https://www.devze.com 2023-03-21 00:36 出处:网络
Here\'s what I have so far: typedef const float Chord[6]; static Chord progressionOne[] = { {5.0f / 6.0f, 1.0, 1.25, 5.0f / 3.0f, 2.0, 2.5},// i

Here's what I have so far:

    typedef const float Chord[6];

    static Chord progressionOne[] = {
        {5.0f / 6.0f, 1.0, 1.25, 5.0f / 3.0f, 2.0, 2.5},         // i
        {5.0f / 6.0f, 2.0f / 3.0f, 1.0, 4.0f / 3.0f, 5.0f / 3.0f, 2.0}, // VI
        {0.75, 1.0, 1.25, 1.5, 2.0, 2.5},                       // III
        {1.125, 1.5,开发者_C百科 1.875, 2.25, 3.0, 3.75}                      // VII
    };

    static Chord progressionTwo[] = {
        progressionOne + 1, //VI
        progressionOne + 3, //VII
        progressionOne + 0, // i
        progressionOne + 0,
    };

This seems to be the closest I can get in several rounds of massaging my notation, swapping around asterisks and adjusting the typedef. What I want, which is hopefully clear in context, is to rearrange progressionTwo's rows, yielding progressionTwo. I don't mind data duplication or hairy notation--I'd just like to be DRY enough to avoid typing the actual values twice.

...I really need to get around to reading the standard :/


If you really want these to be simple arrays, you could simply create #define macros (pre-processor definitions) for the chords, and make the progressions reference those chords:

typedef const float Chord[6];

#define CHORD_I { 5.0f / 6.0f, 1.0, 1.25, 5.0f / 3.0f, 2.0, 2.5 }
#define CHORD_VI { 5.0f / 6.0f, 2.0f / 3.0f, 1.0, 4.0f / 3.0f, 5.0f / 3.0f, 2.0 }
#define CHORD_III { 0.75, 1.0, 1.25, 1.5, 2.0, 2.5 }
#define CHORD_VII { 1.125, 1.5, 1.875, 2.25, 3.0, 3.75 }

const Chord progressionOne[] = {
    CHORD_I,
    CHORD_VI,
    CHORD_III,
    CHORD_VII,
};

const Chord progressionTwo[] = {
    CHORD_VI,
    CHORD_VII,
    CHORD_I,
    CHORD_I,
};

Pre-processor definitions are are messy, though.

If you turn Chord into a full-blown class, you can define them as locals or static variables in the same way, but with more type safety, and more flexibility. Then you could reference progressionOne while defining progressionTwo the way that you want (at least you can with locals. With globals, you might run into a "static order initialization fiasco")

0

精彩评论

暂无评论...
验证码 换一张
取 消