2

I tried out a c++ program, where i declared a variable of type int named "a" inside a class. And created a function named "b" inside which again i declared a variable named "a" and assigned the value to it. The variable "a" inside the function is considered as local variable. And if i want to assign the value to the variable a which is present in the class definition(not inside the function), how can i do it?

#include <iostream>

using namespace std;

class a{
    public:int a;//need to assign value for this "a" inside the function how can i do it
    int b(){
        int a=5;
        a=7;
        cout<<a;
    }
};
int main() {
    a A;
    A.b();
}
9
  • put int & ra = a; before you decare a in the function. Then use ra to refre to the class scope a. Commented Feb 10, 2018 at 19:10
  • 3
    This is purely academic, right? You don't actually want a member variable and a member function local variable with the same name, do you? Commented Feb 10, 2018 at 19:12
  • 1
    Hang on. You can't have a data member a in a class named a, can you? Commented Feb 10, 2018 at 19:12
  • 1
    @StoryTeller yes i tried this program. It works when the name of the class and data members are same Commented Feb 10, 2018 at 19:17
  • 1
    You sent me checking, and it does indeed seem valid. I learned something today. Commented Feb 10, 2018 at 19:21

4 Answers 4

8

To access the class variable you can use this keyword . For more explanation and getting knowledge of 'this` keyword you can go here

#include <iostream>

using namespace std;

class a{
    public:int a;//need to assign value for this "a" inside the function how can i do it
    int b(){
        int a=5;
        a=7;
        this->a = 8; // set outer a =8
        cout<< "local variable a: " << a << endl;
        cout<< "class a object variable a: " << this->a << endl;
        return 0;
    }
};
int main() {
    a A;
    A.b();
    cout << "A's variable a: " << A.a << endl; //should print 8
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

5

Use this->a.

this allows you to access the members and methods of an instance from within.

EDIT: this is an academical exercise, but a bad programming practice. Just use different names for classes, members and variables.

1 Comment

... even if they are hidden by something in the member functions scope.
2

Specifying the class-qualified name of a, i.e.: a::a, will do:

a::a=7;

4 Comments

That can't work. This will refer to the constructor.
Well, this says it shouldn't. I'd check your compiler flags if I were you.
Well, that's distressing
Aha, so it is indeed correct. The injected class name is hidden. +1.
0

The problem is lying under the lack of this pointer to the class variable.

If you have a local variable inside your class function that is identical to the class variable, then you must use this keyword for the compiler to distinguish.

An easy mistake people tend to do is when having exactly the same names for the mutator's parameter variable as the class' variable.

#include <iostream>

using namespace std;

class Foo {
    int numberA;
public:
    void setNumberA(int numberA) {
        numberA = numberA; /*Incorrect way, the compiler thinks 
                             you are trying to modify the parameter*/

        this->numberA = numberA; //Correct way
    }
};

In your case, you must use this->a inside the function b().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.