473,732 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9119
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
4832
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 to be? Am I not suppose not to have any constant data member if I am going to have the assignment operator working for the class? Or am I missing something here and there is something I need to learn about? Clear, easy to understand...
2
3018
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 should generate a default constructor for me and the default one shoule be no different than the one I specified. Why do I have to specify one in this code? class A {
3
5980
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 zero-initialized values How do I construct a vector of ints without initializing them to zero? Why is #3 zero-initializing the ints? This is unintutitive, unexpected and slow. If one wanted to intialize he would use syntax #4. World would be better if int()...
8
3153
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, 1> and A<int, 2> are different types. Now, if the A template
13
20296
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
2133
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. This raises questions. Is there any difference between a Python immutable value, and a constant? I suppose "constant" also implies that the *name* binds
25
2578
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 constants in Python language. There are some workarounds available, for example the const-module. To me, this looks quite cumbersome and very unintuitive. For the interpreter, in the efficiency-wise, I just cannot tell.
7
10199
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? extern int *b; int *a = b; int main(void)
6
4013
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 a new object of class T, I would like to check if this object already exists in the list: meaning one having same integers. This can be done in linear time in a list, and probably faster if I use STL Set instead of list. I am wondering however if...
1
3037
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? #include <cstdio> int main (int argc, char** argv) { char ch = 'A'; FILE* fh = NULL;
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.