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

const object declaration

Consider the following program:

#include <iostream>

using namespace std;

class X
{
public:
int value() const { return val; }
int val;
};

int main()
{
const X obj;

return 0;
}

When this program is compiled with g++ as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

the following compilation error is generated for the line const X obj;

error: uninitialized const `obj'

However VC++ 2005 Express Edition gives only the following warning(no
error):

warning: 'obj' : 'const' automatic data initialized with compiler
generated default constructor produces unreliable results

Which is the correct behaviour as per the standard ?

Kindly clarify.

Also please let me know how to set the compilation flags in VC++ 2005
Express Edition IDE similar to the command line g++ compilation flags
as mentioned above.

Thanks
V.Subramanian
Dec 3 '07 #1
10 2581
Which is the correct behaviour as per the standard ?

I don't know. But my bet is if you define an empty constructor your
problem should go away.
>
Kindly clarify.

Also please let me know how to set the compilation flags in VC++ 2005
Express Edition IDE similar to the command line g++ compilation flags
as mentioned above.
That, im afraid, is better asked in the gcc group.

Ben
Dec 3 '07 #2
benben wrote:
>[..]
Also please let me know how to set the compilation flags in VC++ 2005
Express Edition IDE similar to the command line g++ compilation flags
as mentioned above.

That, im afraid, is better asked in the gcc group.
Or in the VC++ newsgroup.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 3 '07 #3
On Dec 3, 7:54 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
benben wrote:
[..]
Also please let me know how to set the compilation flags in VC++ 2005
Express Edition IDE similar to the command line g++ compilation flags
as mentioned above.
That, im afraid, is better asked in the gcc group.

Or in the VC++ newsgroup.

V
I asked this once on clc++m and had not got a convincing answer. It is
mandated by the standard that a user-defined constructor must be
provided. It is needed even though there are no members in the class.
It just is as it is. I am not aware of the rationale behind this
though.

In case the class has a POD type, it would remain uninitialized
(indeterminate value) when default constructor (by the compiler
generated default constructor). In case of non-POD members, they would
have been default initialized. An indeterminate POD type's value is
rather useless but for non-POD types, it might make sense. I don't
know about the rationale but may be it is based on something along
these lines, to avoid complexity. I don't know for sure. May be some
guru will enlighten us! :)
Dec 3 '07 #4
Abhishek Padmanabh wrote:
On Dec 3, 7:54 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>benben wrote:
>>>[..]
Also please let me know how to set the compilation flags in VC++
2005 Express Edition IDE similar to the command line g++
compilation flags as mentioned above.
>>That, im afraid, is better asked in the gcc group.

Or in the VC++ newsgroup.

V

I asked this once on clc++m and had not got a convincing answer. [..]
As you may have noticed, I removed all mention of the C++ problem but
left in the "compilation flags" request. That (and only that) is what
I am suggesting asking about in the VC++ newsgroup.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 3 '07 #5
On Dec 3, 8:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Abhishek Padmanabh wrote:
On Dec 3, 7:54 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
benben wrote:
[..]
Also please let me know how to set the compilation flags in VC++
2005 Express Edition IDE similar to the command line g++
compilation flags as mentioned above.
>That, im afraid, is better asked in the gcc group.
Or in the VC++ newsgroup.
I asked this once on clc++m and had not got a convincing answer. [..]

As you may have noticed, I removed all mention of the C++ problem but
left in the "compilation flags" request. That (and only that) is what
I am suggesting asking about in the VC++ newsgroup.
Sorry about that! I wrongly quoted your response. My reply was to the
original post. I wouldn't have said : "May be some
guru will enlighten us! :)" to you! :)
Dec 3 '07 #6
Abhishek Padmanabh wrote:
[..] I wouldn't have said : "May be some
guru will enlighten us! :)" to you! :)
Hey, why not? I'll gladly take a guru's advice. After all it's not
that often gurus grace us with their presence here...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 3 '07 #7
Abhishek Padmanabh wrote:
:: On Dec 3, 7:54 pm, "Victor Bazarov" <v.Abaza...@comAcast.net>
:: wrote:
::: benben wrote:
::::: [..]
::::: Also please let me know how to set the compilation flags in
::::: VC++ 2005 Express Edition IDE similar to the command line g++
::::: compilation flags as mentioned above.
:::
:::: That, im afraid, is better asked in the gcc group.
:::
::: Or in the VC++ newsgroup.
:::
::: V
::
:: I asked this once on clc++m and had not got a convincing answer.
:: It is mandated by the standard that a user-defined constructor
:: must be provided. It is needed even though there are no members in
:: the class. It just is as it is. I am not aware of the rationale
:: behind this though.
::
:: In case the class has a POD type, it would remain uninitialized
:: (indeterminate value) when default constructor (by the compiler
:: generated default constructor). In case of non-POD members, they
:: would have been default initialized. An indeterminate POD type's
:: value is rather useless but for non-POD types, it might make
:: sense. I don't know about the rationale but may be it is based on
:: something along these lines, to avoid complexity. I don't know for
:: sure. May be some guru will enlighten us! :)

Since no guru appeared, I will try to answer this. :-)

The reason for PODs to be different, is that they behave like they do
in C. Had C++ added initialization to types that C does not, it would
have

1) changed old behaviour of existing code
and
2) lost all benchmarks to C code.
Bo Persson
Dec 3 '07 #8
On Dec 3, 4:05 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
I asked this once on clc++m and had not got a convincing
answer. It is mandated by the standard that a user-defined
constructor must be provided.
No it isn't. It is mandated that there be some non-trivial
initialization. In his case, something like:

X const obj = { 1 } ;

would be legal, for example.
It is needed even though there are no members in the class.
It just is as it is. I am not aware of the rationale behind
this though.
The requirement in the case of no data members is probably just
the result of an oversight. Since it's trivial to add a no-op
default constructor, however, it's no big problem.
In case the class has a POD type, it would remain
uninitialized (indeterminate value) when default constructor
(by the compiler generated default constructor).
In the case we're considering, the class is a POD.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 4 '07 #9
The following program DOES NOT COMPILE as mentioned earlier in the
beginning of the thread

#include <iostream>

using namespace std;

class X
{
public:
int val;
};

int main()
{
const X obj;

return 0;

}

However the following program COMPILES fine.

#include <iostream>

using namespace std;

class Test
{
public:
Test() { cout << "Test() default ctor" << endl; }
};

class X
{
public:
Test obj;
int val;
};

int main()
{
const X obj;

return 0;
}
I am unable to understand why this program compiles well and the
previous program does not compile.

Kindly explain.

Thanks
V.Subramanian
Dec 5 '07 #10
On Dec 5, 7:15 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
The following program DOES NOT COMPILE as mentioned earlier in the
beginning of the thread
#include <iostream>
using namespace std;
class X
{
public:
int val;
};

int main()
{
const X obj;

return 0;
}
However the following program COMPILES fine.
#include <iostream>
using namespace std;
class Test
{
public:
Test() { cout << "Test() default ctor" << endl; }
};

class X
{
public:
Test obj;
int val;
};
int main()
{
const X obj;

return 0;
}
I am unable to understand why this program compiles well and the
previous program does not compile.
I was about to say: because in the first example, the default
constructor is trivial (so an initialization is required),
whereas in the second it is not. On re-reading the standard,
however... in §8.5/9, it says "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." According to this, since X does not have a
user-declared default constructor, it shouldn't compile. But it
does, with all three of the compilers at my disposition (g++,
Sun CC and VC++).

I'll admit that this somewhat surprises me. I had always
thought (and apparently, the compilers agree with me) that the
rule was that the class must have a non-trivial default
constructor. Since the (compiler generated) default constructor
for X in the second example is non-trivial, the code would be
OK. Given that three compilers also disagree with what I've
just quoted in the standard, I wonder, too, if there isn't
something else that I've missed.

Thinking about it, I think what the rule should be is: an
initializer is required if the class has no user defined
default constructor, and has one or more sub-objects which have
trivial default constructors. That would allow const instances
without initializers for things like:

struct A
{
virtual void f() ;
} ;

or

struct B
{
std::vector< int v ;
} ;

. In both cases, all fields are sufficiently initialized (and
it was to allow such things that I thought the rule involved
trivial vs. non-trivial default constructor). And would cause
your second example to fail to compile, since X::val has a
trivial default constructor. (It doesn't cover all cases, of
course. For example:

struct A
{
virtual void f() ; // means default ctor non-trivial.
int i ;
} ;

struct B
{
A a ;
} ;

B const b ;

would still create a const object in which A::i wasn't
initialized.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 5 '07 #11

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

Similar topics

8
by: Sergey Tolstov | last post by:
Hello, I am working with Visual C++ 6.0 compiler. In the following declaration: int const A = 10, B = 10; both A and B are const. However, in declaration
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
20
by: Blah | last post by:
In MSDN documentation it states.. Remarks The constant declaration can declare multiple constants, for example: public const double x = 1.0, y = 2.0, z = 3.0; The static modifier is not...
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
15
by: deltaquattro | last post by:
Hi, thanks to your suggestions I got "Accelerated C++" and I'm studying it. I found very interesting the possibility of initializing constants at runtime, but it looks like I'm not getting it...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
5
by: gw7rib | last post by:
I was having linking errors when I put: const LPCTSTR Main_window_name = _TEXT("Thingy_main_window"); in one file and extern const LPCTSTR Main_window_name; in another. I've since...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.