Sie müssen nur einen Smart-Zeiger an eine Funktion übergeben, wenn Sie auch den Besitz des Zeigers angeben möchten.
Ich habe vor kurzem einen Artikel über Besitz Semantik bei der Weitergabe von std :: unique_ptr zu/von Funktionen geschrieben. Werfen Sie einen Blick http://timoch.com/blog/2013/04/std-unique_ptr-semantic/
Gegeben Struktur A:
struct A { int field; };
Passing ein Rohzeiger
// you make no statement of ownership
// the caller stays the owner of the pointed object
void useA(A* ptr) {
// useA simply uses the pointed object
// it should not delete it (because it does not own it)
ptr->field = 2;
}
von Wert Passing
std::vector<std::unique_ptr<A>> container;
// the caller explicitly gives you ownership of the object
void ownA(std::unique_ptr<A> ptr) {
// you can use the pointed object
std::cout << ptr->field << std::endl;
// the object pointed to by ptr is yours
// you can place it in a container that you own
container.push_back(std::move(ptr));
// now container owns the pointed A instance
// if you do not move it using std::move()
// the pointed object is deleted automatically
}
Passing durch nicht konstante Referenz
// the caller is giving you the opportunity to take ownership of the object
// it is free to not take it though
void maybeOwnA(std::unique_ptr<A> & ptr) {
// ptr is passed by reference
// so maybeOwnA can decide based on its own internal
// to take ownership of the pointed object
// taking ownership requires that std::move is called on ptr
// it may also simply use the pointed object and not
// take ownership
}
void callMaybeOwnA() {
std::unique_ptr<A> ptr = new A();
maybeOwnA(ptr);
// we know if we still own the instance of A
// by looking at ptr
if (ptr.get() != nullptr) {
// we still own the pointed object
}
}
Vorbei const-Referenz
// this is functionally the same as passing a raw pointer
// but you force the caller to use a std::unique_ptr to call you
// the caller will not be able to use a std::shared_ptr for example
// I strongly recommend against this form. Passing a raw pointer is
// perfectly fine
void useA(const std::unique_ptr<A> & ptr) {
// ptr is const so you cannot call std::move on it
// you effectively cannot own it
}
// a factory function should return a std::unique_ptr by value
// the caller owns the pointed object
std::unique_ptr<A> factory() {
return std::unique_ptr<A>(new A());
}
void callFactory() {
std::unique_ptr<A> ptr = factory();
// the pointed object is deleted when ptr goes out of scope
}
Das gleiche gilt für std :: shared_ptr gilt. Wenn die Funktion einfach den Zeiger verwendet und zurückgibt, gibt es keinen Grund, ein std :: shared_ptr zu übergeben. Sie müssen nur ein std :: shared_ptr übergeben, wenn Sie dem Objekt, an das Sie sich wenden, den gemeinsamen Eigentümer des spitzen Objekts geben möchten. Zum Beispiel, wenn die Funktion den Zeiger speichert und erwartet, dass er nach der Rückkehr verwendet werden kann (wenn er zum Beispiel erneut aufgerufen wird).
Ich hoffe, es hilft.
TiMoch
Die einzige Möglichkeit zur Bestimmung der Leistung ist das Messen. Raten oder versuchen, es zu berechnen, ist normalerweise falsch, da der Compiler so viele Optimierungen durchführt, dass ein großer Teil des Codes tatsächlich entfernt wird. Schreiben Sie den Code das Maß und optimieren Sie es entsprechend. –