Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

c++ - Shadowing variables

My question is about variables defined into classes. I show you my problem.

I have defined this class:

class Measure {

int N;
double measure_set[];
char nomefile[];
double T;

public:
    void get( );
    void printall( );
    double mean( );
    double thermal_comp( );
};

I would like method get to do the following:

  1. read numbers from a .dat file and save into measure_set array;
  2. read user input and save it into variable T;

Here is what I've done:

void Measure::get() 
{   
    cout << "Insert filename:" << endl;
    cin >> nomefile;
    cout << endl;
    cout << nomefile << endl;
    cout << endl;

    int M=0;
    int nmax=50;

    ifstream f;
    f.open(nomefile);
    while(M<nmax)
    {
        f >> measure_set[M];
        if(f.eof()) 
        break;
        M++;
    }
    f.close();
    N=M+1;

    cout << "Insert temperature:" << endl;
    cin >> T;
    cout << endl;
} 

What happens is that I've noticed that T is memorized in measure_set[0]. Why this happens and how can I write a working code? I'm not that expert in C++, using this only for computation purposes, although I could solve my problem in other ways I would like to learn how to make this work in C++. Thanks a lot!

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In both C and C++ it is legal for the same name to be used within multiple scopes - some compilers (e.g. gcc -Wshadow) will provide a mechanism that warns you about this since it can cause confusion.

#include <iostream>

int i = 0;
int main() {
    int i = 1;
    for (int i = 0; i < 10; ++i) {
        int i = 2 * i;
        std::cout << i << std::endl;
    }
    return 0;
}

Some compilers will compile this and output '0, 2, 4, 6, 8, ...'. Some won't.

A common way to avoid this is to use prefixes. "m_" for "member", "s_" for "static", "g_" for "global", etc. Some coding styles use a "_" suffix for member variables. This also helps to keep member variables from clashing with similarly named getters/setters if that's the way your camel rolls.

class Measure {
    int         m_n;
    double      m_measureSet[MEASURE_SET_SIZE]; // [] is not legal in a class.
    std::string m_nomefile;
    double      m_t;

public:
    const std::string& nomefile() const { return m_nomefile; }
    ...
};

Where this pays off is in the usage, I tend to find developers are encouraged to use the accessors rather than the members (it seems to be easier to add () at the end than type "m_" at the beginning).

std::cout << "Measuring file " << nomefile << std::endl;

would become

std::cout << "Measuring file " << m_nomefile << std::endl;

or better:

std::cout << "Measuring file " << nomefile() << std::endl;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...