dynamic-binding

C++ overridden function not called

北战南征 提交于 2019-12-02 11:02:36
I am running into an issue where an overloaded function is not called, and the base function is called instead. I suspect this is related to how things are split between the project files. In files obj1.h/obj1.cpp I have something like this class obj1{ public: void print(); }; void obj1::print(){ cout << "obj1::print()"; } In files obj2.h/obj2.cpp I have something like this: #include "obj1.h" class obj2 : public obj1{ public: void print(); }; void obj2::print(){ cout << "obj2::print()"; } In separate files, I do something like this: #include "obj1.h" class obj3{ public: vector<obj1*> objlist;

How is dynamic binding implemented in Java?

无人久伴 提交于 2019-12-01 00:30:16
I am aware that in C++, there is a virtual pointer in each instance pointing to a virtual table. But how is dynamic binding implemented in Java? I guess I will answer my own question. Basically, an object stores a reference to its class object, where the dynamic binding will be forwarded from an object to. Just to be picky, it's a real pointer to a real table of virtual functions, hence the name "virtual function table," often abbreviated as "vft". It is also commonly abbreviated "vtbl" which doesn't help matters. Java probably does something fairly similar. 来源: https://stackoverflow.com

How is dynamic binding implemented in Java?

♀尐吖头ヾ 提交于 2019-11-30 20:04:49
问题 I am aware that in C++, there is a virtual pointer in each instance pointing to a virtual table. But how is dynamic binding implemented in Java? 回答1: I guess I will answer my own question. Basically, an object stores a reference to its class object, where the dynamic binding will be forwarded from an object to. 回答2: Just to be picky, it's a real pointer to a real table of virtual functions, hence the name "virtual function table," often abbreviated as "vft". It is also commonly abbreviated

Invoking virtual function and pure-virtual function from a constructor

泪湿孤枕 提交于 2019-11-30 15:19:57
When i invoke a virtual function from a base constructor, the compiler does not give any error. But when i invoke a pure-virtual function from the base class constructor, it gives compilation error. Consider the sample program below: #include <iostream> using namespace std; class base { public: void virtual virtualfunc() = 0; //void virtual virtualfunc(); base() { virtualfunc(); } }; void base::virtualfunc() { cout << " pvf in base class\n"; } class derived : public base { public: void virtualfunc() { cout << "vf in derived class\n"; } }; int main() { derived d; base *bptr = &d; bptr-

Question about Java overloading & dynamic binding

亡梦爱人 提交于 2019-11-30 11:47:20
问题 In the code below, how does first and second print statements print out SubObj?? Do top and sub point to the same Sub class? class Top { public String f(Object o) {return "Top";} } class Sub extends Top { public String f(String s) {return "Sub";} public String f(Object o) {return "SubObj";} } public class Test { public static void main(String[] args) { Sub sub = new Sub(); Top top = sub; String str = "Something"; Object obj = str; System.out.println(top.f(obj)); System.out.println(top.f(str))

Objective-C uses dynamic binding, but how?

那年仲夏 提交于 2019-11-30 05:28:56
I know that Objective-C uses dynamic binding for all method calls. How is this implemented? Does objective-c "turn into C code" before compilation and just use (void*) pointers for everything? Louis Gerbarg Conceptually, what is going on is that there is a dispatcher library (commonly referred to as the Objective C runtime), and the compiler converts something like this: [myObject myMethodWithArg:a andArg:b ]; into //Not exactly correct, but close enough for this objc_msgSend(myObject, "myMethodWithArg:andArg:", a, b); And then the runtime deals with all the binding and dispatch, finds an

Question about Java overloading & dynamic binding

人走茶凉 提交于 2019-11-30 01:58:45
In the code below, how does first and second print statements print out SubObj?? Do top and sub point to the same Sub class? class Top { public String f(Object o) {return "Top";} } class Sub extends Top { public String f(String s) {return "Sub";} public String f(Object o) {return "SubObj";} } public class Test { public static void main(String[] args) { Sub sub = new Sub(); Top top = sub; String str = "Something"; Object obj = str; System.out.println(top.f(obj)); System.out.println(top.f(str)); System.out.println(sub.f(obj)); System.out.println(sub.f(str)); } } Above code returns below result.

Run-time Polymorphism in Java without “abstract”?

拥有回忆 提交于 2019-11-29 17:24:25
I was going over the official Oracle tutorial where it introduces the idea of polymorphism with the example of a class hierarchy of 3 classes; Bicycle being the superclass, and MountainBike and RoadBike being 2 subclasses. It shows how the 2 subclasses override a method "printDescription" declared in Bicycle, by declaring different versions of it. And finally, toward the end, the tutorial mentions the Java Virtual Machine (JVM) calls the appropriate method for the object that is referred to in each variable. But, nowhere does the tutorial on polymorphism mention the concept of "abstract"

Run-time Polymorphism in Java without “abstract”?

半城伤御伤魂 提交于 2019-11-28 12:03:14
问题 I was going over the official Oracle tutorial where it introduces the idea of polymorphism with the example of a class hierarchy of 3 classes; Bicycle being the superclass, and MountainBike and RoadBike being 2 subclasses. It shows how the 2 subclasses override a method "printDescription" declared in Bicycle, by declaring different versions of it. And finally, toward the end, the tutorial mentions the Java Virtual Machine (JVM) calls the appropriate method for the object that is referred to

When to mark a function in C++ as a virtual?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 21:29:45
Because of C++ nature of static-binding for methods, this affects the polymorphic calls. From Wikipedia: Although the overhead involved in this dispatch mechanism is low, it may still be significant for some application areas that the language was designed to target. For this reason, Bjarne Stroustrup, the designer of C++, elected to make dynamic dispatch optional and non-default. Only functions declared with the virtual keyword will be dispatched based on the runtime type of the object; other functions will be dispatched based on the object's static type. So the code: Polygon* p = new