Jump to content

Initialization List Array Members

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Buttacup

Buttacup

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
This works:

struct SEdge 
{
private:
    const size_t V1;
    const size_t V2;

public:
    SEdge( size_t v1, size_t v2) : V1(v1), V2(v2){};
};

Why does this not work?

struct SEdge 
{
private:
    const size_t V[2];

public:
    SEdge( size_t v1, size_t v2) : V[0](v1), V[1](v2){};
};

[me] shakes fist [/me]

#2
Buttacup

Buttacup

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Or better yet why is this not working?

struct SEdge 
{
    SEdge( size_t v1, size_t v2 ) : V((size_t[2]){{v1}, {v2}}){};
	
    const size_t V[2];
};


[edit]

is ok?

struct SEdge 
{
    SEdge( size_t v1, size_t v2 ) : V(){V[0] = v1; V[1] = v2;};

    size_t V[2];
};

[edit]

#3
Buttacup

Buttacup

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
ok really nasty.....

struct SEdge 
{
    SEdge(){}
    SEdge( size_t v1, size_t v2 ) : V(){V[0] = v1; V[1] = v2;};

    const size_t        V[2];
};

struct SFace 
{
    SFace(){}
    SFace( size_t v1, size_t v2, size_t v3, size_t v4, size_t v5, size_t v6 ) : ????;
    SFace( size_t v1, size_t v2, size_t v3, size_t v4, size_t v5, size_t v6, size_t v7, size_t v8) : ????{ };

    const SEdge          E[4];

    const bool           is_quad;
};

const SFace BaseTri(0, 1, 1, 2, 2, 0);
const SFace BaseQuad(0, 1, 1, 2, 2, 3, 3, 0);

is aesthetically unpleasant :p

struct SFace 
{
    SFace(){}
    SFace( size_t v1, size_t v2, size_t v3, size_t v4, size_t v5, size_t v6 ) 
    {
        E[0].V[0] = v1;
        E[0].V[1] = v2;

        E[1].V[0] = v3;
        E[1].V[1] = v4;

        E[2].V[0] = v5;
        E[2].V[1] = v6;

        is_quad = false;
    };
    SFace( size_t v1, size_t v2, size_t v3, size_t v4, size_t v5, size_t v6, size_t v7, size_t v8) 
    {
        E[0].V[0] = v1;
        E[0].V[1] = v2;

        E[1].V[0] = v3;
        E[1].V[1] = v4;

        E[2].V[0] = v5;
        E[2].V[1] = v6;

        E[3].V[0] = v7;
        E[3].V[1] = v8;

        is_quad = false;
    };

    SEdge 				E[4];

    bool				is_quad;
};

[edit]

Better!
struct SEdge 
{
    SEdge(){}
    SEdge( size_t v1, size_t v2 )
    {
        V[0] = v1; 
        V[1] = v2;
    };

    size_t V[2];
};

struct SFace 
{
    SFace(){}
    SFace( size_t first, bool is )
    {
        if(!is)
        {
            E[0].V[0] = first;
            E[0].V[1] = first + 1;

            E[1].V[0] = first + 1;
            E[1].V[1] = first + 2;
            
            E[2].V[0] = first + 2;
            E[2].V[1] = first;

            is_quad = false;
        }
        else
        {
            E[0].V[0] = first;
            E[0].V[1] = first + 1;

            E[1].V[0] = first + 1;
            E[1].V[1] = first + 2;

            E[2].V[0] = first + 2;
            E[2].V[1] = first + 3;

            E[3].V[0] = first + 3;
            E[3].V[1] = first;

            is_quad = true;
        }
    };

    SEdge        E[4];

    bool        is_quad;
};

const SFace BaseTri(0, false);
const SFace BaseQuad(0, true);

[/edit]

Edited by Buttacup, 01 January 2010 - 12:34 PM.


#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
The only solution I can find is to compile this with the -std=c++0x flag (I'm assuming you're using g++. If it's a Microsoft compiler then I have no idea.)

struct SEdge 
{
private:
    const size_t V[2];

public:
    SEdge( size_t v1, size_t v2) : V({v1,v2}) { }
}

This is the usual way to do it (at least in my experience), and is correct:
struct SEdge 
{
    SEdge( size_t v1, size_t v2 ) 
    {
        V[0] = v1;
        V[1] = v2;
    }

    size_t V[2];
};

Same goes for your other problems. A word of advice: please try not to cram everything onto one line. It's hard to read.

Edited by dargueta, 01 January 2010 - 01:25 PM.
Syntax error?

sudo rm -rf /

#5
Buttacup

Buttacup

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
yeah,


struct SEdge 

{

private:

    const size_t V[2];


public:

    SEdge( size_t v1, size_t v2) : V({v1,v2}) { }

}


doesn't seem to work in the VC++ compiler......

thx you!

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Why are you so diametrically opposed to the usual way, i.e. not using initializers?
sudo rm -rf /

#7
Buttacup

Buttacup

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
My real beef is with default ctors! Most anything I want to create needs to have at least 1 member assignment...

putting it here:

MyClass(DATA required) : STORAGE(required){}

instead of here:

MYClass(DATA required)
{
STORAGE = required;
}

makes more sense to me because inside the constructor is where I initialize ALL DATA and hence the DATA required somehow loses meaning.

[edit] For my above example SFace will be going into a list to reference a vector of SBasicModel with per vertex data. Faces will be appended to the list one by one and will not be of NULL or uninitialized value.

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Well...in this case I'd say tough luck. I can't think of any other way to get this working. I'm sure there's something simple we're missing, but for now just do it the normal way.
sudo rm -rf /

#9
Buttacup

Buttacup

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
kk will do! *-*