Discussion:
static member variables and DLLS
(too old to reply)
rk
2011-08-12 19:31:27 UTC
Permalink
Hi,

I have run into similar problems before and have used convoluted
strategies to resolve the issue. This time I want to undertand this
problem better. I have the following scenario.

My dll has the files A.cpp, B.hpp and someother_file.cpp
/// A.hpp

EXPORT_CLASS class A : public B
{
public:
static const char *flag_;

virtual const char* flag() { return A::flag_; }

}

/// B.hpp
EXPORT_CLASS class B
{
public:
virtual const char * flag() = 0;
}

///// somother_file.cpp
#include "A.hpp"

void test(B& b)
{
if( b->flag() == A::flag_ )
{
//// Do something...
}
}


/// main.cpp
#include <factory.hpp>

int main(int argc, char *argv[])
{
B* b = factory_create_a();
test(*b);
}


The issue is that the condition b->flag() == A::flag_ fails. I have
looked at the dissasembly and the addresses being compared are
different, i.e. b->flag() returns ad different address than A::flag_.

It would be great to get an Idea of what is going on here?
DDD
2011-08-21 14:51:19 UTC
Permalink
Post by rk
Hi,
I have run into similar problems before and have used convoluted
strategies to resolve the issue. This time I want to undertand this
problem better. I have the following scenario.
My dll has the files A.cpp, B.hpp and someother_file.cpp
/// A.hpp
EXPORT_CLASS class A : public B
{
      static const char *flag_;
      virtual const char* flag() { return A::flag_; }
}
/// B.hpp
EXPORT_CLASS class B
{
    virtual const char * flag() = 0;
}
///// somother_file.cpp
#include "A.hpp"
void test(B& b)
{
   if( b->flag() == A::flag_ )
   {
        //// Do something...
   }
}
/// main.cpp
#include <factory.hpp>
int main(int argc, char *argv[])
{
    B*  b = factory_create_a();
    test(*b);
}
The issue is that the condition b->flag() == A::flag_ fails. I have
looked at the dissasembly and the addresses being compared are
different, i.e. b->flag() returns ad different address than A::flag_.
It would be great to get an Idea of what is going on here?
If we pretend char* flag_ is int flag_

EXPORT_CLASS class A : public B
{
public:
static const int flag_;

virtual const int flag() { return A::flag_; }

}

Then we can see the function return a copy value of flag_, but not
flag_ itself.

So, char * returned by the function is not char*flag_ int the static
scope.

Is that right?

Loading...