Results 1 to 3 of 3

Thread: Delegation vs Inheritance

  1. #1

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Unhappy Delegation vs Inheritance

    Some I asked say that superclass functions are instantiated once while obtaining access to base class variables via an extra addition (for the offset in different derivatives) while others say that it's compiler dependent? Or does the functions get instantiated per subclass?

    The problem I'm not facing but took out of the blue, is 3 classes which are subclasses of 3 other superclasses. A, B and C are superclasses while AB, AC,BC are subclasses which derive 2 different classes each.

    The objects will contain data that will obviously misaligned at least in one class:

    aaaabbbbbb
    aaaaccc
    bbbbbbccc

    Suppose a function in the base class B has to edit bbbbbb, in AB it occurs at offset=4 in BC it occurs at offset=0. What will the compiler do? Will it instanciate 2 functions, will a offset be passed with the function and then added to each variable? Will the pointer to the object not be passed but a pointer to the offset data passed? What happens with this pointer then?

    The problem i'm facing is having a templitized baseclass which is quite rich of methods which i need to derive quite a lot, would it be smarter to use delegation instead, i'd rather not but I need to know how the compiler (I'm using MS VC++6) handles it.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    i have made a little program to test the memory alignment of the data. basically it works like this:
    class C : public A, public B
    will result in
    aaaabbbbcccc
    while
    class D : public B, public A
    will result in
    bbbbaaaadddd

    now, if you call function D.func(), the this-pointer will point to &D
    if you call the function of A through D (D.parentFunc() ), the this-pointer will point to ((A*)(((char*)&D) + sizeof(B))), or simply, the address of D plus the size of B

    you can compile the attached cpp file and test it if you want. It's a VC++6 console app.
    Attached Files Attached Files

  3. #3

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Thumbs up Thanks

    that clears up a lot, by moving forward the this pointer, it's almost the same as delegation, but that's not much to pay, thanks again, i'll have a look at the project
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width