copy constructors hurting performance

From: Shea Martin (smartin_at_arcis.com)
Date: 11/04/03


Date: Tue, 04 Nov 2003 09:52:38 -0700

I have a String class (I know I am re-inventing the wheel, yes I have
heard of boost, and of QString).

My copy constructor does a deep (strcpy) of the char *_buffer member.
I have a member function func(const String &param). When an actual
String is passed as the param this is nice and efficient.

I have a constructor which takes a const char* as an argument. And
performs a deep copy of the const char *buffer.

The issue occurrs when I make the following call:
func("this is the param");
The String(const char *buf) constructor is called, making a deep copy of
the data. Because func takes a const arg, it will never alter the
buffer, thus the deep copy of the const char * is needless, and could be
expensive.

Does anyone see an easy and somewhat safe design, to avoid the needless
copy's? Or is the only way to overload the methods to take a const
char* as well?

I think I already know the answer to this, but I would rather be safe
that sorry.

Thanks,

~S



Relevant Pages

  • Re: copy constructors hurting performance
    ... > String is passed as the param this is nice and efficient. ... > I have a constructor which takes a const char* as an argument. ... > performs a deep copy of the const char *buffer. ...
    (comp.lang.cpp)
  • Re: copy constructors hurting performance
    ... > I have a member function func(const String ¶m). ... > String is passed as the param this is nice and efficient. ... > I have a constructor which takes a const char* as an argument. ...
    (comp.lang.cpp)
  • Re: copy constructors hurting performance
    ... > I have a member function func(const String ¶m). ... > I have a constructor which takes a const char* as an argument. ... > performs a deep copy of the const char *buffer. ... could simply store the pointer instead of making a deep copy. ...
    (comp.lang.cpp)
  • Re: Graph traversieren wie ein Petri-Netz
    ... @param strValue zu prüfender String ... public static final boolean empty{ ... @param strBuffValue zu prüfender StringBuffer ...
    (de.comp.lang.java)
  • Re: Hashtable question
    ... * @param modelName is a filename less the .pim extension where the ... public void load() throws IOException { ... public void add(String keyString, Item item) ... * @param keyString the string to lookup by. ...
    (comp.lang.java.programmer)