Jump to content
  • Advertisement
Sign in to follow this  
lordikon

Var/Class init performance question

This topic is 3479 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

Which is faster? 1.) String myStr = "Foo"; or 2.) String myStr("Foo"); Does the first method call the default constructor for String, and then the operator= operator? The second method I'm assuming just calls a copy constructor?

Share this post


Link to post
Share on other sites
Advertisement
Assuming C++, they are the exact same, both will call the one constructor that accepts a C-style string or some other type which accepts a C-style string.

Share this post


Link to post
Share on other sites
Yes I meant C++, sorry about the lack of info.

Nice that they generate identical code. What about this?

3.)
String myStr;
myStr = "Foo";

Obviously this is a poor choice anyway because it could be done on one line, and it leaves 'myStr' uninitialized when it is created, which is bad.

Share this post


Link to post
Share on other sites
That will default construct myStr and then call the assignment operator. This will generally be slower than direct initialization.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!