| View previous topic :: View next topic |
| Author |
Message |
Busin Guest
|
Posted: Sun Jun 27, 2004 9:35 pm Post subject: Why default constructor isn't enough? |
|
|
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
|
Posted: Sun Jun 27, 2004 9:43 pm Post subject: Re: Why default constructor isn't enough? |
|
|
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
|
Posted: Sun Jun 27, 2004 11:54 pm Post subject: Re: Why default constructor isn't enough? |
|
|
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
|
Posted: Mon Jun 28, 2004 10:06 am Post subject: Re: Why default constructor isn't enough? |
|
|
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
|
Posted: Mon Jun 28, 2004 10:15 am Post subject: Re: Why default constructor isn't enough? |
|
|
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 |
|
 |
|