473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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!
Jul 22 '05 #1
4 1178
Busin schrieb:
class AA
{
public:
const int n;
};


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

Thomas
Jul 22 '05 #2
On Sun, 27 Jun 2004 21:35:53 +0000, Busin wrote:
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.
[...]

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

Jul 22 '05 #3
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
Jul 22 '05 #4
If there is an initialiser list...its enough
-Devesh

"JKop" <NU**@NULL.NULL> wrote in message
news:c4*****************@news.indigo.ie...
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

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: jeffc | last post by:
When compiling the following program, I get an error on the line specified: The base class "B" cannot be initialized because it does not have a default constructor. If I remove the "virtual"...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
12
by: NewToCPP | last post by:
does the default constructor initialize values? I have a class as defined below: class A { int i; char c; int * iPtr;
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
10
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
3
by: Rahul | last post by:
Hi Everyone, I have the following code and the compiler complains that there isn't any default constructor available, class C { private: C() {
10
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.