• Advertisement
Sign in to follow this  

My complex polymorphic inheritance problem.. Its like a puzzle !!! can u solve ??

This topic is 3825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

have a complex problem with my C++ application. To explain it better I have made a diagram(UML) which explains the problem better. my problem Classes are a1,a2,b1,c1 and Rel b1 inherits a1 and a2 c1 inherits b1 Class a2 holds a pointer to object of type Rel and has functions setR() and f1() Class Rel holds a pointer to object of type b1. and has the given constructor which takes a pointer to b1 and update function which takes a void pointer. CURRENT STATUS :- Dubugger shows that There is a difference of 4 bytes between memory locations of b1 & tb1 if i put a breakpoint in update function. eg... b1 - a0cb0930 tb1 - a0cb0934 or vice versa i think.

Share this post


Link to post
Share on other sites
Advertisement
0. Your UML symbols are reversed (you are representing a1 and a2 as inheriting from b1).

1. Do not cast to void* unless you know what you're doing. Even if you do know what you're doing, do not cast to void* unless you need to do so to achieve compatibility with a C library that uses the callback-and-void idiom. In particular, do not use void* as the bottom type of a class hierarchy, ever, because it doesn't work.

2. When you cast from a non-POD pointer A* to void*, the only possible conversion is from void* back to the original A*. No other conversion is safe, and in particular casting from a2* to void* to b1* isn't.

3. You don't need to make explicit your cast from c1* to b1*. If you do want to make it clear that you're making the cast, use static_cast<b1*> instead.

4. When manipulating pointers in situations of multiple inheritance, do not use C-style casts, which will compile even in situations where they don't work. Use static_cast instead, and stay away from void*. Also, reinterpret_cast tends to break a lot when interacting with multiple inheritance, because of the unusual memory layout.

Share this post


Link to post
Share on other sites
First, Thank you very much for your reply. I got some gr8 pointers on "pointer" [:)]

I visited your site and was quite impressive, especially the variety of projects, from Mazes, PHP and raytracing etc...

If i shouldn't use void*, what should i do to pass any object?
I used it just like the Object class in .NET

I thought at runtime after creating an object of type c1, the "this" pointers of a1*, a2*, b1* and c1* (obviously) should point to the same address ??? Am i wrong?

Any idea about how i can solve to get the expected equality result?
This is a part of a complex CAD application and only shown part of the class hierarchy where the problem is. good that you have understood the problem?

Please help me.

Share this post


Link to post
Share on other sites
Quote:
If i shouldn't use void*, what should i do to pass any object?
I used it just like the Object class in .NET


C++ has no root type for its object hierarchy, like the Object class in .NET or Java. You can define it yourself or you can use the root of your individual hierarchies (or, one of them, for instance a2).

Quote:
I thought at runtime after creating an object of type c1, the "this" pointers of a1*, a2*, b1* and c1* (obviously) should point to the same address ??? Am i wrong?


You are. First, pointers don't point to addresses, they point to entities. They are represented as numeric memory addresses on your particular compiler-OS-hardware combination, but this is not a general statement so much as a particular remark about your computer. In particular, there's not guarantee that a given entity will have a single address (it may have several), or any address at all.

Second, the void* type isn't a pointer: it can't be dereferenced, and it can't undergo pointer arithmetic. The only thing about void* that has any kind of relation to pointers is its name. void* serves as a bottom type used for polymorphism in C (not in C++, which has inheritance and virtual methods as a much more safe version of that), because it allows storage of an actual pointer in a type-agnostic context. In essence, void* stores neither an address nor an entity, it stores a pointer.

Of course, if you store a a2* inside a void* and read back a b1*, you're going to get unspecified data, just as if you wrote an integer and read back a float: there are conversions from a2* to b1, just like there are from int to float, but you must do these conversions before (or after) you manipulate the void*.

Quote:
Any idea about how i can solve to get the expected equality result?


class Rel
{
a2* mine;
Rel(a2* obj) : mine(obj) {}
void update(a2* obj)
{
if (obj == mine) do_success();
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement