473,583 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloading a constructor and call one from the other

Hi,

I want to overload a constructor of a class. But I want to call the other
one from the second if called. I explain with code because of my english:

public class XMLConfig
{
public XMLConfig(strin g FileName)
{
// here I want to do something with Filename and some init
}

public XMLConfig()
{
// this one should call the other one with a default filename
XMLConfig("defa ult_filename");
}

Can someone explain how to do this ? If I do it as this then I get a
compiler error.

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #1
5 1557
public class XMLConfig
{
public XMLConfig(strin g FileName)
{
// here I want to do something with Filename and some init
}

public XMLConfig()
: this("default_f ilename")
{
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I want to overload a constructor of a class. But I want to call the other
one from the second if called. I explain with code because of my english:

public class XMLConfig
{
public XMLConfig(strin g FileName)
{
// here I want to do something with Filename and some init
}

public XMLConfig()
{
// this one should call the other one with a default filename
XMLConfig("defa ult_filename");
}

Can someone explain how to do this ? If I do it as this then I get a
compiler error.
Nov 16 '05 #2
Hi Richard,
public XMLConfig(): this("default_f ilename")
{
}


Thanks, this compiles, but I was not clear in my question. the 'default
filename' has to be computed in the overloaded constructor. With this coding
I can only give a fixed string to it. Or again I dont see something not :(

for exampel I want to call the create method in overloaded constructor like
somethin this:

XMLConfig(Path. ChangeExtension (Application.Ex ecutablePath, ".xml");

rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #3
Wilfried Mestdagh <Wi************ **@discussions. microsoft.com> wrote:
public XMLConfig(): this("default_f ilename")
{
}


Thanks, this compiles, but I was not clear in my question. the 'default
filename' has to be computed in the overloaded constructor. With this coding
I can only give a fixed string to it. Or again I dont see something not :(

for exampel I want to call the create method in overloaded constructor like
somethin this:

XMLConfig(Path. ChangeExtension (Application.Ex ecutablePath, ".xml");


So do:

public XMLConfig() : this (Path.ChangeExt ension
(Application.Ex ecutablePath, ".xml"))
{
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hi Jon,
public XMLConfig() : this (Path.ChangeExt ension
(Application.Ex ecutablePath, ".xml"))
{
}


Amazing how solutions can be so simple :) Thanks !
One more question because I anger to learn :)

What if I want to have an overloaded constructor that has to execute a bunch
of extra code and then after it calls another constructor ?

eg:
public XMLConfig()
{
// here a lot of code
// after it call another overload constructor
}

thx, Wilfried
Nov 16 '05 #5
Wilfried Mestdagh <Wi************ **@discussions. microsoft.com> wrote:
public XMLConfig() : this (Path.ChangeExt ension
(Application.Ex ecutablePath, ".xml"))
{
}


Amazing how solutions can be so simple :) Thanks !
One more question because I anger to learn :)

What if I want to have an overloaded constructor that has to execute a bunch
of extra code and then after it calls another constructor ?

eg:
public XMLConfig()
{
// here a lot of code
// after it call another overload constructor
}


No, you can't do that - but you *can* call another method in the
parameter part, as shown with the example given before. (That was
Path.ChangeExte nsion, but it could be any other method, including
static methods in the same class.)

Of course, that doesn't help you if you need to call another
constructor which doesn't take any parameters.

If you find constructors restrictive, you may want to think about
making static methods which return an instance.

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

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

Similar topics

14
2658
by: trouble | last post by:
Hi, I have the following code, which is basically overloading + and += for complex numbers. It works fine, but can someone tell me how to use the overloaded + in the definition of += rather than write the function from scratch? Thanks, Ciao.
5
5229
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched...
16
3075
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that...
7
1822
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class xydata with operator- overloaded: class xydata { public:
31
2255
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
6
2843
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually obtain with two classes derived from an abstract base class containing only the interface functions. So, when it's worth to use overloading mechanism...
4
1859
by: Doug | last post by:
Hi I am 'returning' to the learning of C# after a change in jobs and I remember that I used to struggle with the defintion, purpose and function of 'overloading'. When I was beginning with C# I never fully understood what overloading was good for. Could someone please provide me with some words that define overloading, what it is used...
16
2492
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row and _col which are respectively the array to store the data in, the number of rows and the number of columns. _m is of type T, and is size _row *...
22
3601
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 v);
0
7827
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...
0
8184
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. ...
0
8328
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...
0
6581
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...
1
5701
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...
0
5375
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...
0
3845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1434
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1158
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...

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.