Late or early binding?

This is a discussion on Late or early binding? within the C++ Programming forums, part of the General Programming Boards category; I'm stumped on a question which asks, how can you tell if the compiler makes the call in this example ...

  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
    22,267
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,267
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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-22-2009, 12:59 AM
  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, 04: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

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