Thread: Late or early binding?

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Late or early binding?

    I'm stumped on a question which asks, how can you tell if the compiler makes the call in this example using late or early binding? The call in question is the third one, under the comment "Early binding (probably)." I wouldn't put it past the author for the solution to be to look at the assembly, but I'm just checking to see if there's another way (and if someone can give me a clue)...

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Pet {
    public:
      virtual string speak() const { return ""; }
    };
    
    class Dog : public Pet {
    public:
      string speak() const { return "Bark!"; }
    };
    
    int main() {
      Dog ralph;
      Pet* p1 = &ralph;
      Pet& p2 = ralph;
      Pet p3;
      // Late binding for both:
      cout << "p1->speak() = " << p1->speak() <<endl;
      cout << "p2.speak() = " << p2.speak() << endl;
      // Early binding (probably):
      cout << "p3.speak() = " << p3.speak() << endl;
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    p3 is a Pet object. There is no way it could be anything else, short of changing its declaration. Therefore, p3.speak() invokes Pet::speak(), and this can be resolved at compile time.

    I suppose that p1->speak() and p2.speak() could also be determined at compile time in this case, but I am not sure if a compiler would actually be allowed to do that as an optimisation. But generally, the idea is that polymorphism in C++ works through pointers and references, so p1 is a pointer to a Pet, but whether it actually points to a Pet or to an object of a subclass of Pet may need to be resolved at run time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    I understand how late binding works and what it's used on, but the author has mentioned that it's up to the compiler whether or not it performs late or early binding when it makes a call to a virtual function through an object. His exact words on the matter are:

    ...early binding is probably used. However, if the compiler doesn’t want to work so hard, it can still use late binding and the same behavior will occur.
    And the question in this case was, how can you tell if it's one way or the other.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sharke
    but the author has mentioned that it's up to the compiler whether or not it performs late or early binding when it makes a call to a virtual function through an object
    Right. It seems that this notion of early/late binding is regarded as implementation detail by the standard, hence the author is correct. If this is so, then I would argue that the claim of "late binding for both" is also making an assumption, since in theory the types of what p1 and p2 refer to can be determined at compile time.

    Quote Originally Posted by Sharke
    And the question in this case was, how can you tell if it's one way or the other.
    Your guess of checking assembly output is probably correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by laserlight View Post
    Your guess of checking assembly output is probably correct.
    That's that then. Onto the next question! Assembly output still confusing to me at this point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Late Binding
    By xddxogm3 in forum C# Programming
    Replies: 2
    Last Post: 05-21-2009, 11:59 PM
  2. inet_aton()... Noob needs help with sockets :|
    By Maz in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2005, 04:33 PM
  3. dynamic binding
    By freethenet in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-26-2004, 03:31 PM
  4. late binding with template function
    By clf in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2002, 01:13 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 08:51 PM