"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > ## Why is my temperature value being stored in the array instead of the intended variable in my C++ class?

## Why is my temperature value being stored in the array instead of the intended variable in my C++ class?

Posted on 2025-03-22
Browse:340

## Why is my temperature value being stored in the array instead of the intended variable in my C   class?

Shadowing Variables in C

In object-oriented programming, shadowing occurs when a variable defined within a class has the same name as a variable in an outer scope. This can lead to unexpected behavior, as the inner variable takes precedence over the outer variable.

Problem: Shadowing in a Class

Consider the following class definition:

class Measure {
    int N;
    double measure_set[];
    char nomefile[];
    double T;

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

The get method in this class is intended to read values from a file and save them in the measure_set array, and to read a temperature value and store it in the T variable.

However, when you implement the get method as follows:

void Measure::get() {
    cout > nomefile;
    cout > measure_set[M];
        if (f.eof()) break;
        M  ;
    }
    f.close();
    N = M   1;

    cout > T;
    cout 

You noticed that the temperature value (T) is being stored in the first element of the measure_set array (measure_set[0]) instead of the intended T variable.

Solution

This occurs because C allows variables with the same name to be declared in different scopes. In this case, the T variable declared in the get method shadows the class member variable T.

To avoid shadowing, you can either use different names for the variables or use the scope resolution operator (::) to explicitly refer to the class member variable.

Using a different name for the temperature variable in the get method would look like this:

void Measure::get() {
    cout > nomefile;
    cout > measure_set[M];
        if (f.eof()) break;
        M  ;
    }
    f.close();
    N = M   1;

    cout > temperature;
    T = temperature;
    cout 

Using the scope resolution operator to explicitly refer to the class member variable would look like this:

void Measure::get() {
    cout > nomefile;
    cout > measure_set[M];
        if (f.eof()) break;
        M  ;
    }
    f.close();
    N = M   1;

    cout > this->T;  // Use the scope resolution operator to refer to the class member variable
    cout 
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3