C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why default constructor isn't enough?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Busin
Guest





PostPosted: Sun Jun 27, 2004 9:35 pm    Post subject: Why default constructor isn't enough? Reply with quote



class AA
{
public:
const int n;
};

int main()
{
AA a;
....
}

This code gives error: "C2512: 'AA' : no appropriate default constructor
available."

Then adding a constructor fixes the error.

class AA
{
public:
const int n;
AA(int m) : n(m) {}
};

int main()
{
AA a(1);
...
}

Can someone elaborate why with a const member variable, the default
constructor isn't enough? Thanks!


Back to top
Thomas J. Gritzan
Guest





PostPosted: Sun Jun 27, 2004 9:43 pm    Post subject: Re: Why default constructor isn't enough? Reply with quote



Busin schrieb:
Quote:
class AA
{
public:
const int n;
};

Your const variable has to be initialized at construction time, because it
could not be changed later.

Thomas

Back to top
Ali Cehreli
Guest





PostPosted: Sun Jun 27, 2004 11:54 pm    Post subject: Re: Why default constructor isn't enough? Reply with quote



On Sun, 27 Jun 2004 21:35:53 +0000, Busin wrote:

Quote:
class AA
{
public:
const int n;
};

int main()
{
AA a;
...
}

This code gives error: "C2512: 'AA' : no appropriate default constructor
available."

Then adding a constructor fixes the error.

[...]

Quote:

Can someone elaborate why with a const member variable, the default
constructor isn't enough? Thanks!

What I understand from this is that a const member can only be
initialized in the initialization list, which requires a constructor.

Ali


Back to top
JKop
Guest





PostPosted: Mon Jun 28, 2004 10:06 am    Post subject: Re: Why default constructor isn't enough? Reply with quote

Busin posted:

Quote:
Can someone elaborate why with a const member variable, the default
constructor isn't enough? Thanks!


Looks like you must initialize a const variable.


-JKop

Back to top
Devesh Mandowara
Guest





PostPosted: Mon Jun 28, 2004 10:15 am    Post subject: Re: Why default constructor isn't enough? Reply with quote

If there is an initialiser list...its enough
-Devesh

"JKop" <NULL (AT) NULL (DOT) NULL> wrote

Quote:
Busin posted:

Can someone elaborate why with a const member variable, the default
constructor isn't enough? Thanks!


Looks like you must initialize a const variable.


-JKop



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.