For example, there is some class:

class CSomeClass { double GetLengthLine(std::shared_ptr<CPoint> firstVertex, std::shared_ptr<CPoint> secondVertex) const; } double CTriangle::GetLengthLine(std::shared_ptr<CPoint> firstVertex, std::shared_ptr<CPoint> secondVertex) const { .... return std::hypot(dx, dy); }; 

Will the value be copied when passing smart pointers to the function, or will the link be passed?

  • The shared_ptr object will either be copied or moved depending on how the arguments are delivered. shared_ptr has both a copy constructor and a displacing constructor. Your functions declare the corresponding parameter by value, not as a link. - Vlad from Moscow

2 answers 2

If I correctly understood the question (although there is doubt about this, since there is already an accepted answer), then it’s still a matter of copying an object of type CPoint , which has been parameterized to std::shared_ptr . In this case, it does not matter at all how the smart pointer object will be transmitted by reference or value - this will not lead to additional copying of the object stored in the pointer. Example:

 #include <iostream> #include <memory> struct S { S() { std::cout << "ctor\n"; } S(const S&) { std::cout << "copy\n"; } S(S&&) { std::cout << "move\n"; } }; void f(std::shared_ptr<S>) {} void g(const std::shared_ptr<S>&) {} int main(){ auto s = std::make_shared<S>(); f(s); g(s); } 

Conclusion:

ctor

Those. only one object was created. Neither copying nor moving occurred. This is basically logical, since similarly, no creation of (useful) objects occurs when passing ordinary (non-smart) pointers.

In the general case, any sufficiently large (more than several sizeof(int) ) object makes sense to pass on a constant link, if you do not intend to modify it.

    Since the parameters of your functions take an object of type std::shared_ptr<CPoint> by value, copies of the arguments with which they are called will be passed to the functions. That is, the corresponding argument will either be moved or copied to a function parameter, since the class std::shared_ptr has both a copy constructor and a move constructor.

    But the number of links to the pointer that is wrapped in std::shared_ptr will change. That is, when calling functions, the reference count to the original "raw" pointer will change.

    Consider the following demo program.

     #include <iostream> #include <memory> void f( std::shared_ptr<int> p ) { std::cout << "Inside f() shared_ptr<int>::use_count() = " << p.use_count() << std::endl; } int main() { std::shared_ptr<int> p( new int ( 10 ) ); std::cout << "Before calling f shared_ptr<int>::use_count() = " << p.use_count() << std::endl; f( p ); std::cout << "Aftera calling f shared_ptr<int>::use_count() = " << p.use_count() << std::endl; return 0; } 

    Her console output is as follows.

     Before calling f shared_ptr<int>::use_count() = 1 Inside f() shared_ptr<int>::use_count() = 2 After calling f shared_ptr<int>::use_count() = 1