I have a base class and child class. I want shared_from_this
to return shared_ptr
to the object in which it is invoked. So if shared_from_this()
is called on child it should return a shared_ptr
to child and same way for base. Below is my sample program
#include <iostream>
#include <memory>
class parent : private std::enable_shared_from_this<parent> {
public:
int a;
parent() : a(1) { }
virtual void f(){
auto self = shared_from_this();
std::cout << "Parent is " << self->a << std::endl;
}
};
class child : public parent, private std::enable_shared_from_this<child> {
public:
int a;
child() : a (2) { }
virtual void f() {
auto self = shared_from_this();
std::cout << "Child is " << self->a << std::endl;
}
};
int main(){
auto p = std::make_shared<parent>();
auto c = std::make_shared<child>();
p->f();
c->f();
}
Since enable_shared_from_this
is privately inherited, shared_from_this()
method for both the classes should be private to themselves hence there shouldn't be any collision/ambiguity. However I get following error while compiling.
[root@archlinux cpp]# clang++ -g stackoverflow_shared_from_this.cpp
stackoverflow_shared_from_this.cpp:19:17: error: member 'shared_from_this' found in multiple base classes of different types
auto self = shared_from_this();
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../include/c++/11.1.0/bits/shared_ptr.h:807:7: note: member found by ambiguous name lookup
shared_from_this()
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.1.0/../../../../include/c++/11.1.0/bits/shared_ptr.h:807:7: note: member found by ambiguous name lookup
1 error generated.
[root@archlinux cpp]#
Can somebody please explain why am I getting this ambiguity error ? What part of my explanation above is incorrect.