Were you ever convinced that you'd found a compiler bug...

Started by
11 comments, last by Succinct 22 years, 8 months ago
quote:Original post by Dormeur
I don''t know how Java does it, but it works. I''m programming in Java daily at work, and I''m using this feature all the time. I think Java just creates the entire object, including vtables, before actually calling any constructors on the object. Perhaps it''s because Java supports RTTI (Run-time Type Identification) by default, which makes it possible to retrieve info on the classes themselves at runtime, whereas C++ compilers require you to enable it (possibly for performance reasons).

I really think this is so essential to OO programming, that I don''t understand why C++ doesn''t support it.

Dormeur


The problem is not the VTable, the problem is having valid objects in those virtual functions.

I will do some test with java later today and post my results.
Advertisement
The first thing a Java constructor does is call the base classes'' constructor (eg. super(); ).

You can override which base class constructor is called by calling it yourself...Suppose there''s two constructor forms for the base class, one takes a string, one doesn''t, you can be sure the String form is called by calling it yourself:

Derived()
{
super("Blah!");
}


If you don''t specify a constructor form to call, Java will automatically insert a call to the default base constructor for you as the first thing it does, before it runs any other code in your constructor.

Another note..if you DO want to call a specific base constructor, it has to be the first thing you do in the Derived constructor, because Java guarantees that the base constructor will be called before the rest of the Derived constructor is executed:


Derived()
{
i = 0;
super("Blah!");
}


Will result in a compile-time error because Java has to be sure the base class constructor is called before the derived class does anything else.



Java does call the virtual function of the dynamic cast, but if remove the if ( myobj == null ), the code will crap out because myobj does exist until the derived class constructor is called.

It just design decision in language, Java and C++ took different approach.

C++ said, well when you are in the base class constructor we know that the derived class is not created, so when we call a virtual function, it is like we would try to access class member of the derived class. This is obviously pretty bad, so will call the function of the base class.

Java said, we will call the right one, but be carefull and make sure all your object exist when you call a virtual function.

  class TestObj{     public TestObj()     {    	 System.out.println( "TestObj::TestObj()" );     }     public void test()     {         System.out.println( "TestObj::test()" );     }}class Base{       public Base()       {           System.out.println( "Base::Base()" );           doStuff();       }	public void doStuff()        {            System.out.println( "Base::doStuff" );        }}class Elem extends Base{	public Elem()        {             System.out.println( "Base::Elem" );                     }       public void doStuff()       { 		System.out.println( "Elem::doStuff()" );                		if ( myobj != null )                {			myobj.test();		}       }     private TestObj myobj = new TestObj();}       public class testApp{	public static void main(String Args[])	{		Elem e = new Elem();		e.doStuff();	}		}  

This topic is closed to new replies.

Advertisement