What is a smart pointer and when should I use one

0 votes
What exactly is a smart pointer, and when should I use it?
Jun 2, 2022 in C++ by Nicholas
• 7,760 points
273 views

1 answer to this question.

0 votes
A smart pointer is similar to a regular (typed) pointer, such as "char*," except that when the pointer itself exits scope, the value it points to is also deleted.

You can use it as a regular pointer by using "->," but not as an actual pointer to the data.

You can do this by using "&*ptr."

It can be used for:

Objects that must be allocated with new but have the same lifetime as something on that stack.

If the object is linked to a smart pointer, it will be destroyed when the program exits that function/block.

Class data members, so that when the object is deleted, all owned data is also deleted, with no special code in the destructor (you will need to be sure the destructor is virtual, which is almost always a good thing to do).
answered Jun 2, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

I looked up the differences between cout, ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
516 views
0 votes
0 answers

When and why do I need to use cin.ignore() in C++?

In C++, I developed a simple application that requested the user to enter a number and then a string.  Surprisingly, when I ran the application, it never paused to ask for the string.  It simply ignored it.  After conducting some research on StackOverflow, I discovered that I needed to include the following line: cin.ignore(256, '\n'); before the line with the string input  That addressed the problem and allowed the software to run.  My issue is why C++ need the cin.ignore() line, and how can I forecast when I will need to use it. Here's the software I created: #include <iostream> #include <string> using namespace std; int main() { ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
541 views
0 votes
0 answers

What is the difference between a concrete class and an abstract class?

I'm learning C++, but I'm having trouble ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
306 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
314 views
0 votes
1 answer

What is a lambda expression in C++11?

In C++11, what is a lambda expression? A: It's the object of an autogenerated class with overloading operator() const under the hood.  Closure is a type of object that is produced by the compiler.  This 'closure' idea is similar to C++11's bind notion.  Lambdas, on the other hand, usually produce better code.  Full inlining is also possible with calls through closures. Q: When do you think I'd utilise one? A: Define "simple and tiny logic" and request that the compiler generate the code from the preceding question.  You tell the compiler the expressions you wish to be inside the operator ().  The compiler will produce everything else for you. Q: What kind of problem do they tackle that couldn't be solved before they were introduced? A: It's some form of syntactic sugar, like using operators instead of functions for custom add, subtract, and other operations... However, wrapping 1-3 lines of genuine logic to some classes, and so on, saves additional lines of needless code!  Some engineers believe that if the number of lines is reduced, there is a lower likelihood of mistakes (which I agree with). Example of usage auto x = [=](int arg1){printf("%i", ...READ MORE

answered Jun 15, 2022 in C++ by Damon
• 4,960 points
283 views
0 votes
0 answers

What are the differences between a pointer variable and a reference variable?

What distinguishes a reference variable from a ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
239 views
0 votes
0 answers

What is the point of function pointers?

The purpose of function pointers is difficult ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
242 views
0 votes
0 answers

Is hiding implementation detail Encapsulation or Abstraction?

I have seen some people defining abstraction ...READ MORE

May 6, 2022 in Java by narikkadan
• 63,420 points
2,505 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
498 views
0 votes
1 answer

What is a Class and Object in C++?

A Class is like a blueprint, an ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
305 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP