Currently my code looks like this:
class A
{
public:
//A(A&&);
A& operator=(const A&);
std::vector<std::shared_ptr<B>> myVec;
int accessMyFunc() {
return myVec[0]->myFunc();
}
}
class B
{
public:
virtual ~B();
virtual int myFunc() const = 0;
}
class C : public B
{
public:
virtual int myFunc() const override {/* do stuff*/}
}
A makeanA()
{
A temp;
temp.myVec.emplace_back(new C)
return temp;
}
When I run this main
int main()
{
A theA;
theA = makeanA();
theA.accessMyFunc();
}
I get a segfault from the -> within accessMyFunc. However, *myVec[0]
does not segfault, and I have checked within the assignment constructor for A that the shared_ptrs are being copied properly. Why would this segfault be occurring not on dereference, but on the method call? Especially when the assignment constructor verifies that the shared_ptr of the lhs and the rhs both point to the same spot?
Any help is appreciated.
So it ended up that my error was dealing with a dynamic/shared library. I opened the library within a function other than my main, so when the function returned, its attempts to find the virtual table resulted in a segfault