www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Bug 91] New: Inherited classes require base class to have a default constructor.

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=91

           Summary: Inherited classes require base class to have a default
                    constructor.
           Product: D
           Version: 0.152
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: eric96 gmail.com


Let me know if this is expected behavior or some rule I don't know about, but I
think it's a bug.  If you insert the default constructor "this(){}" in A, it
compiles fine.

// Looks like it requires the base class to have a default constructor.
class A
{  this (int i) // constructor yage.a.this(int) does not match argument types
()
   {}
}
class B : A
{  this (int i)
   {}
}

Also, Windows XP SP2; Haven't tried it on Linux yet.


-- 
Apr 06 2006
next sibling parent =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
d-bugmail puremagic.com wrote:
 http://d.puremagic.com/bugzilla/show_bug.cgi?id=91
 
            Summary: Inherited classes require base class to have a default
                     constructor.
            Product: D
            Version: 0.152
           Platform: PC
         OS/Version: Windows
             Status: NEW
           Severity: minor
           Priority: P2
          Component: DMD
         AssignedTo: bugzilla digitalmars.com
         ReportedBy: eric96 gmail.com
 
 
 Let me know if this is expected behavior or some rule I don't know about, but I
 think it's a bug.  If you insert the default constructor "this(){}" in A, it
 compiles fine.
 
 // Looks like it requires the base class to have a default constructor.
 class A
 {  this (int i) // constructor yage.a.this(int) does not match argument types
 ()
    {}
 }
 class B : A
 {  this (int i)
    {}
 }
This is correct behaviour. Class B implicitly calls the default constructor and if one is not found, an appropriate parent class constructor call should be explicitly defined in B's constructor. -- Jari-Matti
Apr 07 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=91


smjg iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg iname.com
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID





The compiler is correctly diagnosing an error.  The derived class needs a base
class constructor to call.  The keyword "super", when used in a constructor,
denotes a constructor of the base class.

class B : A {
    this (int i) {
        super(i);
    }
}

If no call to super is present, then it looks for a default constructor and
calls that.  Hence if there's no default constructor in the base class, then
you must call super when calling the derived class.


-- 
Apr 07 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=91






I'm familiar with super(), but I incorrectly thought that I could completely
override the constructor in the parent class, as it works with other methods. 
Is this behavior consistent with other oo languages?


-- 
Apr 07 2006
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=91






Yes, including C++ and Java.  If anybody could circumvent the requirement to
use a constructor simply by creating a derived a class, it would defeat the
point.

You can, however, put a protected constructor in the base class.  This is a
constructor created specifically for derived classes to base their constructors
on.  You would be able to completely override* a constructor if the base class
has a protected constructor that does nothing.  But can you think of an example
in which this would make sense?

* Actually, constructors don't override as such.  A constructor is a member
only of the class in which it is defined, not of any derived classes.  Hence if
the base class has no default constructor, then when deriving a class from it
you must explicitly define a constructor.


-- 
Apr 07 2006