Default construction versus construction with initial values

O

Ook

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?
 
I

int2str

Ook said:
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
 
V

Victor Bazarov

Ook said:
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
 
M

Mike Wahler

Ook said:
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
 
W

wenmang

I have a confusion about this. If you define a ctor with params. Does
compiler still create a default ctor for you?
wm
 
V

Victor Bazarov

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
 
C

ctrl4ltdeleteme

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
 
M

Mike Wahler

ctrl4ltdeleteme said:
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
 
C

ctrl4ltdeleteme

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.
 
O

Ook

A default constructor is a constructor which can be invoked with no
arguments.

Ahh - there is the piece I was missing. "no arguments". Thanks, and as
always thanks to everyone else that pitched in :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top