user475353
user475353

Reputation:

Restrictions on local variable usage in C++?

I had a few questions in a technical interview that I thought I knew, but wanted to double-check (they said I passed it, but I was unsure about these):

  1. A variable declared inside a Class Method... Can that be used outside of that method, like for instance in another method? (I said no)

  2. Can a variable declared inside a method be passed as a parameter to another method? (I said yes but I wasn't sure)


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

Upvotes: 6

Views: 555

Answers (6)

kirakun
kirakun

Reputation: 2790

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

Sure. 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 live on the stack. This is why it works here.

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

Absolutely.

Upvotes: 0

wheaties
wheaties

Reputation: 35980

  1. A variable within a class method, that's instantiated within that method and wholly contained within that method, can only be used within that method. Its lifetime is finite. Edit: to clarify, I'm not saying it can't be passed to another function call within the function scope and I'm not talking about instantiating a member variable or static variable.

  2. Yes, you can pass it to another method if that method is called from within the method it exists. Why? Because its lifetime it tied to the parent method, not the one that is called from within the method.

Let me illustrate:

//aVar does not exist.
int foo(){
   int aVar = 1; //it's been born
   cout << doSomething(aVar); // still alive

   return aVar; //still alive but a copy is being returned from the function.  Not aVar itself!
} // it's dead, man

Upvotes: 4

Jerry Coffin
Jerry Coffin

Reputation: 490148

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

The first is if that member function calls another member function, passing a pointer or reference to that variable to the second member function. The variable exists from the time the first member function is called until it exits from that call; if it calls some other function during that time, that other code can use the variable (if the member function does something to give it access).

The second is if you're dealing with a static variable defined in a member function. This is (for one example) the essence of the Meyers singleton. A static variable is defined in a member function, and not only other members of the singleton, but in fact all the rest of the program that accesses the singleton object use the static variable defined in that member function.

For 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, just like anything else.

Upvotes: 1

Yochai Timmer
Yochai Timmer

Reputation: 49251

  1. Normally a variable's lifetime is inside the block it's declared in. So at the end of the scope it is destroyed.
    BUT there's the case of Static Local Variable which is declared inside a method, but the value is saved in memory after the block is finished. Every call to that function will "see" the value of that variable. So, you could have a trick question, that the variable CAN be used in another instance of that method.

  2. Yes you can pass it.

Upvotes: 1

wilhelmtell
wilhelmtell

Reputation: 58677

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

In C++, no.

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

In C++, in a non-concurrent application, yes. In the case of concurrency and the receiving function takes its parameter by reference you must make sure the object you pass doesn't destruct while the receiving function uses it.

Upvotes: 0

James
James

Reputation: 5425

First Question: Yes, you are correct. Variables declared within the scope of a class method are only valid in that scope. When the method exits, that variable loses scope and is no longer usable.

Second Question: Yes, the method can call another method, using that locally scoped variable. The variable remains in scope and is usable through the duration of the second function call.

Upvotes: 0

Related Questions