Frage zu Strings
-
Also ich habe eine frage. Und zwar was ist schneller?
std::string name = "Synonym"; std::string name("Synonym");oder gibt es keinen unterschied in der geschwindigkeit?
-
Das ist beides identisch.
-
Praktisch kein Unterschied. Dazu habe ich auch erst gestern etwas geschrieben:
Gugelmoser schrieb:
#include <iostream> struct MyInt { int x; MyInt(int const y): x( y ) { std::cout << "Constructor 1 called." << std::endl; } MyInt(MyInt const &y): x( y.x ) { std::cout << "Constructor 2 called." << std::endl; } }; int main() { /* So, what's going on there? In fact it is not that complicated: 1) A instance, named 'i', shall be created. 2) The right operand of the assignment operator is a literal of type 'int', thus your struct 'MyInt' must have a constructor looking like 'MyInt(int)'. 3) Well done, your struct 'MyInt' does have a constructor looking like 'MyInt(int)', thus the compiler is happy. 4) a) The constructor 'MyInt(int)' is called, creating a temporary. ( like 'MyInt tmp(1);' ) In theory: b) That created temporary serves as argument for the copy constructor 'MyInt(MyInt const&)'. c) The copy constructor receives that temporary. d) The copy constructor is now capable of initializing the actual instance 'i' with the aid of that temporary. --> Finally two instances were created, the temporary and 'i'. --> And to be more precise, that two instances are equivalent. --> Kinda stupid and absolutely unnecessary, isn't it? In practice: b) The copy constructor 'MyInt(MyInt const&)' is not called. The constructor 'MyInt(int)' is the only constructor being called. c) As a consequence, the instance created by 'MyInt(int)' is no temporary but the actual instance 'i'. --> Finally only one instance was created, namely 'i'. --> That's pretty cool, isn't it? --> NOTE: The theoretical process must be feasible (compilable), otherwise the practical process can't be done. What I'm trying to say is, you will get a compiler error, if the copy constructor is not public. */ MyInt i = 1; }
-
Synonym schrieb:
Also ich habe eine frage. Und zwar was ist schneller?
std::string name = "Synonym"; std::string name("Synonym");oder gibt es keinen unterschied in der geschwindigkeit?
Das eine ist copy-initialisation, das andere direct-initialisation (Klammerinitialisierung). Die erste Variante unterscheidet sich nur dadurch von der zweiten, dass sie erfordert, dass ein (nicht expliziter) Ctor von
std::stringvorhanden ist, der einen C-String entgegennimmt - was selbstverständlich der Fall ist.Ansonsten sind sie Geschmackssache.
Edit: So spät!?
-
Sone schrieb:
Die erste Variante unterscheidet sich nur dadurch von der zweiten, dass sie erfordert, dass ein Copy-Ctor von
std::stringvorhanden ist.Ist das nicht eigentlich eher ein nicht expliziter Konvertierungskonstruktor, der da vorhanden sein muss?
-
Sone schrieb:
Das eine ist copy-initialisation, das andere normale Initialisierung.
Wenn du schon hier schon für das erste den Begriff aus dem C++ Standard verwendest, kannst du das für die zweite Initialisierung auch tun: direct-initialization.

Sone schrieb:
Die erste Variante unterscheidet sich nur dadurch von der zweiten, dass sie erfordert, dass ein Copy-Ctor von
std::stringvorhanden ist.Sie erfordert auch, dass der Konstruktor, der den const char* entgegen nimmt, nicht mit
explicitmarkiert wurde.
-
krümelkacker schrieb:
...
Hat es 'nen Grund, dass Du noch mal schreibst, was schon geschrieben wurde?
-
Tachyon schrieb:
Sone schrieb:
Die erste Variante unterscheidet sich nur dadurch von der zweiten, dass sie erfordert, dass ein Copy-Ctor von
std::stringvorhanden ist.Ist das nicht eigentlich eher ein nicht expliziter Konvertierungskonstruktor, der da vorhanden sein muss?
Natürlich, ich hab mich total verheddert. Editiert...
-
Tachyon schrieb:
krümelkacker schrieb:
...
Hat es 'nen Grund, dass Du noch mal schreibst, was schon geschrieben wurde?
Absicht ist es jedenfalls nicht. Hab das wohl übersehen, dass du schon "explicit" erwähnt hast.