| View previous topic :: View next topic |
| Author |
Message |
mangled_us@yahoo.com Guest
|
Posted: Wed Oct 12, 2005 4:37 pm Post subject: Reason why it's impossible to initialise non-static const cl |
|
|
I have googled but was not able to find the actual reason why we cannot
initialise non-static constant members in the class definition ? ISTM
that the definition will always be available where the const value
would be used so what was the actual problem with permitting this dak ?
A short example of what I mean is as follows :
class a_class
{
const int c_int = 33;
};
Why should that be illegal except for static members ?
Thanks,
David
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Wed Oct 12, 2005 10:46 pm Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
mangled... (AT) yahoo (DOT) com wrote:
| Quote: | class a_class {
const int c_int = 33;
};
Why should that be illegal except for static members ?
|
I'm sure you'll get many answers to this question, but let me pose
one of my own:
If c_int always has the value 33, why shouldn't it be static?
Is there some reason that you want it to take up space in every
instance of a_class? Why?
If the value is ALWAYS going to be 33, make it static! Let the
optimizer get rid of it completely! You still get to make it
public, protected or private. What possible reason for NOT making
it static?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Wed Oct 12, 2005 10:48 pm Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
[email]mangled_us (AT) yahoo (DOT) com[/email] writes:
| Quote: | class a_class
{
const int c_int = 33;
};
Why should that be illegal except for static members ?
|
What's the benefit of making c_int non-static if the value is constant
for all instances of a_class?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
AnonMail2005@gmail.com Guest
|
Posted: Wed Oct 12, 2005 10:48 pm Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
Non-static class members are initialized in the constructor(s) that's
why. For non-statics, each instance of a class will have a different
instance of the data member contained within it. And that data member
can be initialized however you choose in the constructor(s).
If it is truly a class level constant and you don't need to have each
class contain the data member you can choose a static member. Or you
can choose an enum.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Improving Guest
|
Posted: Wed Oct 12, 2005 10:49 pm Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
This is guesswork but it seems to make sense to me.
Suppose you created three objeacts from same class, each of which has a
const member that as usual is initialised in the constructor, well
theres nothing to say that all those const members have to be equal to
each other. it could be a const id field or something, then you wouldnt
want two objects with same id would you?
Now if we had initialised this const member as you wanted, we would
lose the ability to have the behaviour above and still keep the member
const.
Whereas a static is shared amongst all objects of the class so that is
the behaviour that we just described above.
I suppose if you think about it for a bit it makes perfect sense.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Abhishek Pamecha Guest
|
Posted: Thu Oct 13, 2005 1:42 pm Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
There can be 2 cases what the const-ness of a value can mean:
class-wide const : You should use static as that saves storage space
and is not dependent on instantiation of an object.
object-wide const: If you give it a thought, it would be really
immaterial whether you can declare a member a const or not. Probably
what you are looking for is a variable that, once initialized, is not
accidently changed in the internal implementation of the class as well.
so now you would say, why make it illegal to declare this way, when it
does not make a difference?
It has to do with problems in linking and the fact the "definition" of
a variable is separate from "declaration." As you may be knowing, when
we assign something to a variable we are "defining" it and also
allocating memory to store that value. So compilation ensures that one
variable should never have two different memory allocations. Or else
how would we know which one to change?
So, There can be only one storage allocation for each variable. which
translates to "there can be only one definition for a variable".
Let us compare and see what are const static members and how are they
stored.
Const Static members are declared in a class(.h file) and
defined(assigned a value) EXACTLY at one place (presumably in a .cpp
file). So any usage of that static const member will resolve to only
one symbol definition and hence there will be no "duplicate symbols
found" during compliation. A Happy world!
Where as with const member variables the only place you can declare and
"define" them is in a *.h file. (inside the braces enclosing class
declaration or else i run out of ideas where will you initialize it.
You dont have an object for it and you have to respect the scope
restriction). But alas, this header file is included in so may .cpp
files. so the same const class member will be declared and defined in
all those .cpp files.
Since there would arise multiple definitions for the same
identifier(const class member) the resolution of that symbol during
compile time will not be unique and compilation would fail. A confusing
world.
That is a reason, a compiler will not let you declare a const class
member.
Anyways, declaring static const members is a bad practice as it may
introduce mysterious errors. ( imagine using an uninitialized global
var! )
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
werasm Guest
|
Posted: Fri Oct 14, 2005 10:54 am Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
[email]mangled_us (AT) yahoo (DOT) com[/email] wrote:
| Quote: | A short example of what I mean is as follows :
class a_class
{
const int c_int = 33;
};
|
I would guess because the constant is bound to an instance of the class
and not the class itself. Futhermore, the fact that it is bound to the
class instance, means that it's value is not constant for all the
instances - it may vary. If one did not want it to vary, well, then
make it static (or enumerated). Example:
struct xC
{
xC( unsigned val )
: c_int( init_c_int( val ) )
{ ; }
private:
enum{ eMultiplier = 50; }
unsigned init_c_int( unsigned val )
{
return eMultiplier*val;
}
unsigned const c_int_;
};
Note in above case the multiplier is truly class bound, and therefore
can be initialised as is. I did not do it in the above example, but I
try to not initialise my static constants in my interface. If they need
to change, it may causes large scale re-compilation. If they only
effect the class implementation, initialise it there - or use the pimpl
idiom.
Regards,
Werner
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Improving Guest
|
Posted: Fri Oct 14, 2005 11:12 am Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
| Quote: | imagine using an uninitialized global var!
|
Isn't that impossible?
Doesnt the standard ensure that all globals not otherwise specified
will be initialised with a bitwise zero?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Abhishek Pamecha Guest
|
Posted: Sun Oct 16, 2005 9:46 am Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
is it impossible to imagine? :->
We can atleast _imagine_ the impossibility and some rogue compilers
have the daring to _realize_ it !!
I agree with you but I was just giving an analogy of how bad it could
be.
and Sankara said that no analogy is perfect. :-)
Please check
Bjarne Stroustrup and Margaret Ellis, The Annotated C++ Reference
Manual, Addison-Wesley, 1990, pp. 20-21.
(reference used from Bruce Eckel chapter 10 subtopic: Static
initialization dependency
where it discourages static initializations. So I think it would apply
to static const initializations too.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sun Oct 16, 2005 4:21 pm Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
In article <1129229427.614151.298670 (AT) g44g2000cwa (DOT) googlegroups.com>,
Improving <russ (AT) haydon449 (DOT) plus.com> writes
| Quote: | imagine using an uninitialized global var!
Isn't that impossible?
Doesnt the standard ensure that all globals not otherwise specified
will be initialised with a bitwise zero?
|
struct X {
int i;
X(){};
};
X x;
Now, IIUC x.i will not be initialised because the provision of a user
written default ctor hands control to the class implementer.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
tpochep@mail.ru Guest
|
Posted: Mon Oct 17, 2005 1:11 pm Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
| Quote: | Now, IIUC x.i will not be initialised because the provision of a user
written default ctor hands control to the class implementer.
|
3.6.2/1 says:
Objects with static storage duration (3.7.1) shall be zero-initialized
(8.5) before any other initialization takes place.
So, x.i will be (at least) zero-initialized.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Christoph Schulz Guest
|
Posted: Mon Oct 17, 2005 2:02 pm Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
Hello!
Francis Glassborow wrote:
| Quote: | struct X {
int i;
X(){};
};
X x;
Now, IIUC x.i will not be initialised because the provision of a user
written default ctor hands control to the class implementer.
|
Really? 3.6.2/1 says:
"Objects with static storage duration (3.7.1) shall be zero-initialized
(8.5) before any other initialization takes place."
That means to me that although X::X() does not initialize the member "i"
explicitly (during dynamic initialization), it already has a defined
value, namely zero.
Am I reading the standard correctly?
Regards,
Christoph
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Mon Oct 17, 2005 5:17 pm Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
Francis Glassborow wrote:
| Quote: | Now, IIUC x.i will not be initialised because the provision of a user
written default ctor hands control to the class implementer.
|
[email]tpochep (AT) mail (DOT) ru[/email] wrote:
| Quote: | 3.6.2/1 says:
Objects with static storage duration (3.7.1) shall be zero-initialized
(8.5) before any other initialization takes place.
So, x.i will be (at least) zero-initialized.
|
Except that surely the the constructor can set x.i to an indeterminate
(possibly trapping) value?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Thu Oct 20, 2005 12:03 pm Post subject: Re: Reason why it's impossible to initialise non-static cons |
|
|
Martin Bonner wrote:
| Quote: | Francis Glassborow wrote:
Now, IIUC x.i will not be initialised because the provision of a user
written default ctor hands control to the class implementer.
[email]tpochep (AT) mail (DOT) ru[/email] wrote:
3.6.2/1 says:
Objects with static storage duration (3.7.1) shall be
zero-initialized (8.5) before any other initialization takes
place.
So, x.i will be (at least) zero-initialized.
Except that surely the the constructor can set x.i to an
indeterminate (possibly trapping) value?
|
How? Uninitialized objects might have trapping values, but I
don't see how you can set one.
--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|