Multiple inheritance: casting yields different address

This is a discussion on Multiple inheritance: casting yields different address within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by dwks I wasn't using C-style casts, I was using dynamic_cast. That was my first thought, but having ...

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    37
    Quote Originally Posted by dwks View Post
    I wasn't using C-style casts, I was using dynamic_cast.


    That was my first thought, but having data or having no data makes no difference. My understanding of this is pretty shaky, but here's what I think is happening. I think with multiple inheritance that instances of the class need two vtables, so that if an object is treated as one parent type, the appropriate methods can be looked up and called, and likewise for the other parent type.

    So it would seem to me that there's no way to use multiple inheritance yet have the address of the object, no matter what base class it is casted to, be the same. Anyway, I'm not too concerned about this; I can always use the intrusive idea outlined above by brewbuck and bithub.

    Or I could simply inherit every single class from a given (virtual) base class, then make ReferenceCounter always cast to this class when determining the address. I think that if this base class were virtual, it would always be located in the first vtable (at least that's what my experiments indicate), which would solve the problem.
    It looks like you are indeed correct at least on Visual Studio. I just tested it. In addition what you said makes sense to me so I would imagine it works that way with a lot of compilers if not all of them. Well that's one more reason to avoid multiple inheritance

    Edit: Sorry I didn't mean to imply you were using C style casts. I was just commenting about them in general.
    Last edited by SyntaxError; 06-08-2009 at 03:43 PM.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Edit: Sorry I didn't mean to imply you were using C style casts. I was just commenting about them in general.
    I didn't mean to imply your implication.

    It should be mentioned that you can mess up just as badly with the C++-style cast reinterpret_cast<>(), which is pretty much just a C-style cast in disguise.

    It looks like you are indeed correct at least on Visual Studio. I just tested it. In addition what you said makes sense to me so I would imagine it works that way with a lot of compilers if not all of them.
    Since my tests were done with GCC, there are two compilers at least that seem to follow my reasoning.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 10:58 AM
  2. Multiple Inheritance Question
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2002, 06:09 AM
  3. Multiple Inheritance Ambiguity
    By FillYourBrain in forum C++ Programming
    Replies: 21
    Last Post: 08-23-2002, 11:31 AM
  4. C++ multiple inheritance problem
    By pongsor in forum C++ Programming
    Replies: 7
    Last Post: 02-08-2002, 08:18 PM
  5. Multiple virtual inheritance
    By kitten in forum C++ Programming
    Replies: 3
    Last Post: 08-10-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21