-1

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 ?

LongLT
  • 411
  • 6
  • 19
  • 2
    You should use one of the C++ casting templates, not C-style casts. – Barmar Mar 31 '17 at 04:27
  • To learn more about C++ casting, read this [post](http://stackoverflow.com/questions/103512/why-use-static-castintx-instead-of-intx). –  Mar 31 '17 at 04:43
  • Thanks, I've updated the post. But as I see, C style also are OK. I think It's not the main problem which I am looking for. – LongLT Mar 31 '17 at 05:38
  • What is the problem that you see ? – Rishi Mar 31 '17 at 05:47
  • I'd like to know with this implementation, is there any hidden bug inside it. When foo:Example is deallocated, bar::Data * is used as foo::Data *. I don't know if there is something not good here – LongLT Mar 31 '17 at 05:49
  • is there a practical reason for this conversion operator : operator foo::Example() const() – pcodex Mar 31 '17 at 06:01
  • fooex._data = reinterpret_cast(this->_data); and are you certain this will work always? – pcodex Mar 31 '17 at 06:02
  • please see my update – LongLT Mar 31 '17 at 06:15
  • At the moment it is unclear what you're asking. At the moment this posts says "look at my code, tell me if there's something wrong". Please rephrase your question so that it fits to Stackoverflow. Read the [help pages](http://stackoverflow.com/help) for more info. – TobiMcNamobi Mar 31 '17 at 06:48

1 Answers1

0

My opinion, the code is fragile, but should work. I'm under the impression field ordering and packing is largely consistent, but I don't believe that's actually a C++ spec thing.

What is more factual, is traditional malloc/free run only off the pointer base address. Also since you don't have a virtual destructor, your dealloc phase is simple enough to not be problematic. I can't comment what effect RTTI might have, I never became knowledgable in that area but it's worth considering.

What's important to point out is that you haven't identified what alloc/dealloc you are using, so the "traditional" method may have no application to you at all. My expectation is yours behaves traditionally.

TobiMcNamobi
  • 4,687
  • 3
  • 33
  • 52
Malachi
  • 2,260
  • 3
  • 27
  • 40