Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default construction versus construction with initial values

Reply
Thread Tools

Default construction versus construction with initial values

 
 
Ook
Guest
Posts: n/a
 
      10-07-2005
I'm having trouble comprehending what exactly "default construction" is. I
know how to provide a constructor with initial values, so that if I, for
example, in my code do this:

MyClass zoot(1,2);

It would instantiate MyClass, passing 1 and 2 to my constructor which in
turn would do whatever I want it to. Would a default constructor be as
follows:

MyClass zoot;

With no parameters passed? In my class declaration, would I do this:

class Zoot
{
Zoot(); // default constructor?
Zoot( int a, int b); // constructor with initial values.
....
}

Zoot::Zoot()
{
// default constructor, no parameters passed, do whatever you want?
}
Zoot::Zoot( int a, int b )
{
// constructor with initial values
}

Am I on the right track? IOW, a default constructor is simply a constructor
that receives no parameters, but you can still write code to do what you
want with it?


 
Reply With Quote
 
 
 
 
int2str@gmail.com
Guest
Posts: n/a
 
      10-07-2005

Ook wrote:
> I'm having trouble comprehending what exactly "default construction" is.


>From the FAQ:


A "default constructor" is a constructor that can be called with no
arguments.


So to stay with your example, this could be a default constructor:

class Zoot()
{
public:
Zoot(); // Default constructor
}

But this could be a default constructor as well:

class Zoot()
{
public:
Zoot( int a = 0, int b = 5); // Default constructor
}

Since it has default values for a and b, you can still create a new
obect like so:

Zoot myObj;

However, this ...

class Zoot()
{
public:
Zoot( int a, int b); // NOT a default constructor
}

.... is NOT a default constructor and the compiler will create one for
you implicitly.

> Am I on the right track? IOW, a default constructor is simply a constructor
> that receives no parameters, but you can still write code to do what you
> want with it?


Pretty much.

Cheers,
Andre

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-07-2005
Ook wrote:
> I'm having trouble comprehending what exactly "default construction" is. I
> know how to provide a constructor with initial values, so that if I, for
> example, in my code do this:
>
> MyClass zoot(1,2);
>
> It would instantiate MyClass, passing 1 and 2 to my constructor which in
> turn would do whatever I want it to. Would a default constructor be as
> follows:
>
> MyClass zoot;
>
> With no parameters passed?


Yes, it would.

> In my class declaration, would I do this:
>
> class Zoot
> {
> Zoot(); // default constructor?
> Zoot( int a, int b); // constructor with initial values.
> ...
> }

;

>
> Zoot::Zoot()
> {
> // default constructor, no parameters passed, do whatever you want?
> }


I am not sure what you mean by "do whatever you want", but yes, this is
the default c-tor.

> Zoot::Zoot( int a, int b )
> {
> // constructor with initial values
> }
>
> Am I on the right track? IOW, a default constructor is simply a constructor
> that receives no parameters, but you can still write code to do what you
> want with it?


A default constructor is one that can be invoked without specifying any
arguments. For example,

class has_def_ctor {
public:
has_def_ctor(int a = 42);
};

also declares a default c-tor.

V
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-07-2005

"Ook" <Don't send me any freakin' spam> wrote in message
news:(E-Mail Removed)...
> I'm having trouble comprehending what exactly "default construction" is. I
> know how to provide a constructor with initial values, so that if I, for
> example, in my code do this:
>
> MyClass zoot(1,2);
>
> It would instantiate MyClass, passing 1 and 2 to my constructor which in
> turn would do whatever I want it to. Would a default constructor be as
> follows:
>
> MyClass zoot;
>
> With no parameters passed? In my class declaration, would I do this:
>
> class Zoot
> {
> Zoot(); // default constructor?
> Zoot( int a, int b); // constructor with initial values.
> ...
> }
>
> Zoot::Zoot()
> {
> // default constructor, no parameters passed, do whatever you want?
> }
> Zoot::Zoot( int a, int b )
> {
> // constructor with initial values
> }
>
> Am I on the right track? IOW, a default constructor is simply a
> constructor that receives no parameters, but you can still write code to
> do what you want with it?


Almost. A default constructor is a constructor which can be
invoked with no arguments. It might or might not have formal
parameters. If it does define parameters, and they all have
been given default values, then it is considered a default
constructor (because it can still be called with no arguments).

class C
{
public:
C() {} // 1) default ctor
C(int i = 0) {} // 2) default ctor
C(int i = 0, int j = 42, int x = 99) // 3) default ctor
C(int i) {} // 4) not a default ctor
C(int i, int j = 0) {} // 5) not a default ctor
};

Note that the above contains errors, because a class can
only have one default constructor. So all but one of
1), 2), and 3) would need to be removed.

-Mike


 
Reply With Quote
 
wenmang@yahoo.com
Guest
Posts: n/a
 
      10-07-2005
I have a confusion about this. If you define a ctor with params. Does
compiler still create a default ctor for you?
wm

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-07-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have a confusion about this. If you define a ctor with params. Does
> compiler still create a default ctor for you?


The compiler never generates a default c-tor in presence of _any_ other
user-defined c-tor.

V
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-07-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>I have a confusion about this. If you define a ctor with params. Does
> compiler still create a default ctor for you?


No.

-Mike


 
Reply With Quote
 
ctrl4ltdeleteme
Guest
Posts: n/a
 
      10-07-2005
hrmmm....just wondering. If I made a default constructor

class Object
{
public:
Object( int a=5, int b = 5 );
};

I could still make a call in main like:

Object myObj( 7 , 38 );

which would call the default using 7 and 38 instead of default 5,5.
right?

Also, one more question. What's the difference between making a call:
Object myObj;
and
Object myObj(); //is this not allowed?
I had problems doing it, but i dont see why i should

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-07-2005
"ctrl4ltdeleteme" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> hrmmm....just wondering. If I made a default constructor
>
> class Object
> {
> public:
> Object( int a=5, int b = 5 );
> };
>
> I could still make a call in main like:
>
> Object myObj( 7 , 38 );
>
> which would call the default using 7 and 38 instead of default 5,5.
> right?


Correct.

> Also, one more question. What's the difference between making a call:
> Object myObj;


This defines an object of type 'Object'.
This form requires that 'Object' has defined
a default constructor.

> and
> Object myObj(); //is this not allowed?


Yes, it's 'allowed', but it doesn't do what you perhaps
believe. It does not define an object. It declares
(but does not define) a function, named 'myObj', which
takes no arguments, and returns a value of type 'Object'.
IOW it's a function prototype (a.k.a. declaration).

> I had problems doing it,


What problems specifically. That line should compile just
fine as long as the definition of type 'Object' is in scope
at that point. BTW 'Object' is about the worst possible
name for a type. An object is not a type, and object *has*
a type.


> but i dont see why i should


Obviously because you have limited understanding of C++.
Which C++ books are you reading? (If none, why not?).
See the book reviews at www.accu.org for recommendations.

-Mike


 
Reply With Quote
 
ctrl4ltdeleteme
Guest
Posts: n/a
 
      10-08-2005
hah. I just looked at it again and realized why that wouldn't work.

Yeah, I am in the process of migrating from java. Though I wouldn't
consider myself particularly well versed in either. I was currently
reading a book by deitel on C++, however the peer review verbally hung
it from the gallows so I just ordered
The C++ Programming Language Special Edition. Hopefully that will do
me some good. Thanks for the info.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initial class name for Default page is _Default instead of Default Sean Dockery ASP .Net 3 06-21-2009 04:30 AM
Default initial array value? [TMTOWTDI] h3xx Perl Misc 5 02-22-2009 12:07 PM
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
How to show initial default values in a detailsview form Morris Neuman ASP .Net Web Controls 4 02-15-2007 08:05 AM



Advertisments