smart pointer (Android strong pointer) reference type copy

Viewed 2953

I cannot figure out the following difference in Android framework (in C++) as

class foo {
  ... 
}
class child_foo : public foo {
  ...
} 

sp<child_foo> item = new child_foo;
const sp<foo> &r1 = item;

Then the strong count of item is 2.

However, if it is in

sp<child_foo> item = new child_foo;
const sp<child_foo> &r1 = item;

Then the strong count of item is 1.

What's the difference between them?

I also noticed that const sp<foo> &r2 = item; will invoke sp's(original typo of foo's) constructor, why?

Fix here, NOT foo's constructor but sp's (strong pointer's).

Thanks in advance!

2 Answers
Related