0

I am currently working on some code (c++11), which makes heavy use of references on pointers, e.g.

class SomeClass;

class MyClass
{
public:
   MyClass( const std::shared_ptr < SomeClass > & class) 
    : m_class(class)
   {}

private:
   std::shared_ptr < SomeClass > m_class
}

I made some tests on the performance on this (using Visual Studio 2013 VC12) and there seems to be no difference in time. Handing over a Null-Ptr is also okay.

So what are possible reasons for using a reference in this case?

John
  • 67
  • 1
  • 7
  • Otherwise - it creates a copy and increasing `ref_count` until deletion. Am I wrong, @BartoszKP? – VP. Aug 13 '15 at 09:54
  • @VictorPolevoy The argument is deleted as soon as constructor exists so it doesn't matter. And a copy is still made for `m_class`. – BartoszKP Aug 13 '15 at 09:56

1 Answers1

1

The possible reasons are:

  1. Performance. It should be faster to pass a reference (one CPU register) rather than a smart pointer by value. There is something wrong with your performance tests.
  2. Saving stack space. A smart-pointer passed by value takes more space on the stack than a reference.
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158