From 6777036938767713880
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,df0f8d484c0311f9,start
X-Google-Attributes: gidf78e5,public
Path: controlnews3.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!kibo.news.demon.net!mutlu.news.demon.net!demon!mail2news.demon.co.uk!devnull
From: meang@post.com (Meang Akira Tanaka)
Newsgroups: comp.std.c++
Subject: Proposal for default values when declaring the class
Date: Fri, 30 Apr 2004 03:14:25 +0000 (UTC)
Organization: UNI2 Internet Kunde
Lines: 113
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <am50909691td3473pkvng203rts78gt08i@4ax.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Trace: mail2news.demon.co.uk 1083294865 12749 10.0.0.1 (30 Apr 2004 03:14:25 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Fri, 30 Apr 2004 03:14:25 +0000 (UTC)
X-Received: from mulga.cs.mu.oz.au ([128.250.1.22])
	by news.demon.co.uk with esmtp (Exim 4.12)
	id 1BJOTp-0003JU-00
	for mail2news@news.news.demon.net; Fri, 30 Apr 2004 03:14:25 +0000
X-Received: from mulga.cs.mu.OZ.AU (localhost [127.0.0.1]) by mulga.cs.mu.OZ.AU with ESMTP
	id i3U3ELi2016725; Fri, 30 Apr 2004 13:14:21 +1000 (EST)
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id i3U3ELdm016716;
	Fri, 30 Apr 2004 13:14:21 +1000 (EST)
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Path: comp-std-cpp-robomod!not-for-mail
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Delivered-To: std-c++@ucar.edu
X-Newsgroups: comp.std.c++
X-Newsreader: Forte Agent 1.93/32.576 English (American)
X-NNTP-Posting-Date: Wed, 28 Apr 2004 22:59:23 CEST
X-Spam-Level: 
X-Spam-Checker-Version: SpamAssassin 2.60-mulga_r1 (1.212-2003-09-23-exp) on 
	mulga.cs.mu.OZ.AU
X-Spam-Status: No, hits=0.0 required=5.2 tests=none autolearn=no 
	version=2.60-mulga_r1
Xref: controlnews3.google.com comp.std.c++:20

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                       ]



