Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default constuctor

Reply
Thread Tools

Default constuctor

 
 
Tapeesh
Guest
Posts: n/a
 
      03-01-2005
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.

 
Reply With Quote
 
 
 
 
Gary Labowitz
Guest
Posts: n/a
 
      03-01-2005

"Tapeesh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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.
--
Gary


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      03-01-2005
"Tapeesh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com
> 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.

--
John Carson

 
Reply With Quote
 
Tapeesh
Guest
Posts: n/a
 
      03-02-2005
John Carson wrote:
> "Tapeesh" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com
> > 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.
>
> --
> John Carson



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?

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      03-02-2005
"Tapeesh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com
>
> 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.


--
John Carson
 
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
Adding Microsoft objects in Boa Constuctor Palette Sid K Python 2 08-07-2008 05:43 PM
fail to call another constructor from constuctor cmk128@hotmail.com C++ 18 10-05-2006 09:18 PM
Default Mozilla isn't default! Help! M. FERRANTE Firefox 1 07-27-2005 09:07 AM
still crabby about copy constuctor craziness Unknown Poster Perl Misc 16 08-02-2004 09:08 PM
initialization in initializer list using copy constuctor?!? Alexander Stippler C++ 3 04-09-2004 01:53 PM



Advertisments