Limitations on using a local variable in C ++?

I had several questions in a technical interview that I thought I knew, but wanted to check (they said that I transmitted it, but I did not know about it):

  • A variable declared inside a class method ... Can it be used outside this method, for example, in another method? (I said "No)

  • Can a variable declared inside a method be passed as a parameter to another method? (I said yes, but I was not sure)


This is for an entry-level C ++ position, and I'm used to C, so I would like to double check my understanding of C ++ / OO concepts.

+6
c ++ scope oop
source share
6 answers
  • A variable inside a class method created inside this method and completely contained in this method can be used only in this method. His life time is of course. Edit : to clarify, I'm not saying that it cannot be passed to another function call within the scope, and I'm not talking about instantiating a member variable or a static variable.

  • Yes, you can pass it to another method if this method is called from an existing method. What for? Since its lifetime is associated with the parent method, and not with what is called from the method.

Let me illustrate:

//aVar does not exist. int foo(){ int aVar = 1; //it been born cout << doSomething(aVar); // still alive return aVar; //still alive but a copy is being returned from the function. Not aVar itself! } // it dead, man 
+4
source share
  • Usually a variable lifetime is inside the block in which it was declared. Thus, at the end of the scope, it is destroyed.
    BUT there is a case of a Static local variable that is declared inside the method, but the value is stored in memory after the completion of the block. Each call to this function will “see” the value of this variable. Thus, you may have a trick that the CAN variable will be used in another instance of this method.

  • Yes, you can pass it on.

+1
source share

Contrary to popular belief, a variable declared in a member function can be used in another member function. There are two obvious ways to do this.

First, if this member function calls another member function, passing a pointer or referencing this variable to the second member function. A variable exists from the moment the first member function is called until it leaves this call; if during this time it calls some other function, this other code can use the variable (if the member function does something to give it access).

Secondly, if you are dealing with a static variable defined in a member function. This (on one example) is the essence of Meyers singleton. A static variable is defined in a member function, and not just for other members of a singleton, but in fact the rest of the program that accesses the singleton object uses the static variable defined in that member function.

In the second question, you're right - a variable defined in a member function is pretty much like any other local variable. It can be passed as a parameter, like everything else.

+1
source share

First question: Yes, you are right. Variables declared as part of a class method are valid only in this area. When a method exits, this variable loses its scope and can no longer be used.

Second question: Yes, a method can call another method using this local scope. The variable remains in scope and can be used for the duration of the second function call.

0
source share

First of all ..... a variable declared inside a class method .... can it be used outside this method, for example, for an instance in another method

In C ++, no.

Secondly, can a declared variable inside a method be passed as a parameter to another method?

In C ++, in a non-competitive application, yes. In the case of concurrency and the receiving function takes its parameter by reference, you must make sure that the object you pass does not destroy while the receive function uses it.

0
source share
 First of all.....a variable declared inside a Class Method.... can that be used outside of that method, like for instance in another method 

Of course. For example,

 class Base { public: static Base *create_instance() { static int variable_declared_inside_class_method = 0; return new Base(variable_declared_inside_class_method); } Base(int &var_) : var(var_) {} int inc_var() { var++; // this method uses variable_declared_inside_class_method } private: int &var; }; 

Note that variable_declared_inside_class_method does not work on the stack. That is why it works here.

 Secondly, can a variable declared inside a method be passed as a parameter for another method? 

That's right.

0
source share

All Articles