C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Proposal for default values when declaring the class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Meang Akira Tanaka
Guest





PostPosted: Fri Apr 30, 2004 3:14 am    Post subject: Proposal for default values when declaring the class Reply with quote



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 ]

Back to top
Francis Glassborow
Guest





PostPosted: Fri Apr 30, 2004 3:04 pm    Post subject: Re: Proposal for default values when declaring the class Reply with quote



In article <am50909691td3473pkvng203rts78gt08i (AT) 4ax (DOT) com>, Meang Akira
Tanaka <meang (AT) post (DOT) com> writes
Quote:
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.

Quote:

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)

Quote:

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 ]


Back to top
Meang Akira Tanaka
Guest





PostPosted: Sat May 01, 2004 4:51 pm    Post subject: Re: Proposal for default values when declaring the class Reply with quote



On Fri, 30 Apr 2004 15:04:49 +0000 (UTC), [email]francis (AT) robinton (DOT) demon.co.uk[/email]
(Francis Glassborow) wrote:

<*snip*>
Quote:
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 ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.