473,396 Members | 1,765 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,396 software developers and data experts.

base() question :-)

Hi everyone,

Quick question:

If I don't use base() in a subclass's construcor, will the base classes
constructor be called at any point. It's just, I would have thought that the
base class constructor would always need to be called before anything else
seeing as the subclass may well depend on functionality and variables
created in the base class.

When base() is used, does it get called before anything else or is there a
way to control when it gets called.

Simon
Jul 21 '05 #1
8 1402
If you don't call it expcitly, it will never be called. If you do want it
called, it will be called prior to any other code in the subclass's
constructor.

"Simon" <sh856531@microsofts_free_email_service.com> wrote in message
news:On*************@TK2MSFTNGP12.phx.gbl...
Hi everyone,

Quick question:

If I don't use base() in a subclass's construcor, will the base classes
constructor be called at any point. It's just, I would have thought that the base class constructor would always need to be called before anything else
seeing as the subclass may well depend on functionality and variables
created in the base class.

When base() is used, does it get called before anything else or is there a
way to control when it gets called.

Simon

Jul 21 '05 #2
That is not correct.
The base class constructor is always called.
If you do not explicitly specify which one,
the default constructor is called.
The default constructor is the one which do not have parameters.
So, we can verify this by not providing a default constructor for a test
base class:
class Base
{
public Base( int id ) {}
}
class Concrete : Base
{
public Concrete() {}
}

This will result in compilation error:
"No overload for method Base takes 0 arguments"
However, if we remove the parameter from Base constructor,
the code will compile.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Marina" <so*****@nospam.com> wrote in message
news:eZ**************@TK2MSFTNGP11.phx.gbl...
If you don't call it expcitly, it will never be called. If you do want it
called, it will be called prior to any other code in the subclass's
constructor.

"Simon" <sh856531@microsofts_free_email_service.com> wrote in message
news:On*************@TK2MSFTNGP12.phx.gbl...
Hi everyone,

Quick question:

If I don't use base() in a subclass's construcor, will the base classes
constructor be called at any point. It's just, I would have thought that

the
base class constructor would always need to be called before anything
else
seeing as the subclass may well depend on functionality and variables
created in the base class.

When base() is used, does it get called before anything else or is there
a
way to control when it gets called.

Simon


Jul 21 '05 #3
Marina <so*****@nospam.com> wrote:
If you don't call it expcitly, it will never be called.


From the C# spec, section 17.10.1:

<quote>
If an instance constructor has no constructor initializer, a
constructor initializer of the form base() is implicitly provided.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
"Marina" <so*****@nospam.com> wrote in message
news:eZ**************@TK2MSFTNGP11.phx.gbl...
If you don't call it expcitly, it will never be called. If you do want it
called, it will be called prior to any other code in the subclass's
constructor.

Howdy, I just thought I would jump in here too with my two cents. :>

The advantage of this is with constructor chaining (I'm sure there are more
reasons, but this is one I like to use it for :>). In other words, you're
overloading constructors, gotta love OOP! :>

Take a look here for Chaining.
http://www.c-sharpcorner.com/Languag...orsInCSRVS.asp
Jul 21 '05 #5
Hi everyone,

If the base constructor is implicitly called whether you specify it or not,
then what is the point of base()?

Thanks for your help so far everyone. Much appreciated

tce
Jul 21 '05 #6
Simon <sh856531@microsofts_free_email_service.com> wrote:
If the base constructor is implicitly called whether you specify it or not,
then what is the point of base()?


Only clarity - just specifying it (without any parameters) is basically
redundant.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7
Because sometimes you want to call a base constructor that has variables in
it.
For example:

public class Point
{
public int X;
public int Y;

public Point(int xValue, int yValue)
{
X = xValue;
Y = yValue;
}
}

public class ExtendedPoint : Point
{
public ExtendedPoint(int xValue, int yValue) : base(xValue, yValue)
{
}
}

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"Simon" <sh856531@microsofts_free_email_service.com> wrote in message
news:ei**************@tk2msftngp13.phx.gbl...
Hi everyone,

If the base constructor is implicitly called whether you specify it or
not, then what is the point of base()?

Thanks for your help so far everyone. Much appreciated

tce

Jul 21 '05 #8
Gotcha. Thanks guys

Simon
Jul 21 '05 #9

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

Similar topics

52
by: Dick Moores | last post by:
I need to figure out how to compute pi to base 12, to as many digits as possible. I found this reference, <http://mathworld.wolfram.com/Base.html>, but I really don't understand it well enough....
2
by: Stephan Br?nnimann | last post by:
Dear all thanks to Scott Meyers (ME C++, item 33) we know assignment operators should be protected in the base class. Is there a common pattern (similar to virtual construction via `clone()')...
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
7
by: Jef Driesen | last post by:
Suppose I have an abstract base class that looks like this: template <typename T> class base { public: // Typedefs typedef double value_type; typedef std::size_t size_type; public: //...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
1
by: miben | last post by:
I want to create a new inherited class givin its base. For example: class Base { public: void operator =(const Base &base) { // copy base items } }; class Inherits: public Base {
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
0
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.