Default constuctor

T

Tapeesh

Does C++ say that a default constructor needs to be generated by the
compiler even if a class has the default constructor defined ? Say for
eg. there is a class

class A
{

int i;
A()
{
i=0;
}
};


class B
{
int i;
void foo()
{
i=0;
}
};

In this case will the compiler generate its own default constructor for
class A like in the case of class B and also create the one defined
in the class ? If yes, then what is the functionality of this compiler
generated constructor.
 
G

Gary Labowitz

Tapeesh said:
Does C++ say that a default constructor needs to be generated by the
compiler even if a class has the default constructor defined ? Say for
eg. there is a class
<<snip>>
No. Once you supply a consstructor the compiler will not provide one.
 
J

John Carson

Tapeesh said:
Does C++ say that a default constructor needs to be generated by the
compiler even if a class has the default constructor defined ? Say for
eg. there is a class

class A
{

int i;
A()
{
i=0;
}
};


class B
{
int i;
void foo()
{
i=0;
}
};

In this case will the compiler generate its own default constructor
for class A like in the case of class B and also create the one
defined in the class ? If yes, then what is the functionality of this
compiler generated constructor.

The compiler will NOT generate a default constructor if you define ANY
constructor for a class. Consider for example:

class A
{
int i;
public:
A(int n)
{
i=n;
}
};

This doesn't have a default constructor. Thus if you write:

int main()
{
A a;
}

then it won't compile because

A a;

requires a default constructor and class A does not have one. If you change
it to, say,

A a(5);

then it compiles without a problem.

Incidentally, your original class declaration:

class A
{
int i;
A()
{
i=0;
}
};

has a private constructor, so

A a;

won't compile. You need to make the constructor public.
 
T

Tapeesh

John said:
The compiler will NOT generate a default constructor if you define ANY
constructor for a class. Consider for example:

class A
{
int i;
public:
A(int n)
{
i=n;
}
};

This doesn't have a default constructor. Thus if you write:

int main()
{
A a;
}

then it won't compile because

A a;

requires a default constructor and class A does not have one. If you change
it to, say,

A a(5);

then it compiles without a problem.

Incidentally, your original class declaration:

class A
{
int i;
A()
{
i=0;
}
};

has a private constructor, so

A a;

won't compile. You need to make the constructor public.


What i wanted to ask is that when the compiler compiles a code at that
time while creating an object model for the source code, does the
compiler generate some default constructor even if some constructor
(default or otherwise) has been defined for the class?
 
J

John Carson

Tapeesh said:
What i wanted to ask is that when the compiler compiles a code at that
time while creating an object model for the source code, does the
compiler generate some default constructor even if some constructor
(default or otherwise) has been defined for the class?

The answer has already been given twice. NO.
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top