Suppose I declare classes like this:
foo.h
namespace foo
{
class Data
{
public:
void reatain();
void release()
{
_refCnt--;
if(_refCnt == 0) _alloc->dealloc(_data);
}
int _refCnt;
Allocator *_alloc;
void *_data;
};
class Example
{
public:
// some functions
Data *_data;
};
}
bar.h
namespace bar
{
class Data
{
// declare the same with foo
};
class Example
{
public:
// some functions
/* except casting to foo::Example operator*/
operator foo::Example() const
{
foo::Example fooex;
fooex._data = reinterpret_cast<foo::Data *>(this->_data);
return fooex;
}
};
}
main.cpp
#include <foo.h>
#include <bar.h>
int main(void)
{
bar::Example barex;
foo::Example fooex = static_cast<foo::Example>(barex);
// do smt
return 0;
}
All classes are compiled in the same host, arch, compiler, etc.
I'd like to know with this implementation, is there any hidden bug inside it.
When foo:Example is deallocated. foo think his Data *
is foo::Data *
but actually, it's bar::Data *
. Then it calls _data->release()
.
I don't know if there's any problem if I implement like this
I don't know how the compiler does.
2 classes are declared the same. So, someday will they cause an error if bar::Data
is used as foo:Data
?