473,666 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing the base constructor of a derived class

Hi,
I want to be able to make a Master constructor for a class which all overloads of the class constructor would call and thus if I have minor changes to something I only need to make them in the Master and not in each instance, but this does not seem to be allowed. Is there some way to do it?

The specific example is something like this:

I have created a base class "QCResult" from which "LengthQCResult " (and others) inherit. "QCResult" has a couple of attributes which "LengthQCResult " sets in constructors

public LengthQCResult( QCResultType newResult, LengthQCMethod newMethod)

: base(QCType.Len gth, newResult)

{

thisMethod = newMethod;

}

public LengthQCResult( QCResultType newResult, string newMethod)

: base(QCType.Len gth, newResult)

{

switch (newMethod)

{

case "RE":

{

thisMethod = LengthQCMethod. RestrictionDige st;

break;

}

case "PCR":

{

thisMethod = LengthQCMethod. PCR;

break;

}

default:

{

throw new ArgumentExcepti on("QC method is not recognized.");

}

}

}

public LengthQCResult( )

: base(QCType.Len gth)

{

}

I realized I need to add a new attribute (IsDatabased) to the base class, but this will be "false" by default and I don't want to have it be a parameter in all constructors for all derived classes. So, I want to make a derived Master constructor (or something like that) which has all the possible parameters and have each of the constructor overloads call the Master. It would look something like:

public LengthQCResult( QCResultType newResult, LengthQCMethod newMethod, bool isInDatabase)

: base(QCType.Len gth, newResult, isInDatabase)

{

thisMethod = newMethod;

}

And the other constructors would call it:

public LengthQCResult( QCResultType newResult, LengthQCMethod newMethod)

{

this = new LengthQCResult( newResult, newMethod, false);

}

and so forth.

When I try this, I get an error "Cannot assign to '<this>' because it is read-only".

In base classes, I don't have this problem because I can just create a Private MasterConstruct or type method which all Public constructors call, but I don't know of a way to access the base constructor in a derived class except from the definition line of a constructor. Is there a way to do that?

Thanks!

Ethan

Ethan Strauss Ph.D.
Bioinformatics Scientist
Promega Corporation
2800 Woods Hollow Rd.
Madison, WI 53711
608-274-4330
800-356-9526
et***********@p romega.com

Jun 13 '07 #1
2 1674
I think you are looking for:

public LengthQCResult( QCResultType newResult, LengthQCMethod
newMethod)
: this(newResult, newMethod, false)
{
}

Marc
Jun 13 '07 #2
Thanks! That's it.
Ethan

"Marc Gravell" <ma**********@g mail.comwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
>I think you are looking for:

public LengthQCResult( QCResultType newResult, LengthQCMethod newMethod)
: this(newResult, newMethod, false)
{
}

Marc

Jun 13 '07 #3

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

Similar topics

7
1621
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base' copy constructor Calling 'func(d_handle)':
3
2340
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base class definition. Therefore I can't specify the return type of the function, that returns a pointer to the derived class, because the derived class is not yet known at that point. Hov can I solve this problem? In a nice manner :)
3
3488
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ... more protected fields }
12
5798
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. The subclass's constructor sets this variable to a real value. So first the variable is set to 0, the variable is never used before it once again gets set to a real value. It's a waste. Any ideas anyone?
4
5241
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not attempt to access the object -- since m_object is not actually created and initizialized until after the base constructor has been called. Any thoughts on the practice below? class Base { public:
6
2489
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;
26
5354
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
4
1616
by: Angus | last post by:
I have a base class which has a function GetThingID which returns a integer value. The ctor of the base class sets the value. In my derived class I want to use this function GetThingID. I have NOT decalred a function GetThingID in my derived class. How how do I construct my derived class to pass a value so as to populate the private base class member?
4
2939
by: softwaredoug | last post by:
Here is some test code I've been attempting to compile (Visual Studio 2003) test.h: class Base { protected: Base() {} public:
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
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...
0
8640
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...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
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
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.