Topic: Proposal for default values when declaring the class
Author: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Fri, 30 Apr 2004 15:04:49 +0000 (UTC) Raw View
In article <am50909691td3473pkvng203rts78gt08i@4ax.com>, Meang Akira
Tanaka <meang@post.com> writes
>Hi
>
>It may not be the right forum but I would like to make a proposal.
>(if this is not the right forum could anybody please tell me which
>forum this should be be submitted too)
Fine place to float proposals.
>
>In my experience with programming C++.
>
>I have wondered why it hasn't been possible to define default values
>in the class declaration. One had to do it in the constructor.
However your problem is indirectly addressed by the proposal to provide
a syntax for forwarding constructors. This is one of the new features
that is just about ready for inclusion in the next full release of C++
(circa 2008/9) but could easily be supported as an extension by existing
compilers as soon as we have added the necessary words to the working
paper (document that will eventually become the next C++ IS)
>
>e.g.
>
>class foo
>{
> int mVar1;
> bool mVar2;
> char mVar3;
>
> foo() : mVar1(100), mVar2(true), mVar3('a')
> {
> }
>
> foo(int newVar1) : mVar1(newVar1), mVar2(true), mVar3('a')
> {
> }
>
> foo(bool newVar2) : mVar1(105), mVar2(newVar2), mVar3('a')
> {
> }
>
>}
>
>The disadvantage with the current approach is manyfold.
Using forwarding ctors the above becomes:
class foo {
int var1;
bool var2;
char var3;
public:
foo(int v1 = 100, bool v2 = true, char v3 = 'a');
// that covers your first two ctors
foo(bool v2) : foo(105, v2){}
This change (forwarding ctors) does not quite do what you were proposing
but it is much simpler to incorporate into the language and simpler for
the implementors to implement.
--
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
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: meang@post.com (Meang Akira Tanaka)
Date: Sat, 1 May 2004 16:51:24 +0000 (UTC) Raw View
On Fri, 30 Apr 2004 15:04:49 +0000 (UTC), francis@robinton.demon.co.uk
(Francis Glassborow) wrote:
<*snip*>
>Using forwarding ctors the above becomes:
>
>class foo {
> int var1;
> bool var2;
> char var3;
>public:
> foo(int v1 = 100, bool v2 = true, char v3 = 'a');
> // that covers your first two ctors
> foo(bool v2) : foo(105, v2){}
>
>
>
>This change (forwarding ctors) does not quite do what you were proposing
>but it is much simpler to incorporate into the language and simpler for
>the implementors to implement.
It has it charms... and definitly better than nothing, but I think it
isn't as clear as the one I proposed...
how are the arguments mapped?
It isn't clear from the variable names you used v1, v2 etc.. or did
you mean var1 = 100, var2 = true etc...
if that is not the case
how do you know which variable should be mapped to which attribute?
class foo
{
int var1;
int var2;
int var3;
public:
foo(int v1 = 100, bool v2 = 105, char v3 = 107 );
}
but I am definitly looking forward to compilers who will support it...
br
Meang
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: meang@post.com (Meang Akira Tanaka)
Date: Fri, 30 Apr 2004 03:14:25 +0000 (UTC) Raw View
Hi
It may not be the right forum but I would like to make a proposal.
(if this is not the right forum could anybody please tell me which
forum this should be be submitted too)
In my experience with programming C++.
I have wondered why it hasn't been possible to define default values
in the class declaration. One had to do it in the constructor.
e.g.
class foo
{
int mVar1;
bool mVar2;
char mVar3;
foo() : mVar1(100), mVar2(true), mVar3('a')
{
}
foo(int newVar1) : mVar1(newVar1), mVar2(true), mVar3('a')
{
}
foo(bool newVar2) : mVar1(105), mVar2(newVar2), mVar3('a')
{
}
}
The disadvantage with the current approach is manyfold.
1. One has to define the initial value for all or some of the member
variables again and again for each declared constructor. Typically the
values which has not been assigned a value explicitly, they typically
have the same value no matter which constructor was invoked.
2. When someone is adding an another constructor to the class. They
have to make sure that all the variables have been initialized. In an
idealistic world everybody would probably remember to do it, however I
claim this is not the case in the real world.
3. If the class is added with a new attribute, one has to ensure that
all the constructors do initialize the value.
I propose that one can do the following:
class foo
{
int mVar1 = 100;
bool mVar2 = true;
char mVar3 = 'a';
foo()
{
}
foo(int newVar1) : mVar1(newVar1)
{
}
foo(bool newVar2): mVar1(105), mVar2(newVar2)
{
}
}
The difference between this and the current implementation of the
standard is the variable is assigned to a value. if it isn't overriden
in the constructor. What are the advatanges?
Well...
If the an initial value is given, then they don't need to be defined
for all the new constructor actually needs to assign it to another
value. This ensures that the instant has a sane or at least a more
sane value than some value in memory.
if an instance is invoked with default constructor then the member
attributes has the following value:
mVar1 = 100;
mVar2 = true
mVar3 = 'a'
if an instance is invoked with the second constructor then the member
attributes has the following value:
mVar1 = newVar1
mVar2 = true
mVar3 = 'a'
if an instance is invoked with the second constructor then the member
attributes has the following value:
mVar1 = 105
mVar2 = newVar2
mVar3 = 'a'
The mVar is overriden with 105
br
Meang
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]