What’s the behavior when calling an overwritten virtual method in the constructor method of a base class?
2011-05-27 16:26
267 查看
What’s the behavior when calling an overwritten virtual method in the constructor method of a base class? This is a frequently asked question in a technical interview. In practice, we should avoid such case. But we need to know the answer. This is a so confusing problem that C++ and Java behave differently.
For C++, the method in base class, not derived class, is called.
For Java, the method in derived class, not base class, is called. But apparently, the default value of field in derived class is used because it is not initialized at that time. When base class constructor calls virtual methods in derived class, the whole object is a partial object.
For C++, the method in base class, not derived class, is called.
For Java, the method in derived class, not base class, is called. But apparently, the default value of field in derived class is used because it is not initialized at that time. When base class constructor calls virtual methods in derived class, the whole object is a partial object.
//C++ code #include <iostream> using namespace std; class Glyph{ public: Glyph() { cout << "before draw" << endl; draw(); cout << "end draw" << endl; } virtual void draw(){ cout << "Glyph.draw()" << endl; } }; class RoundGlyph:public Glyph{ public: int radius; RoundGlyph(){ cout << "RoundGlyph.RoundGlyph()" << endl; radius = 1; } virtual void draw(){ cout << "RoundGlyph.draw()"<<radius << endl; } }; int main(){ new RoundGlyph(); }
//C++ result before draw Glyph.draw() end draw RoundGlyph.RoundGlyph()
//Java code class Glyph { Glyph(){ System.out.println("Glyph.Glyph() before draw"); draw(); System.out.println("Glyph.Glyph() after draw"); } void draw(){ System.out.println("Glyph.draw()"); } } class RoundGlyph extends Glyph{ private int radus=1; RoundGlyph (){ System.out.println("RoundGlyph()"); } void draw(){ System.out.println("RoundGlyph.draw() radus="+radus); } } public class Poly { public static void main(String[] args){ new RoundGlyph(); } }
//Java result Glyph.Glyph() before draw RoundGlyph.draw() radus=0 Glyph.Glyph() after draw RoundGlyph()
相关文章推荐
- Question 48: In C++, which of the following statements accurately describe a base class destructor calling a virtual function ov
- c++ what happens when a constructor throws an exception and leaves the object in an inconsistent state?
- What happens when you type an URL in the browser and press enter?
- ReactJs 报错 Element type is invalid: expected a string (from built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `Me`.
- An easy instance of using the template class in C++
- What happens when more restrictive access is given to a derived class method in C++?
- An error occurred at line: 307 in the generated java file The code of method _jspService (HttpServle
- android.util.AndroidRuntimeException Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? com.uethinking.microvideo.manag
- What is the difference between @staticmethod and @classmethod in Python?
- How one VBA project can use an object declared in the class module of another project
- How can I protect derived classes from breaking when I change the internal parts of the base class?
- Each child in an array or iterator should have a unique "key" prop. Check the render method of `Tabl
- An implementation of the skip list data structure written in C++
- IllegalArgumentException in class: my.models.base.BaseVo, getter method of property: id
- the memory of virtual base class of C++
- The method below converts an array of objects to a DataTable object in C#.
- Which of the following expression(s) will NOT cause an error when used to replace the ***** in the C
- Why is the size of an empty class not zero in C++?
- Factory methods are static methods that return an instance of the native class(保护CONSTRUCTOR)
- Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for