Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-23-2007, 02:55 PM   #1 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 203
falsepride is on a distinguished road
constructors and destructors

can constructors have a default parameter? and if so is it considered to be bad coding to do it?

can someone give me an example of a destructor? the book im reading says that a destructor can be used to free up memory used by the class, but in all of its code examples the body of the destructors are empty.
falsepride is offline   Reply With Quote
Old 04-24-2007, 05:22 AM   #2 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
Ok, about constructors having default parameter, I think this is what you mean. Generally, it may be useful to offer a default state for your object and to initialize your variables of your object to a certain state. You can also add other constructors (think of it the same way as method overloading. as long as the signature is different it is legal).

Now I haven't touched c++ in quite some time, been going over to the world of java lately but you might have to be careful about adding constructors without having a default constructor. I think it is required if you add additional constructors that you specify a default constructor (you can make it do nothing, or make it call another constructor, etc). Something like this should illustrate the concept:
Code:
#include <iostream>
using namespace std;

class A {
private:
 int a;
 int b;
public:
 A() { a = 0; b = 1; cout << "default constructor\n"; }
 A(int x,int z) { a = x; b = z; cout << "2nd constructor\n"; }
 ~A() { /* here's your destructor. maybe you have a private char * which you dynamically allocate and you want to free it
			anytime the object is about to be destroyed */ }

};

int main() {

A a;
A b(2,3);
return 0;

}
I may be off on some things slightly, as I said i haven't touched c++ in a while but I think this is a step in the right direction. if you look around there's an excellent post by Valmont on destructors.
Deliverance is offline   Reply With Quote
Old 05-08-2007, 01:51 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
can constructors have a default parameter?
Yes. See constructor in the post above.

Quote:
and if so is it considered to be bad coding to do it?
Certainly not a bad thing. In some cases it may be highly desirable to do it.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 01:29 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting