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 07-02-2005, 12:23 PM   #1 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Default Constructors

Is a default constructor a constructor that never takes a value, or is there a difference between a constructor that accepts no parameters and a default constructor? If you create a constructor that takes no parameters, does this automatically become your default constructor?

For instance, is this constructor a default constructor? Why or why not?

Code:
template <class T>
class ListNode
{
public:
    ListNode();
    // ...

private:
    ListNode* nextPtr;
    T* data;
};

template <class T>
ListNode<T>::ListNode():
nextPtr(0), data(0)
{}
One last question, if you provide default paramaters to a no-argument constructor, is it still a default constructor?

Code:
const int DEFAULT_ARRAY_SIZE = 10;

class Array
{
public:
    Array(arraySize = DEFAULT_ARRAY_SIZE);
    // ...

private:
    // ...
    int arraySize;
};
fp_unit is offline   Reply With Quote
Old 07-02-2005, 05:12 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I'll check the standard to see what they have to say about it later.
__________________
Valmont is offline   Reply With Quote
Old 07-03-2005, 05:44 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Ah here it is. I've quoted it for you so you can draw your own conclusions:

Quote:
12.1 Constructors

12 Special member functions

5: A default constructor for a class X is a constructor of class X that can be called without an argument. If
there is no userdeclared
constructor for class X, a default constructor is implicitly declared. An implicitlydeclared
default constructor is an inline public member of its class. A constructor is trivial if it is an
implicitlydeclared
default constructor and if:
— its class has no virtual functions (10.3) and no virtual base classes (10.1), and
— all the direct base classes of its class have trivial constructors, and
— for all the nonstatic data members of its class that are of class type (or array thereof), each such class has
a trivial constructor.

6 :Otherwise, the constructor is nontrivial.

7: An implicitlydeclared
default constructor for a class is implicitly defined when it is used to create an object
of its class type (1.8). The implicitlydefined
default constructor performs the set of initializations of the
class that would be performed by a userwritten
default constructor for that class with an empty meminitializerlist
(12.6.2) and an empty function body. If that userwritten
default constructor would be illformed,
the program is illformed.
Before the implicitlydeclared
default constructor for a class is implicitly
defined, all the implicitlydeclared
default constructors for its base classes and its nonstatic data members
shall have been implicitly defined. [Note: an implicitlydeclared
default constructor has an exceptionspecification
(15.4). ]
__________________
Valmont is offline   Reply With Quote
Old 07-03-2005, 09:37 AM   #4 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Thanks, I should really get myself a copy of the standard some day soon, although I didn't actually check TC++PL book first My conclusion I drew was any user-defined constructor with no parameters will become the default constructor, and if no user defined constructor is available, C++ will implicitly create one. This is was what I had thought, thank you for helping me clarify. I've always been in the habit of defining no-argument constructors, and I'll stick with that habit until I learn more about the language. For now it seems like it can't hurt as long as I put thought into each constructor and don't write it incorrectly.
fp_unit 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
default values for functions?? falsepride HTML, XML, Javascript, AJAX 2 06-15-2005 10:24 AM
Can I delete the default code? 357mag Standard C, C++ 2 04-05-2005 11:23 PM
Simple Forums robert_w_ark PHP 7 03-14-2005 06:07 PM
moving mysql from its default directory infinite_root Linux / BSD / OS X 2 04-28-2004 10:32 PM
Mini Games Nitro PHP 18 11-11-2003 11:17 AM


All times are GMT -8. The time now is 11:00 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