typos in set functions

S

Siemel Naran

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;
}
 
S

Serge Paccalin

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
 
M

Micha

Siemel said:
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" ?
 
P

Pete Becker

Siemel said:
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.
 
O

Old Wolf

Siemel Naran said:
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.
 
S

Siemel Naran

Old Wolf said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top