What are the differences between member functions and member variables in terms of symbols?

Viewed 64

I'm learning to use __attribute__ ((visibility("default"))) for symbol export

// a.cpp
class A
{
public:
     __attribute__ ((visibility("default"))) void func() {;};
     __attribute__ ((visibility("default"))) int cnt;
};

But I ran into the following problem

# g++ -c a.cpp 
a.cpp:5:50: warning: ‘visibility’ attribute ignored [-Wattributes]
      __attribute__ ((visibility("default"))) int cnt;
                                                  ^~~

What's the difference between a member function and a member variable? Why can one export a symbol and the other cannot?

1 Answers

Member functions are really just ordinary functions with special signature to accomodate the hidden this argument. So you can attach visibility attributes to them like to other global functions.

On the contrary, member variables do not correspond to global entities - they are just symbolic names for offsets inside memory occupied by object of the class. So there is no point in attaching visibility to them.

Related