473,778 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1425
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@micro softs_free_emai l_service.com> wrote in message
news:On******** *****@TK2MSFTNG P12.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******** ******@TK2MSFTN GP11.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@micro softs_free_emai l_service.com> wrote in message
news:On******** *****@TK2MSFTNG P12.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.co m>
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******** ******@TK2MSFTN GP11.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@micro softs_free_emai l_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.co m>
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(i nt xValue, int yValue) : base(xValue, yValue)
{
}
}

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

"Simon" <sh856531@micro softs_free_emai l_service.com> wrote in message
news:ei******** ******@tk2msftn gp13.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
5886
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. Could someone show me how to do what I need? Thanks, Dick Moores rdm@rcblue.com
2
1553
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()') that allows me to have the following: (std::auto_ptr and covariant returns omitted for simplicity.) class Base {
6
2841
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
1530
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: // Assignment operators. virtual base<T>& operator=(const base<T>& rhs) = 0;
7
6625
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
5
3164
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 CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
1
1483
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
2501
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
2146
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" targetNamespace="http://swaMaragatoNS" xmlns:tns="http://swaMaragatoNS" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
11
2708
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> #include <string.h> #define LENGTH 20 int temp, m, n, i, r, base10, true;
0
9629
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10127
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7475
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.