Skip to main content
gamedev.net
🔒 Locked

std:string's in structs

Started by chbfiv Oct 15, 2003 at 4:45 PM 9 replies 700+ views
Original Post
chbfiv
chbfiv
If I have a basic c++ struct
struct Node {
     std::string Label;
     int some_number;
};

Node myNode = {0}; //

no workie, any ideas on how to get around this so I don''t need to init each var one at a time? I''m working on converting alot of my char''s to strings and I use alot of structs.
-BourkeIV
Anon Mike
Anon Mike
Once you start playing with non-trivial objects there is no way to get around initializing each member seperately.

You can ease the pain a bit by giving Node a constructor. Then at least you only have to write the init code once.
-Mike
chbfiv
chbfiv
Well that was what I needed, both are good ideas. thanks
-BourkeIV
CWizard
CWizard
quote:
Original post by chbfiv
Well that was what I needed, both are good ideas. thanks

Zeroing a std::string in NOT a good idea.

In your case you can do: Node myNode = { 0, "something" };
Otherwise, write a constructor or live with initializing the one you need to initialize.
daerid
daerid
Actually, you can't CWizard.

A std::string has a constructor, and therefore a function call is made when the object is constructed. Non-trivial objects may not be used in aggregate initialization.

*EDIT* Well, F**K. There goes that... Apparently CWizard was correct. I guess the only thing you can't use in aggregate initialization are objects with a non-trivial constructor.

[edited by - daerid on October 15, 2003 7:45:42 PM]
daerid@gmail.com
CWizard
CWizard
quote:
Original post by daerid
I guess the only thing you can't use in aggregate initialization are objects with a non-trivial constructor.
Haven't tried lately, but I think you can initialize any object if it has an appropriate copy constructor, eg:
struct stuff_t
{
not_a_trivial_1_t a;
not_a_trivial_2_t b;
not_a_trivial_3_t c;
};

stuff_t x = {
not_a_trivial_1_t(foo, bar),
not_a_trivial_2_t("lkjdslfsd"),
not_a_trivial_3_t(1, 2, 3)
};


[edited by - CWizard on October 15, 2003 7:55:11 PM]
DrPizza
DrPizza
quote:
Original post by daerid
Actually, you can''t CWizard.

A std::string has a constructor, and therefore a function call is made when the object is constructed. Non-trivial objects may not be used in aggregate initialization.

*EDIT* Well, F**K. There goes that... Apparently CWizard was correct. I guess the only thing you can''t use in aggregate initialization are objects with a non-trivial constructor.



I would have to check to be sure, but I believe you can aggregate initialize anything with a single-argument constructor. string has one -- taking const char* -- so one can use that in the aggregate initializer.

At the expense of a copy, one could call any constructor, by creating an anonymous object in the aggregate initializer -- this will attempt to invoke the copy constructor which, of course, is a one-argument constructor.

Like I say, I would have to check to be certain of this, but I think it''s how things work.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
jonnii
jonnii
why not just make it a class with a constructor?
-jonnii=========jon@voodooextreme.comwww.voodooextreme.com
chbfiv
chbfiv
Well you can jst do this, as Anon Mike pointed out

struct Node {
Node() :Label("ANameofANode"),somenumber(2) { }
string Label;
int somenumber;
};


ZeroMemory( &Node, sizeof(Node)); also works, except someone here says that is bad=) Thats also a Windows function btw.

-BourkeIV
deepdene
deepdene
The default constructor isn''t a bad way of doing things anyway much cleaner than ZeroMemory which you have to be REAL careful of with any objects that use virtual functions. Since there is a chance with any byte manipulation tools that you''ll also corrupt the VTABLE (stands for virtual''s table, it''s the way the c++ compiler keeps track of the virtuals inside a class)

quote:
Original post by chbfiv
Well you can jst do this, as Anon Mike pointed out

struct Node {
Node() :Label("ANameofANode"),somenumber(2) { }
string Label;
int somenumber;
};


ZeroMemory( &Node, sizeof(Node)); also works, except someone here says that is bad=) Thats also a Windows function btw.




Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.