1

I have a class which handles many of huge search process (Start, stop,... etc).

class CSearch
{
 public:
   CSearch();
  ~CSearch();

   bool Start();
   bool Stop();
};

I'm a little bit confused for calling Start function from this way:

CSearch search;
search.Start();

Or this:

std::unique_ptr<CSearch> search(new CSearch);
search->Start();

I have no idea about the diffrence Or it affect performance?

Which is better and why? (Question is not about the only unique_pointer)

Ali Sepehri-Amin
  • 493
  • 1
  • 6
  • 18
  • 1
    Possible duplicate of [How to declare std::unique\_ptr and what is the use of it?](https://stackoverflow.com/questions/16894400/how-to-declare-stdunique-ptr-and-what-is-the-use-of-it) – wally Oct 05 '17 at 12:18
  • 3
    Without knowing the full context, I would say using pointers (smart or not) is probably not the right solution to whatever problem you have. – Some programmer dude Oct 05 '17 at 12:20
  • 3
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Oct 05 '17 at 12:21
  • If you do not understand the difference, then the only place you will find a thorough explanation would be your C++ book. This is a rather long, technical topic, and stackoverflow.com is not a place to get an in-depth C++ tutorial from someone. – Sam Varshavchik Oct 05 '17 at 12:30
  • @Someprogrammerdude I edited my post, I have The class above. `Start` function will search a string in files. But I don't know should I use the Constructor or Remove it and use Smart Pointer instead. What is different? – Ali Sepehri-Amin Oct 05 '17 at 12:32
  • Constructors have nothing to do with smart pointers. Whether one uses a smart pointer, or not, there's always a constructor. Again: this cannot be fully explained in one or two sentences. The full explanation will be in your C++ book. – Sam Varshavchik Oct 05 '17 at 12:33
  • @SamVarshavchik Ok I will check that – Ali Sepehri-Amin Oct 05 '17 at 12:34
  • Please break this question into smaller, more specific questions. There are specific answers dealing with [performance of smart pointers](https://stackoverflow.com/q/22295665/1460794) for example. The way this question is currently written "performance or something...", "question is not about the only unique_ptr", makes it seem like a long rambling answer is required and that doesn't quite fit the SO format. – wally Oct 05 '17 at 12:51

3 Answers3

3

If you can't explain why do you really need pointers then don't use them.

Here's quick read from Stroustrup: 5.2 Resource Management

StahlRat
  • 1,199
  • 13
  • 25
2

Which is better, a mountain bike or a road bike?

Both of those have situations where they are better, although in or better I'd use make unique instead of a wrapped new there.

As a general rule, unless you need dynamic lifetime ir allocation for a soecific reason, automatic storage is a better idea.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
0

The C++ core guidelines advise that scoped objects (local objects, global objects, or members) should be preferred to heap allocation.

In terms of performance, using scoped variables implies that there is no separate allocation and deallocation cost in excess of that already used for the containing scope or object.

The above is taken from here: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-scoped

You can find further guidelines on resource management here: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#S-resource