Reputation: 4893
In the following program, I have declared class inside the main() function.
case 1:
int main()
{
static int i = 10; // static variable
class A
{
public:
A()
{
std::cout<<i;
}
};
A a;
return 0;
}
and It's working fine in G++ compiler.
But, If I remove static
keyword and compile it, compiler gives an error.
Case 2:
int main()
{
int i = 10; // removed static keyword
class A
{
public:
A()
{
std::cout<<i;
}
};
A a;
return 0;
}
Error:
In constructor 'main()::A::A()':
13:32: error: use of local variable with automatic storage from containing function
:cout<<i;
^
7:13: note: 'int i' declared here
int i = 10;
^
Why case 1 working fine? and Why doesn't working case 2?
Upvotes: 0
Views: 10292
Reputation: 1
/*Class inside the main using the c++.*/
#include<iostream>
#include<string>
using namespace std;
int main(){
class Details{
public :
string name;
string address;
int regNo;
Details(string name, string address, int regNo){
this->name = name;
this->address = address;
this->regNo = regNo;
}
void display(){
cout << "The name of the student is : " << name << "\n";
cout << "The address of the student is : " << address << "\n";
cout << "The register number is : " << regNo << "\n";
}
};
cout << "Enter the name of the student : ";
string name;
cin >> name;
cout << "Enter the address of the student : ";
string address;
cin >> address;
cout << "Enter the register number : ";
int regNo;
cin >> regNo;
Details dt(name, address, regNo);
dt.display();
return 0;
}
Upvotes: -1
Reputation: 42
I am not sure please correct me if I am wrong, this may be because static variable and class both are stored in heap hence case 1 is working fine, and in case 2 as the variable i is not stored in heap it creating problem.
Upvotes: -1
Reputation: 3068
Copy/paste from https://www.quora.com/Why-cant-local-class-access-non-static-variables-of-enclosing-function
You are wondering about a variable outside of a class. I will explain this the none-C++ way. Let's look at it from the paradigm of general machine architecture and the way programming languages are oft defined. The issue is stack frames, the concept of the stack, and how program refer to memory locations.
When a function is called, the variables of that function are pushed onto the stack. A function and its variables are often a sequence of memory locations. When the function is finished, it and those variables are popped off the stack. That means when the function is called, variables come into existence. When the function is done, the variables depart immediately. Each variable, like the function itself are memory locations (may be assigned to registers).
Declaring the class does not declare a variable. The class is just a definition in the world of C++ and has no linkage to the variable defined in the outer scope. The phrase, automatic storage duration, is roughly synonymous with the idea of the variable (memory) automatically recovered when the function exits. Even though it is C++, when it compiles, it is still machine language and will obey the rules of the machine. The method you called on the class is part of the class but is not part of the function. Only the class definition is local to the function.
All functions, regardless of where they exist are their own stack frame. The standard way stack frames are done means, unless the memory location referenced is valid, the data will be inaccessible by the time the function in the class is called. In this case, it isn't because the variable in the outer scope has been reclaimed, but because when the method in the class is called, the stack frame in which the outer variable exists is not active in the series of registers used by the method being called. The compiler was encoded with the understanding of this process and gave an error message to ward off the trouble that would ensue if such access was attempted.
You can still access the variable if you affix the static keyword to the variable. That is mentioned in the web page Local Classes in C++ that has the same code example you have listed. Basically, you have to extend the storage duration or the duration that the memory for the variable remains valid in the enclosing scope. Generally, a good way to think through these kind of error messages is through knowledge of the language specification, but in terms of time, relating the representations back to machine architecture can zero in on the underlying reasons why.
Simply pass the variable you'd like to use inside of the class as an argument to the constructor (i've made it a reference member, so changes in i
will be visible inside of the class as well, but be aware that as soon as the function exits, i
will go out of scope):
#include<iostream>
int main()
{
int i = 10; // static variable
class A
{
private:
int &r_i;
public:
A(int &i)
:
r_i(i)
{
std::cout<<r_i;
}
};
A a(i);
return 0;
}
Upvotes: 3