To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here

http://justlinux.com/   Mon, 06-Sep-2010 01:20:51 GMT
         internet.com

Go Back   JustLinux Forums > Community Help: Check the Help Files, then come here to ask! > Programming/Scripts

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

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 08-06-2005, 05:59 PM
Sepero's Avatar
Sepero Sepero is offline
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.
Reply With Quote
  #2  
Old 08-07-2005, 01:50 PM
bwkaz bwkaz is offline
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:

Code:
Test(2);
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!
Reply With Quote
  #3  
Old 08-08-2005, 08:36 AM
tecknophreak tecknophreak is offline
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++;
Reply With Quote
  #4  
Old 08-08-2005, 09:50 AM
Sepero's Avatar
Sepero Sepero is offline
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?
Reply With Quote
  #5  
Old 08-08-2005, 08:05 PM
bwkaz bwkaz is offline
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!
Reply With Quote
  #6  
Old 08-12-2005, 10:12 AM
Sepero's Avatar
Sepero Sepero is offline
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...
Reply With Quote
  #7  
Old 08-12-2005, 10:36 AM
tecknophreak tecknophreak is offline
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++;
Reply With Quote
  #8  
Old 08-13-2005, 01:15 PM
Sepero's Avatar
Sepero Sepero is offline
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!!!

.
Reply With Quote
  #9  
Old 08-13-2005, 07:22 PM
bwkaz bwkaz is offline
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!
Reply With Quote
  #10  
Old 08-13-2005, 08:50 PM
Sepero's Avatar
Sepero Sepero is offline
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.
Reply With Quote
  #11  
Old 08-14-2005, 12:42 PM
bwkaz bwkaz is offline
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!
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 09:20 PM.

Help File Library
Miscellaneous
Security
Shells
Compiling Kernels
Soundcards
More



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
Free Newsletter
Server Daily


More Free Newsletters




internet.commerce
Be a Commerce Partner












Linux is a trademark of Linus Torvalds.

Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.7.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.