Dynamic binding

This is a discussion on Dynamic binding within the C++ Programming forums, part of the General Programming Boards category; I understand the advantages and the uses of dynamic binding. I also understand how it works and what I need ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,576

    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?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    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 05:54 PM. Reason: I was doing it wrong :p

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    Portugal
    Posts
    7,576
    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 05:16 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Portugal
    Posts
    7,576
    Thanks Chaos. Definitely I need to read more on how compilers work...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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, 04: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, 04:31 PM
  4. dynamic or static binding
    By noob2c in forum C++ Programming
    Replies: 1
    Last Post: 08-06-2003, 02:43 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 08:51 PM

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