Question

I use shared_ptr for an abstract class ABC. ABCImpl class is the implementation of ABC. abc_ptr is a shared_ptr< ABC> points to an ABCImpl objects. In caller function, abc_ptr will call one of the member function (func_in_ABC) in ABC class. The compile is successful. But when I use nm or objdump, I could only see the symbol for abc_ptr. There is no symbol displayed for func_in_ABC() in the caller function.

Anyone knows why, or how an I get the output for the symbol for func_in_ABC() in the caller function?

The code is as follows: In ABC.h:

#include <boost/shared_ptr.hpp>

class ABC
{
    public:
        virtual void func_in_ABC(const int param) = 0;
};

typedef boost::shared_ptr<ABC> ABCPtr;
ABCPtr get_ABC_ptr();

In ABCImpl.h:

#include "ABC.h"

class ABCImpl : public 
{
    public:
        ABCImpl() {}
        void func_in_ABC(const int param);
    private:
        int data;
};

In ABCImpl.cpp:

#include "ABCImpl.h"

ABCPtr get_ABC_ptr()
{
        return ABCPtr(new ABCImpl());
}

void ABCImpl::func_in_ABC(const int param)
{
    data = param;
}

In caller function D.cpp:

#include "D.h"
#include "ABC.h"

void D::call_ABC()
{
    ABCPtr abc_ptr = get_ABC_ptr();
    abc_ptr->func_in_ABC(100);
}

The output for D.o from nm:

         U _Unwind_Resume
         U get_ABC_ptr()
0000000000000000 T D::call_ABC()
0000000000000000 W boost::shared_ptr<ABC>::operator->() const
0000000000000000 r boost::shared_ptr<ABC>::operator->() const::__PRETTY_FUNCTION__
         U __assert_fail
         U __gxx_personality_v0

If I change the definition of func_in_ABC in ABC.h, the compilation for D.cpp will fail. I think it will check the definition of class ABC when compiling D.o. But why I can't find the symbol at caller to map to the definition in ABC?

Was it helpful?

Solution

Since func_in_ABC is a virtual function, you don't actually need the symbol name to call it. You just need the offset into the virtual table for that specific virtual function.

If you make func_in_ABC non-virtual, you should see the symbol show up in the nm output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top