473,387 Members | 1,597 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,387 software developers and data experts.

Uninitialized constant objects

Hello.

Based on this simple test program:

class Test {};

int main()
{
Test t;
const Test u;
}

Why does the definition of 'u' fail? What if all that's wanted is to
have a default constructed Test object? Must one explicitly write so? E.g.:

const Test u = Test();

What's the rationale for such restriction? And, lastly, why does
defining a default constructor solve the issue? I.e.:

class Test
{
public:
Test() {}
};

int main()
{
Test t;
const Test u;
}

Now the compiler accepts it just fine. Why? Pardon me if the answer to
these questions should be obvious. I don't presently find them to be so.

Thank you,

--
Ney André de Mello Zunino
Jul 23 '05 #1
5 9095
which compiler you are using..?
I tried it on VC++ compiler it is not giving any error in both the
cases

since constructor is called during object creation, it doesn't matter
wether the object is const or not

by declaring a object as const the only restrictions that you have on
it is
-- you can only call const methods of that object( ex: methods like
void print() const )
-- you cannot modify any member variables

Jul 23 '05 #2
Ney André de Mello Zunino wrote:
[snip]
What's the rationale for such restriction? And, lastly, why does
defining a default constructor solve the issue? I.e.:


The code you posted is not your real code.
The code you posted should compile fine without a problem,
since if you don't define any constructor on your own, the compiler
will create a default constructor for you.

So please post your real problem code.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3
Ney André de Mello Zunino wrote:
class Test {};

int main()
{
Test*t;
const*Test*u;
}

Why does the definition of 'u' fail?

[8.5]
9. If no initializer is specified for an object, and the object is of
(possibly cv-qualified) non-POD class type (or array thereof), the object
shall be default-initialized; if the object is of const-qualified type,
the underlying class type shall have a user-declared default constructor.
Otherwise, if no initializer is specified for an object, the object and
its subobjects, if any, have an indeterminate initial value 90) ; if the
object or any of its subobjects are of const-qualified type, the program
is ill-formed.

Your object is of const POD type, then the last sentence of the paragraph
applies.

MM


Jul 23 '05 #4
* Karl Heinz Buchegger:
Ney André de Mello Zunino wrote:
[snip]
What's the rationale for such restriction? And, lastly, why does
defining a default constructor solve the issue? I.e.:


The code you posted is not your real code.
The code you posted should compile fine without a problem,
since if you don't define any constructor on your own, the compiler
will create a default constructor for you.

So please post your real problem code.


Sorry.

Check out the standard's text quoted by "Max M." in this thread;
some explicit definition of the value is necessary for a const
object.

Unfortunately Visual C++ erronously compiles the code, at least as
I remember it it does.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #5
"Alf P. Steinbach" wrote:

* Karl Heinz Buchegger:
Ney André de Mello Zunino wrote:
[snip]
What's the rationale for such restriction? And, lastly, why does
defining a default constructor solve the issue? I.e.:


The code you posted is not your real code.
The code you posted should compile fine without a problem,
since if you don't define any constructor on your own, the compiler
will create a default constructor for you.

So please post your real problem code.


Sorry.

Check out the standard's text quoted by "Max M." in this thread;
some explicit definition of the value is necessary for a const
object.


I saw it.
I didn't know that.
I learned.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6

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

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
2
by: nan.li.g | last post by:
I have this simple code below. When I compiled it, I got the following error. But after I removed the comment marker(//), i.e. explicitly defined a constructor, it becomes OK. The compiler...
3
by: Marcin Kalicinski | last post by:
int a; // #1 uninitialized value int a(0) // #2 zero-initialized value std::vector<int> v(10); // #3 zero-initialized values (why?) std::vector<int> v(10, 0); // #4...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
13
by: rswanster | last post by:
When I compile and run the following: #include <iostream> int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
25
by: tsaar2003 | last post by:
Hi Pythonians, To begin with I'd like to apologize that I am not very experienced Python programmer so please forgive me if the following text does not make any sense. I have been missing...
7
by: Paul Edwards | last post by:
On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the error "initializer element is not constant" on the below code. The code compiles fine with other compilers I have. Is this valid C89 code?...
6
by: Amit Bhatia | last post by:
Hi, I am not sure if this belongs to this group. Anyway, my question is as follows: I have a list (STL list) whose elements are pairs of integers (STL pairs, say objects of class T). When I create...
1
by: pillbug | last post by:
Hello, I would like to know what the standard says regarding writing beyond the end of file. For example, in the following program, is the result always the same or is it system dependent? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...

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.