Archived

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

Efficiently passing strings?

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

I have a class with a std::string data member. In that class, I have a constructor that takes a value for the std::string data member. I want to make sure I’m implementing this the most efficient way possible. Here’s some code to show you what I mean:
#include <iostream>
#include <string>
using namespace std;

class Player
{
public:
    // --Uncomment on of the following lines--

    //Player(const string& name): m_Name(name) {}  

    //Player(const char name[]): m_Name(name) {}

    //Player(const char* name): m_Name(name) {} 

    const string& GetName() const {return m_Name;}  
private:
    string m_Name;
};

int main()
{
    Player p1("Dirk");
    cout << "Player one''s name is " << p1.GetName() << endl; 
}
All three constructors work, but is any more efficient that the other? I’m concerned that by passing a string literal to the constructor:
    Player(const string& name): m_Name(name) {}  
I’m instantiating the same string object twice (once for the parameter and once for the data member). Thanks.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
To be honest how often are you instanitating the Player object ? I mean you''re not doing it hundreds of times per frame surely?! ergo, you must be calling the constructor (& hence the string copy func) only a few times at initiallisation of each game/level/whatever, so it might not be the most efficient way of doing it, but frankly its safe & even if you did optimise the hell out of it you''d never notice the speed difference. I hate to say this, but I think you''re trying to prematurely optimise.

What you really should do is build the app & then profile it, then worry about the things that are really taking the time.

Share this post


Link to post
Share on other sites
Actually, the Player class I made is a simplification so that I could ask the question in a clear way. I''m planning on instantiating hundreds or even thousands of objects with this kind of data member.

I understand what you''re saying about premature optimization, but I want to understand what''s happening here, behind the scenes with strings, so that I can make good design choices for larger, more complex programs.

Share this post


Link to post
Share on other sites
I think the point being made though, was that most of the player class objects are constructed before play commences, the overhead involved, compared to other factors is probably minimal... and once in game, you probably aren''t going to be creating many player objects per second (let alone per frame)... but anyway, as fruny said... const references are the best way to pass container types.. as they may well just be handled like pointers behind the scenes (ie.. the cost is independant of the content/type of the object).

Share this post


Link to post
Share on other sites