Thread: Dynamic binding

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Dynamic binding

    I understand the advantages and the uses of dynamic binding. I also understand how it works and what I need to do to make it work. In short, only member functions can be dynamically bound and the call has to be made through a reference or pointer to a base class.

    I coded some toy classes to test this and understand the concept. All is well...

    However, being this the cornerstone of polymorphism, and such a fundamental concept, I would like to understand it better.

    The problem I have is that I cannot abstract myself enough to understand the following statement:

    Quote Originally Posted by C++ Primer
    The crucial point about references and pointers to base-class types is that the static type, the type of the reference or pointer, which is knowable at compile time, and the dynamic type, the type of the object to which the pointer or reference is bound, which is knowable only at run time may differ.
    I cannot understand why is that the compiler cannot know the pointer or reference to a base class type is actually pointing or referencing a derived type. Why is this information only obtainable at run-time?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I know that you understand how to code dynamic binding but I will probably need an example to explain.
    The base class is a part of all it's child classes, but in memory, the base class part is always stored first. Then parts of the derived classes are stored. This is why a base class pointer can point to any of it's derived classes as long as you use public inheritance (and protected too, I believe). Due to the flexibility of such a pointer, it can actually point to a base class or be reassigned to a derived one at any time, therefore the compiler can't know in advance what it points to.
    Code:
    struct BoardMember {
        std::string m_name;
        BoardMember::BoardMember( std::string s = "citizen" ) : m_name(s) { };
    };
    
    struct Mod : public BoardMember {
       bool isVIP;
       Mod::Mod( ) : BoardMember("Prelude"), isVIP(true) { };
    };   
    
    int main( ) {
       BoardMember *Prelude;
       // We aren't sure that this will be me or her until runtime when the memory
       // is actually allocated, and you remember how all memory is allocated at run time?
       Prelude = new Mod;
       std::cout << Prelude->m_name << " is VIP" << std::endl;
    }
    Last edited by whiteflags; 07-09-2006 at 04:54 PM. Reason: I was doing it wrong :p

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why is this information only obtainable at run-time?
    With a sufficiently intelligent compiler, it's certainly possible to determine the dynamic type at compile-time provided you have access to all of the source (which I'm guessing is what you're thinking of, and what prompted the question). But that's an incredibly difficult feat, and the C++ standard doesn't cater to the status quo. We're talking about a compiler that borders on AI quality parsing algorithms. And that's without considering black boxes, which throw a very large and awkwardly shaped monkey wrench into the gears.

    Is it possible to determine the dynamic type at compile-time for all cases? Possibly, but it would probably take someone a great deal smarter than I am to figure out how. And the result would be dreadfully slow.
    My best code is written with the delete key.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Gotcha!

    I was thinking in terms of static memory.

    Code:
    BoardMember Citizen;
    Mod Prelude;
    BoardMember* ptr = &Citizen;
    ptr = &Prelude;
    Under this situation the dynamic type of the pointer is known at compile-time.

    Didn't occur to me that quote was in the context of dynamic allocated memory.

    Thanks

    EDIT: Ah! True Prelude. I guess my question comes also from the fact I understand very little of how compilers operate. Thanks for the info.
    Last edited by Mario F.; 07-09-2006 at 04:16 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Mario F.
    Gotcha!

    I was thinking in terms of static memory.

    Under this situation the dynamic type of the pointer is known at compile-time.

    Didn't occur to me that quote was in the context of dynamic allocated memory.
    even with static memory it can sometimes be impossible to know.
    Code:
    Base b; 
    Derived d; // assume d derived from b and overrides virtual func "DoStuff"
    
    Base *pB = NULL;
    
    char x;
    cin >> x;
    if (x == 'b')
        pB = &b;
    else if (x == 'd')
        pB = &d;
    
    if (pB)
        pB->DoStuff();
    All stack memory and still impossible to know at compile time.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Thanks Chaos. Definitely I need to read more on how compilers work...
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boost::shared_ptr and dynamic binding
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 03:50 PM
  2. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  3. dynamic binding
    By freethenet in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-26-2004, 03:31 PM
  4. dynamic or static binding
    By noob2c in forum C++ Programming
    Replies: 1
    Last Post: 08-06-2003, 01:43 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 08:51 PM