Jump to content
  • Advertisement

Archived

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

chbfiv

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
you can try ::ZeroMemory( &struct, lenght );

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!