Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typos in set functions

Reply
Thread Tools

typos in set functions

 
 
Siemel Naran
Guest
Posts: n/a
 
      11-30-2004
Is there any trick to avoid typos in set functions, such as

void date::set(int day, int month, int year) {
d_day = day;
month = month; // oops: should be d_month = month
d_year = year;
}


 
Reply With Quote
 
 
 
 
Serge Paccalin
Guest
Posts: n/a
 
      11-30-2004
Le mardi 30 novembre 2004 à 11:34, Siemel Naran a écrit dans
comp.lang.c++*:

> Is there any trick to avoid typos in set functions, such as
>
> void date::set(int day, int month, int year) {
> d_day = day;
> month = month; // oops: should be d_month = month
> d_year = year;
> }


const parameters can help in this case.

void date::set(const int day,const int month,const int year)
{
d_day = day;
month = month; // oops: compilation error
d_year = year;
}

--
___________ 2004-11-30 12:08:04
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
Reply With Quote
 
 
 
 
Micha
Guest
Posts: n/a
 
      11-30-2004
Siemel Naran wrote:

> Is there any trick to avoid typos in set functions, such as
> month = month; // oops: should be d_month = month


what about declaring the arguments "const" ?
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      11-30-2004
Siemel Naran wrote:
>
> Is there any trick to avoid typos in set functions, such as
>
> void date::set(int day, int month, int year) {
> d_day = day;
> month = month; // oops: should be d_month = month
> d_year = year;
> }


Test your code.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      11-30-2004
"Siemel Naran" <> wrote:
> Is there any trick to avoid typos in set functions, such as
>
> void date::set(int day, int month, int year) {
> d_day = day;
> month = month; // oops: should be d_month = month
> d_year = year;
> }


Turn up your compiler warning level? You may get
a warning that the statement has no effect.
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      12-02-2004
"Old Wolf" <> wrote in message
> "Siemel Naran" <> wrote:


> > Is there any trick to avoid typos in set functions, such as
> >
> > void date::set(int day, int month, int year) {
> > d_day = day;
> > month = month; // oops: should be d_month = month
> > d_year = year;
> > }

>
> Turn up your compiler warning level? You may get
> a warning that the statement has no effect.


OK, but many compilers only issues the warning only if month is a
fundamental or POD type. With a user defined operator=, the compiler can't
eally make the assumption that the statement has no effect (though would be
nice if it did).

Everyone, thanks for the pointers. Now how to avoid the problem

d_month = d_month; // oops: should be d_month = month

I suppose we could turn up the warning level and get a warning that variable
month is not used or test the code thoroughly. Is there a way to force a
compile error here?

Thanks.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing text acounting for typos? dagoodyear Java 1 06-12-2005 09:19 PM
Typos os Bugs(70-315 self paced)? john hansen MCSD 4 10-30-2003 06:43 PM
How to detect typos in Python programs Manish Jethani Python 15 07-29-2003 04:53 PM
Re: How to detect typos in Python programs Bob Gailer Python 2 07-26-2003 03:00 AM
Typos in the Exam Davin Mickelson MCSE 3 07-21-2003 11:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57