typos in set functions

Discussion in 'C++' started by Siemel Naran, Nov 30, 2004.

  1. Siemel Naran

    Siemel Naran Guest

    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;
    }
     
    Siemel Naran, Nov 30, 2004
    #1
    1. Advertising

  2. 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
     
    Serge Paccalin, Nov 30, 2004
    #2
    1. Advertising

  3. Siemel Naran

    Micha Guest

    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" ?
     
    Micha, Nov 30, 2004
    #3
  4. Siemel Naran

    Pete Becker Guest

    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)
     
    Pete Becker, Nov 30, 2004
    #4
  5. Siemel Naran

    Old Wolf Guest

    "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.
     
    Old Wolf, Nov 30, 2004
    #5
  6. Siemel Naran

    Siemel Naran Guest

    "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.
     
    Siemel Naran, Dec 2, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dagoodyear

    Parsing text acounting for typos?

    dagoodyear, Jun 12, 2005, in forum: Java
    Replies:
    1
    Views:
    396
    Harald
    Jun 12, 2005
  2. Manish Jethani

    How to detect typos in Python programs

    Manish Jethani, Jul 25, 2003, in forum: Python
    Replies:
    15
    Views:
    1,678
    David Bolen
    Jul 29, 2003
  3. Bob Gailer
    Replies:
    2
    Views:
    442
    Bengt Richter
    Jul 26, 2003
  4. Peter v.d. Berger

    Matching filenames with typos

    Peter v.d. Berger, Dec 4, 2006, in forum: Perl
    Replies:
    1
    Views:
    1,878
    Jim Gibson
    Dec 5, 2006
  5. Wolfgang Nádasi-donner

    Typos in eigenclass - Changes in Ruby 1.9

    Wolfgang Nádasi-donner, Aug 3, 2007, in forum: Ruby
    Replies:
    0
    Views:
    156
    Wolfgang Nádasi-donner
    Aug 3, 2007
Loading...

Share This Page