class, instance, and default constructors 
Author Message
 class, instance, and default constructors

I'm confused in my reading through the C# reference docs.  Basically I'm
trying to understand what gets inherited.  If the base class has a
parameterless constructor is that the class constructor?  Is that what gets
called when the derived class has no constructors (or at least doesn't have a
parameterless constructor)?  Is the default constructor called if neither the
base nor the derived class has a parameterless constructor?  Will this happen
even if they have other constructors?  Are class and instance constructors
names for the same thing?  I can find instance constructor in the index but
not class constructor yet I find uses of class constructor that don't really
define it.


Fri, 19 Nov 2004 23:16:08 GMT  
 class, instance, and default constructors
I'm confused in my reading through the C# reference docs.  Basically I'm
trying to understand what gets inherited.

Constructors never gets inherited. A class have to have its constructor or
the parameterless constructor is created for you, which calls the base
classes paramerter less constructor.

 If the base class has a
parameterless constructor is that the class constructor?  Is that what gets
called when the derived class has no constructors (or at least doesn't have
a
parameterless constructor)?  Is the default constructor called if neither
the
base nor the derived class has a parameterless constructor?

If the base class a parameterless constructor, thats the constructor for
the case class. Any class derived from it should have a constructor which
call the parameter less constructor ot any other overloaded constructor. IF
a base class has a parameter less constructor and u dont provide a
constructor in the derived class then a parameter less constructor is
created for you. If the base class has a parameterized constructor and you
dont provide a constructor in the derived class then it is a compiler error.

 Are class and instance constructors
names for the same thing?  I can find instance constructor in the index but
not class constructor yet I find uses of class constructor that don't
really
define it.

Yes class constructor and instance constructor are on and hte same thing.
class constructor is called each time an instance is created and in that
context its refered to as instance constructor.

Hope this helps.



Sat, 20 Nov 2004 08:08:40 GMT  
 class, instance, and default constructors
Quote:

> Constructors never gets inherited. A class have to have its constructor or
> the parameterless constructor is created for you, which calls the base
> classes paramerter less constructor.

But in that sense, the parameterless constructor IS inherited.  The docs says
that if a class (I assume here they must mean a base class) doesn't have a
parameterless constructor then one is created and default values are use to
initialize the object fields (this is what I think they're calling a default
constructor).  But I didn't find any place where it says a derived class
without a parameterless constructor uses the base class's, although that's
the behavior I thought I'd observed.


Sat, 20 Nov 2004 02:40:38 GMT  
 class, instance, and default constructors

Quote:
>> Constructors never gets inherited. A class have to have its constructor
or
>> the parameterless constructor is created for you, which calls the base
>> classes paramerter less constructor.
>But in that sense, the parameterless constructor IS inherited.  The docs
says
>that if a class (I assume here they must mean a base class) doesn't have a
>parameterless constructor then one is created and default values are use
to
>initialize the object fields (this is what I think they're calling a
default
>constructor).  But I didn't find any place where it says a derived class
>without a parameterless constructor uses the base class's, although that's
>the behavior I thought I'd observed.

Well, there is a difference between inherited and being called, though the
results may be the same in terms of initialization.

If the constructor was to be inherited, then a parameterless constructor
created will call the inherited constructor, or we can call one of hte
inherited constructor if we supply the constructor in the derived class. If
you really look at it we call the base class constructor even if we provide
a constructor.



Sun, 21 Nov 2004 07:14:38 GMT  
 class, instance, and default constructors
Quote:

> Well, there is a difference between inherited and being called, though the
> results may be the same in terms of initialization.

Yes, I guess it really is just semantics.  But as I've been thinking about
all this I'm not sure I understand the language design reasons for not
inheriting constructors in the first place?


Sun, 21 Nov 2004 02:47:42 GMT  
 class, instance, and default constructors
Constructors are executed only once when the object is created, sole
purpose of constructors is to initialize the members of the object. you can
call a base class constructor to initialize the base class members, and we
dont need constructors to do more than that. Its is never inherited because
its not need for any of the OOP principals, we dont need to override,
polymorpic or reuse them...

thanks



Mon, 22 Nov 2004 02:19:33 GMT  
 class, instance, and default constructors
Quote:

> Constructors are executed only once when the object is created, sole
> purpose of constructors is to initialize the members of the object. you can
> call a base class constructor to initialize the base class members, and we
> dont need constructors to do more than that. Its is never inherited because
> its not need for any of the OOP principals, we dont need to override,
> polymorpic or reuse them...

I somewhat disagree with this.  Think of a base class which has a dozen
constructors (most likely because of the fact that C# also doesn't support
default parameters).  All I want to do is add one additional feature which
requires a different constructor signature but doesn't affect the others and I
want the user to have access to all the other constructors.  This means that
in my derived class I have to retype constructors to mimic the signatures of
all the base classes and use the base class initializer.  That gets very
tedious and is sure prone to errors.  It'd be much simpler if when I didn't
supply a constructor for a particular signature that the base class's was just
used.


Sun, 21 Nov 2004 23:30:27 GMT  
 class, instance, and default constructors

--
Visit the C# product team at http://www.gotdotnet.com/team/csharp

This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:

> > Constructors are executed only once when the object is created, sole
> > purpose of constructors is to initialize the members of the object. you
can
> > call a base class constructor to initialize the base class members, and
we
> > dont need constructors to do more than that. Its is never inherited
because
> > its not need for any of the OOP principals, we dont need to override,
> > polymorpic or reuse them...

> I somewhat disagree with this.  Think of a base class which has a dozen
> constructors (most likely because of the fact that C# also doesn't support
> default parameters).  All I want to do is add one additional feature which
> requires a different constructor signature but doesn't affect the others
and I
> want the user to have access to all the other constructors.  This means
that
> in my derived class I have to retype constructors to mimic the signatures
of
> all the base classes and use the base class initializer.  That gets very
> tedious and is sure prone to errors.  It'd be much simpler if when I
didn't
> supply a constructor for a particular signature that the base class's was
just
> used.

I agree that this is somewhat tedious, though I think a class with a dozen
constructors is poor design, and that properties should be used instead.

But just adding one feature is a rare occurance. It's much more common to be
adding more complexity, complexity that requires the constructor to set
things up correctly. If you just used the base class' constructor, that
would be *very* bad, as you'd get objects that weren't correctly
constructed.



Tue, 23 Nov 2004 01:15:57 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. deleting a class instance from its constructor

2. Non Default Constructors Called From Definition Classes

3. Calling Default Constructor from another constructor

4. Can you call a default constructor from an overloaded constructor

5. Finding Class Instance from Interface Instance

6. Calling Constructor on Existing Instance?

7. Missing calls to default constructor in unmanaged VC++ 7.0

8. CDialog getting CWnd reference through default constructor?

9. C2512 Error with ATL COM: no appropriate default constructor available

10. Non-default constructors for COM objects

11. no appropriate default constructor?

12. Reusing (the default) constructor

 

 
Powered by phpBB® Forum Software