Default constructor

G

Gunnar G

If I don't define a default constructor for my class "Foo", I still get one
from the compiler. But if I define a constructor that takes an argument, I
don't get the default constructor, why?
I understand that this is a rule in the language, but what is the motivation
of this rule?
 
A

Alf P. Steinbach

* Gunnar G:
If I don't define a default constructor for my class "Foo", I still get one
from the compiler. But if I define a constructor that takes an argument, I
don't get the default constructor, why?
I understand that this is a rule in the language, but what is the motivation
of this rule?

To have a default and allow you to take charge.
 
J

John Harrison

If I don't define a default constructor for my class "Foo", I still get
one
from the compiler. But if I define a constructor that takes an argument,
I
don't get the default constructor, why?
I understand that this is a rule in the language, but what is the
motivation
of this rule?


A desire not to define constructors by default except where you have to
for compatibility with C.

struct X
{
};

struct X x;

This is legal C, and therefore must be legal C++, therefore C++ must
define a default constructor for X. But as soon as you add your own
constructor to X you no longer have legal C, therefore the compatibility
with C argument no longer applies.

john
 
G

Gunnar G

To have a default and allow you to take charge.

Sorry, I don't understand how this can be an answer to my question.
I can always take charge. But why is the default constructor provided by the
compiler disabled if I define any other constructor that takes an argument?
I can't see any obvious reason for this.
Is it because it is easier to write a compiler? Does anybody know?
 
R

rookkey

Sorry, I don't understand how this can be an answer to my question.
I can always take charge. But why is the default constructor provided
by the compiler disabled if I define any other constructor that takes
an argument? I can't see any obvious reason for this.
Is it because it is easier to write a compiler? Does anybody know?

There are a lot of times when you do not want a class to have a default
constructor.

For example, say you have a class representing a connection to a
database. Say you decide that it does not make sense for there to be a
"default" constructor which represents a connection; sometimes you need
to have clients of you class to explicitly state what the connection is
when clients create an instance of the class. Forbidding default
constructors forces the clients of a class to fully initialize an object
when the object is created. It prevents clients from "partially"
initializing objects before using them.

If C++ always created a default constructor, C++ would need some kind of
syntax for programmers to tell the compiler not to create a default
constuctor. Essentially, designers of a class would have to declare the
default constructor as private in the class definition. The authors of
the language saw that programmers would frequently forget to explicitly
declare the default constructor (or more exactly, the constructor which
takes no arguments) as private, so they decided that if one user-defined
constructor exists, it makes more sense to not automatically generate any
other constructors.
 
C

Cy Edmunds

Gunnar G said:
If I don't define a default constructor for my class "Foo", I still get one
from the compiler. But if I define a constructor that takes an argument, I
don't get the default constructor, why?
I understand that this is a rule in the language, but what is the motivation
of this rule?

OK, let's see what happens if we drop either part of this rule.

1) No automatic default constructor if the programmer provides no
constructor. Now the object has no constructor at all. It is in an undefined
state so using it in any way would be undefined behavior. Not good.

2) Automatic default constructor inserted even if the programmer provides a
constructor with arguments. Now the client can declare objects of this type
without providing the arguments, possibly leaving the object in an undefined
state again. The programmer can define a private default constructor but
that seems like too much trouble for what is after all a perfectly common
situation.

In short, there seems to be no reasonable alternative.
 
J

jeffc

Cy Edmunds said:
2) Automatic default constructor inserted even if the programmer provides a
constructor with arguments. Now the client can declare objects of this type
without providing the arguments, possibly leaving the object in an undefined
state again. The programmer can define a private default constructor but
that seems like too much trouble for what is after all a perfectly common
situation.

In short, there seems to be no reasonable alternative.

2 is a reasonable alternative, they just chose not to do it that way. (If
the programmer thought creating an object with no initializing parameters
were bad, he could simply define a default constructor that initialized
things in a default way.) Philosophically, they just decided that when the
programmer takes over, he takes over all. No technical reason it has to be
that way.
 

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

Forum statistics

Threads
474,175
Messages
2,570,945
Members
47,495
Latest member
Jack William

Latest Threads

Top