Polymorphism with smart pointers

I am trying to move past a "C with Classes" approach to C++11 and C++14. My topic du jour is polymorphism, but with smart pointers.

Say, I have a set classes like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class A
{
protected:
   int a;

public:
   A() { a = 1; }
   foo() { cout << a << endl; }
   virtual bar() = 0;
};

class B : public A
{
private:
   char b;

public:
   B() { b = 'b'; }
   bar() { cout << "in B: " << b << endl; }
};

class C : public A
{
private:
   string c;

public:
   C() { C = "ccc"; }
   bar() { cout << "in C: " << c << endl; }
};


Now, using raw pointers, as follows:

1
2
3
4
5
6
7
8
9
10
11
void baz(A* param)
{
   param->bar();
}

//...

A* b = new B();
A* c = new C();
baz(b) // prints "in B: b"
baz(c) // prints "in C: c" 


I have been watching videos and have heard Stroustrup talk about how you shouldn't really be using raw pointers in modern C++. I am unsure of how to implement the same behavior using smart pointers.
Hi,

Have a look at the example here:

http://en.cppreference.com/w/cpp/memory/unique_ptr

There is much more information if you do a Google search.

Good Luck !!
here's an example

1
2
3
std::vector<std::unique_ptr<int>> lol;
int wat = 2;
lol.push_back(std::make_unique(wat));
vocal dtors make use of smart pointers even more vivid illustrating heap created instances being destroyed without an explicit call to delete:
http://coliru.stacked-crooked.com/a/9394f3a98050bc2b
Thanks all for the references. I am beginning to be able to wrap my head around it. My next question is when passing as parameters. For instance:

1
2
3
4
5
6
7
8
9
10
11
void baz(unique_ptr<A> temp)
{
   temp->bar();
}

// .....

unique_ptr<A> b = make_unique<B>();
unique_ptr<A> c = make_unique<C>();
baz(b);
baz(c);


It is at this point I am getting errors (it looks like the default constructor is deleted). When moving, it works fine:

1
2
3
baz(move(b));
baz(move(c));
c->bar();


But there is a seg fault when c->bar() is called. Assuming my understanding of move semantics is correct (c would be destroyed), this is to be expected. How would I be able to pass a unique ptr over and keep its value after?

I noticed that there are no issues with using shared_ptr. Perhaps this gets back to a poor understanding of smart pointers.

unique_ptr only allows it to have one owner. In this instance, the function from which it is created is the owner and it simply cannot be passed. However, when moved, ownership goes to baz. When baz ends, the memory gets cleaned up and you get the seg fault.

shared_ptr allows for more than one owner, hence why you are able to initialize another shared_ptr with a shared_ptr and pass it in the standard fasion.

Does this sound about right?
Does this sound about right?
Yes.

Generally: After an object is moved, you shouldn't make any assumptions about its content. Rather assign some new value to it or don't use it at all after it has been moved.
it looks like the default constructor is deleted

rather the copy ctor is deleted (along with the copy assignment operator), in addition to moving passing by reference would also work:
http://coliru.stacked-crooked.com/a/a244f4ef2553f3ee
Topic archived. No new replies allowed.