473,407 Members | 2,320 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,407 software developers and data experts.

Ensuring a base constructor is called?

If I have a base class that has one constructor, is the only way that that
constructor will get called be by having no constructors in my derived
classes or explicitly calling base() in my derived constructors?
Illustration:

abstract class whatever
{
public whatever()
{
//code here I want to run
}
}

class derivedClass : whatever
{
public derivedClass() : base()
{
}
}

That ":base()" is necessary in this case, correct? I guess what I'm more
looking for is some mechansism that will ensure that code in my base classes
is always executed when a derived object is instantiated. I'm mostly trying
to compensate for a behavioral problem here, fearing that other developers
won't remember to put the ": base()" on their constructors. :-/

TIA,

Josh
Jul 30 '08 #1
2 1586
On Jul 30, 5:08 pm, "Josh Valino" <no spam pleasewrote:

<snip>
That ":base()" is necessary in this case, correct?
No. If you don't specify either :base(...) or :this(...) then the
compiler effectively inserts :base() for you.
I guess what I'm more
looking for is some mechansism that will ensure that code in my base classes
is always executed when a derived object is instantiated. I'm mostly trying
to compensate for a behavioral problem here, fearing that other developers
won't remember to put the ": base()" on their constructors. :-/
They don't need to - the base class constructor will always be called.

Jon
Jul 30 '08 #2
On Jul 30, 12:08*pm, "Josh Valino" <no spam pleasewrote:
If I have a base class that has one constructor, is the only way that that
constructor will get called be by having no constructors in my derived
classes or explicitly calling base() in my derived constructors?
Illustration:

abstract class whatever
{
* public whatever()
* {
* * //code here I want to run
* }

}

class derivedClass : whatever
{
* public derivedClass() : base()
* {
* }

}

That ":base()" is necessary in this case, correct? *
No, the compiler will add a call for you.
>I guess what I'm more
looking for is some mechansism that will ensure that code in my base classes
is always executed when a derived object is instantiated. *I'm mostly trying
to compensate for a behavioral problem here, fearing that other developers
won't remember to put the ": base()" on their constructors. *:-/
the base will always be constructed, if you do not especify one
version, the compiler will try to call the default (no paremeter) if
not exist you get an error
Jul 30 '08 #3

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

Similar topics

8
by: Simon | last post by:
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...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
1
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
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;
1
by: mwebel | last post by:
Hi ikind of remember this being easy but cant find it on google: i have a base class and a derived class. The base class has two constructors normal and int overloaded one. thing is i want the...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
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
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
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
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,...
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
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...

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.