| Programming/Scripts Perl, Python, C/C++, Java, CGI, shell, etc. Share your code! |

08-06-2005, 05:59 PM
|
 |
Modurator Wannabe
|
|
Join Date: Mar 2003
Location: Tampa, FL USA
Posts: 2,193
|
|
|
C++ constructor overloading
Is it possible to call a separate constructor from a default constructor?
Here is an example of what I mean (this code does not compile):
Code:
class Test
{
private:
int i;
public:
Test(int i)
{
this->i = i;
}
Test()
{
this->Test(2)
}
};
Obviously, I could put initialization in a separate ("init") method, but that is undesirable.
|

08-07-2005, 01:50 PM
|
|
Moderator
|
|
Join Date: Apr 2001
Location: SF Bay Area, CA
Posts: 14,950
|
|
Well, first, you're missing a semicolon.
But this seems to work on my machine. Use:
in the parameterless constructor. (I.e., remove the "this->" part.)
Maybe default values for the constructor's parameters would help, too? Maybe the actual program you're doing this with is much more complex, though.
__________________
"Quando omni flunkus moritati" -- Possum Lodge motto -- Loose translation: "When all else fails, play dead."
Registered Linux User #219692
Please take a look at the Posting Guidelines, to help us help you!
|

08-08-2005, 08:36 AM
|
|
Bad Speller ^^
|
|
Join Date: May 2001
Location: Uh, I'm somewhere where I don't know where I am.
Posts: 1,228
|
|
Why not just use the following method:
Code:
class Test
{
private:
int i;
public:
Test(int i)
{
this->i = i;
}
Test() : i(2)
{
}
};
__________________
if (i_forgot && this_is_about_code)
language = c++;
|

08-08-2005, 09:50 AM
|
 |
Modurator Wannabe
|
|
Join Date: Mar 2003
Location: Tampa, FL USA
Posts: 2,193
|
|
Quote:
|
Originally Posted by bwkaz
Well, first, you're missing a semicolon. 
|
Whoops!
Thanks for the help guys!
Unfortunately bwkaz, your suggestion doesn't work. I think it only "creates" a temporary Test(2) class. When letting "int i" be public and printing it, I got 134514688.
Fortunately, tecknophreak's suggestion does work. I've seen that ": variable"(colon space variable) type initialization before, but only with structs. Do you know where I might read more about it?
|

08-08-2005, 08:05 PM
|
|
Moderator
|
|
Join Date: Apr 2001
Location: SF Bay Area, CA
Posts: 14,950
|
|
Heh -- it would help if I maybe ran some tests, more than just "it compiles, it must work!"
Anyway, yeah, that makes sense.
The colon initializer thing was originally put in C++ to call base class constructors. (If class Inherit inherits from class Base, then in the Inerhit constructor, you sometimes need to call at least one of the Base constructors. If you don't, C++ will call the default one for you, I think. If you inherit from more than one class, then it's either an error to not call the constructors for both base classes, or they get called in the order that the inherited class lists them.)
Some time later (though perhaps it was right away, I don't know for sure), it was adapted to also allow you to initialize the class's member variables. That's where its use in constructors for structs (in C++ anyway) comes from.
__________________
"Quando omni flunkus moritati" -- Possum Lodge motto -- Loose translation: "When all else fails, play dead."
Registered Linux User #219692
Please take a look at the Posting Guidelines, to help us help you!
|

08-12-2005, 10:12 AM
|
 |
Modurator Wannabe
|
|
Join Date: Mar 2003
Location: Tampa, FL USA
Posts: 2,193
|
|
|
Although tecknophreak's method works(thank you again), it technically isn't calling one constructor from another. I'm still curious if it is possible, because I may need this in the future...
|

08-12-2005, 10:36 AM
|
|
Bad Speller ^^
|
|
Join Date: May 2001
Location: Uh, I'm somewhere where I don't know where I am.
Posts: 1,228
|
|
Why would you want to call one constructor from another?
I guess you could call the operator and do an assignment like so:
Code:
#include <iostream>
class dumb {
private:
int i;
public:
dumb(int init) {
i = init;
};
dumb() {
*this = dumb(2);
};
int geti() {
return i;
};
};
int main() {
dumb d;
std::cout << d.geti() << std::endl;
}
Don't mind the class name of dumb, I was just sick of using a test class name. This does the job, but I think doing more constructor overloads and initializing vars is a lot nicer than doing this.
__________________
if (i_forgot && this_is_about_code)
language = c++;
|

08-13-2005, 01:15 PM
|
 |
Modurator Wannabe
|
|
Join Date: Mar 2003
Location: Tampa, FL USA
Posts: 2,193
|
|
Ah hah! Last night I found out about something very interesting that I didn't know. (BTW, thanks again technofreak for the help. Where's bwkaz when I need him  ).
I found out that you can pre-initialize(?) variables (simliar to Python)! (Oh, and I read up on that "colon space variable" thing. It's very, very much like using inheritance to initialize variables.) Excusing any typos, this should compile.
Code:
class Test
{
private:
int hidden;
public:
Test(int i = 2) : hidden(i) {}
};
That code should allow Test(); and Test(x); to be called. I'd be impressed if it could get any better than that.
THANKs guys!!!
.
|

08-13-2005, 07:22 PM
|
|
Moderator
|
|
Join Date: Apr 2001
Location: SF Bay Area, CA
Posts: 14,950
|
|
Ah, so you're using default values for the variables then.
__________________
"Quando omni flunkus moritati" -- Possum Lodge motto -- Loose translation: "When all else fails, play dead."
Registered Linux User #219692
Please take a look at the Posting Guidelines, to help us help you!
|

08-13-2005, 08:50 PM
|
 |
Modurator Wannabe
|
|
Join Date: Mar 2003
Location: Tampa, FL USA
Posts: 2,193
|
|
Yeah, default variables was the main goal. I had no idea that it could be done like that, which is kind of strange considering that I have a pretty good foundation in Java. I don't recall ever seeing default initialization(int i = 2) used in my Java studies before. It makes me wonder if it's even possible in Java.
I would test it out in Java, but I'm too lazy.  If anyone else finds out, let me know, I'm just curious.
|

08-14-2005, 12:42 PM
|
|
Moderator
|
|
Join Date: Apr 2001
Location: SF Bay Area, CA
Posts: 14,950
|
|
|
I am pretty sure it is possible, yes.
That's memory from some first-year CS classes at college, though, and therefore it's been a few years since I used it. So I could be remembering incorrectly, though I don't think so.
__________________
"Quando omni flunkus moritati" -- Possum Lodge motto -- Loose translation: "When all else fails, play dead."
Registered Linux User #219692
Please take a look at the Posting Guidelines, to help us help you!
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -4. The time now is 09:20 PM.
|


|