473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Constructor rules - please verify

1. If you do not declare an explicit constructor, C-sharp will generate a
default paramaterless constructor that calls Object:ctor()

2. All derived class constructors implicity call base(), this can be
verified by looking at the MSIL.

3. If you declare a constructor in the base class with a parameter, but not
the paramaterless one, and you derive from this class, you will get a
compile error, because the C-sharp compiler wants to call base() and it
isn't there in the base class.
Nov 16 '05 #1
4 2549

"Manco" <ma***@dollars.net> wrote in message
news:qItld.862$GV5.701@trnddc04...
1. If you do not declare an explicit constructor, C-sharp will generate a
default paramaterless constructor that calls Object:ctor()
Pretty sure the default constructor calls base:ctor(), not Object:ctor(),
but I haven't checked the MSIL...
2. All derived class constructors implicity call base(), this can be
verified by looking at the MSIL.
yes, unless you provide a different call.
3. If you declare a constructor in the base class with a parameter, but not the paramaterless one, and you derive from this class, you will get a
compile error, because the C-sharp compiler wants to call base() and it
isn't there in the base class.
You can avoid this by simply specifying the parameter to the constructor for
the base class:

Class MyClass : MyBaseClass
{
public MyClass() : base("parameter")
{
return;
}
}

Nov 16 '05 #2
> 1. If you do not declare an explicit constructor, C-sharp will generate a
default paramaterless constructor that calls Object:ctor()
Yep... To be more precise, it will call the parent class's parameterless
constructor, which could be explicitly written or implicitly created.
Eventually it will propagate to Object.ctor(). If the parent class has a
constructor with parameters and no parameterless constructor, a compiler
error will be generated.
2. All derived class constructors implicity call base(), this can be
verified by looking at the MSIL.
Yep.
3. If you declare a constructor in the base class with a parameter, but
not the paramaterless one, and you derive from this class, you will get a
compile error, because the C-sharp compiler wants to call base() and it
isn't there in the base class.


Yep. If you declare a constructor and remove the parameterless one, a
default constructor will not be created implicitly, hence the compiler
error.

I recommend taking a peek at the C# spec...very informative:
http://blogs.msdn.com/csharpfaq/arch.../11/87814.aspx

ShaneB
Nov 16 '05 #3
This looks correct but applies to classes only. Structs don't allow explicit
parameterless constructors whereas classes do. Of course you can't derive
from structs anyway. You won't get a compiler error if you invoke an
overloaded constructor in the base class explicitly, e.g., base(<params>). A
derived class constructor will only implicitly call the default base class
constructor if you don't have an explicit call.

The following compiles without error:

class A //has no default parameterless constructor
{
public A(int i) {}
}
class B: A
{
public B(): base(0) {}
}

Thomas P. Skinner [MVP]
"Manco" <ma***@dollars.net> wrote in message
news:qItld.862$GV5.701@trnddc04...
1. If you do not declare an explicit constructor, C-sharp will generate a
default paramaterless constructor that calls Object:ctor()

2. All derived class constructors implicity call base(), this can be
verified by looking at the MSIL.

3. If you declare a constructor in the base class with a parameter, but
not the paramaterless one, and you derive from this class, you will get a
compile error, because the C-sharp compiler wants to call base() and it
isn't there in the base class.

Nov 16 '05 #4
Manco <ma***@dollars.net> wrote:
1. If you do not declare an explicit constructor, C-sharp will generate a
default paramaterless constructor that calls Object:ctor()
Well, it calls the base class parameterless constructor.
2. All derived class constructors implicity call base(), this can be
verified by looking at the MSIL.
Unless you declare it to call a different constructor, either this(...)
or base(...).
3. If you declare a constructor in the base class with a parameter, but not
the paramaterless one, and you derive from this class, you will get a
compile error, because the C-sharp compiler wants to call base() and it
isn't there in the base class.


Only if you don't explicitly call that constructor from the derived
class too.

See http://www.pobox.com/~skeet/csharp/constructors.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: sb | last post by:
If there is at least one user-defined constructor, no constructors are implicitly declared. struct my_vec : public vector<int> { double foo; my_vec(size_t n) : vector<int>(2*n) {} // oops, no...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
3
by: Martin Vorbrodt | last post by:
In "C++ Templates, The Complete Guide" i read that template copy-con is never default copy constructor, and template assignment-op is never a copy assignment operator. Could someone please explain...
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
7
by: pallav | last post by:
I'm having some trouble with my copy constructor. I've tried using gdb to find the bug, but it seg faults in the destructor. I'm not able to see what I'm doing wrong. Since I'm using pointers, I...
2
by: DaTurk | last post by:
Hi, I was wondering if there is a CLI equivalent to using the this keyword to overload constructors. You know where you would do something like MyClass() : this("something") { }
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
4
by: hocjr | last post by:
I have defined a class A. This class can only be instantiated if certain business rules are met. The following two options come to mind for implementing the business rules check: 1. Place the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.