#### Archived

This topic is now archived and is closed to further replies.

# std:string's in structs

This topic is 5392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
you can try ::ZeroMemory( &struct, lenght );

##### Share on other sites
Well that was what I needed, both are good ideas. thanks

##### Share on other sites
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.

##### Share on other sites
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]

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
why not just make it a class with a constructor?

##### Share on other sites
Well you can jst do this, as Anon Mike pointed out

struct Node {
string Label;
};

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

1. 1
2. 2
3. 3
Rutin
24
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002228
×