C++ :: Static Variable Declaration
Oct 4, 2014If i declare 2 variables like this static int first, second; will both of them be declared static or will only first be declared static and second a regular variable?
View 5 RepliesIf i declare 2 variables like this static int first, second; will both of them be declared static or will only first be declared static and second a regular variable?
View 5 RepliesHere is the code,
Code:
class A {
};
A& CreateObject() {
static A a;
return a;
} static A aa;
int main() {
return 0;
}
So is there any difference between a defined in CreateObject and aa?
I read that Memory is allocated during definition of a variable and not during declaration. Declaration is something like,
Code: int x;
And definition is assigning some value to it. This is what my professor taught. My doubt is if memory is not allocated during declaration, then how the compiler successfully compiles and runs the following, which i had already tried.
Code:
#include<stdio.h>
#include<conio.h>
int main() {
int c;
int *p=&c;
printf("%x",p);
getch();
return 0;
}
The variable c is only declared. But the program outputs a memory address. Shouldn't it show an error?
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class ir;
class Bank_acc {
private:
string name,type,s;
long int accno,temp,balance,in;
[Code]....
errors are:
|6|error: forward declaration of 'class ir'|
|54|error: invalid use of incomplete type 'class ir'|
|99|error: no matching function for call to 'ir::interest()'|
Why I take warning on this code :
Code:
#include<stdio.h>
extern int v = 0; // Declaration and definition of an external
int main(void)
[code]...
Is there any error? Why I take a warning: 'v' initialized and declared 'extern'|||=== Build finished: 0 errors, 1 warnings ===| ???
and what is the meaning of :An extern declaration that initializes a variable serves as a definition of the variable. This rule prevents multiple extern declarations from initializing a variable in different ways.
Code:
#include <iostream>
using namespace std;
void f() {
int x=17;
//cout<<main::y<<endl; i want to access y from main scope
}
int main() {
int y=23;
//cout<<f::x<<endl;
I want to access x from f scope is there any way for this without global declaration? specially about function scopes...
What is difference (memory allocation or any) between declaring a variable inside or outside the variable
program1:
#include<stdio.h>
int main() {
int i;
for (i=0;i=<100;i++) {
[Code] .....
Difference b/w program1 and program2. Its look both are same. but my compiler shows something difference.
(while)
T minus 10 and counting
T minus 9 and counting
T minus 8 and counting
T minus 7 and counting
T minus 6 and counting
T minus 5 and counting
T minus 4 and counting
T minus 3 and counting
T minus 2 and counting
T minus 1 and counting
Declare the following index before the while loop:
int index = 10;
Correctly code a while statement below using the variable index as defined above, to produce the output shown above.
So this is what my code looks like... I also have to convert this same loop into a do while and for loop. So if I can get this one right I think the others should come relatively easy.
while (int index >= 10) {
cout << "T minus " << index;
index--;
}
im doing a program to store name, age, time and fitness. and i need to hold a table of 5 such records.can i do this?
#include <iostream>
using namespace std;
int name1, age1, time1, fitness1;
int name2, age2, time2, fitness2;
int name3, age3, time3, fitness3;
int name4, age4, time4, fitness4;
int name5, age5, time5, fitness5;
[code].....
I got the following lines of code from a book. The variable char c is being used here to demonstration local variable declaration.
while(char c = cin.get() != 'q') {
cout << c << " wasn't it" << endl;
if(char x = c == 'a' || c == 'b')
cout << "You typed a or b" << endl;
else
cout << "You typed " << x << endl;
}
It compiles and runs. There are two issues when I try to run it.
1) It seems to loop through two times for every entry. If I insert cin.ignore() just before the closing bracket, it seems to work better.
2) the variable c does not seem to have the character I entered when examined in the if statement.
What is happening with the variable c inside the while loop scope?
Does c actually get initialized?
When a declare a string e.g.
Code:
char str[30]; as a global variable, the srting is initialized to NULL.
But if I declare char str1[30] INSIDE main(), then the string has garbage inside.... Why this happens??
E.g. the following code snippet...
Code:
#include <stdio.h>
char str[50];
int main(){
char str1[50];
[Code] ....
I wrote the following program to initialize a string after the variables is declared, but it isn't working. A warning is given by the compiler, and the execution of the program shows a strange string. How do I initialize variable word in a separate statement from its declaration?
Code:
#include <stdio.h>
int main( void )
{
char word[20];
*word = "Hello";
printf( "The string is %s.
", word );
return 0;
}
I'm having to do a little c++ (coming from java) and don't understand the syntax of the following declaration
Code:
SensorBase* const sensor(mSensors[i]);
It looks like it's declaring a const pointer to a SensorBase object but I don't understand how that applies to sensor(mSensors[i]) which looks like a function??
I am getting a compilation error from the code below. It is when i am naming a variable with my user defined type.
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class person {
[Code] .....
C:Dev-CppTRIAL.PASS.!!!.cpp In function `int main()':
66 C:Dev-CppTRIAL.PASS.!!!.cpp expected primary-expression before "p"
66 C:Dev-CppTRIAL.PASS.!!!.cpp expected `;' before "p"
74 C:Dev-CppTRIAL.PASS.!!!.cpp `p' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
83 C:Dev-CppTRIAL.PASS.!!!.cpp `X' undeclared (first use this function)
I was exploring static variable by writing code snippets. I tried below code and it ended up throwing error saying "error: storage class specified for parameter 'b'"
Why static cannot be used in func() ?
Code:
int main() {
int a;
a=5;
func(a);
printf("%d",a);
return 0; }
void func(static int b)
{b=6;
printf("%d",b); }
can i have 1 static variable with instance different value?
View 3 Replies View RelatedI need to keep a static variable in a member function of a class that I have many objects of. I've had some trouble with it, and when I read up I found that such variables are static across all instances. Is there any way around this?
View 3 Replies View RelatedIn a function, I have a static variable that I want to assign the time in seconds when a certain condition is met and keep that value until a different condition is met. The time value is a struct. Since now->sec is always incrementing, will timeWhenEventMadeActive below hold onto the initial value or will it increment every time the function is called? I cant seem to test this.
static time_t timeWhenEventMadeActive = 0;
static bool initTime = 0;
if (!initTime) {
timeWhenEventMadeActive = now->sec; //holds uptime value in seconds
[Code] .....
What is the problem with the following code is? It compiles with Visual C++ 2012 but does not with g++:
//a.h
#ifndef Loaded
#define Loaded
using namespace std;
class MyClass{
public:
static const int MyStaticValue = 200;
[Code] ....
If I try to compile this using the command
g++ a.cpp b.cpp
I get an "undefined reference to 'MyClass::MyStaticValue'" error for the line "A = MyClass::MyStaticValue;" in main(). The strange thing is that if I change the line to "A = (int) MyClass::MyStaticValue;" it works fine and the output is
200
200
as expected.
The code also compiles under g++ if I move the defintion of MyStaticValue from a.h to a.cpp by const int MyClass::MyStaticValue = 200;
I've got a static variable in the master file called :
static dtNavMesh *g_navMesh;
I just need one copy of this object.
In another module, I need to reference this global variable.
extern dtNavMesh *g_navMesh;
I try to create small project in order to better understand how key word static works with templates . However some compiles errors crush my plan.
1>------ Build started: Project: 4.2b - Ex 1. Static Variable for Array Def Size. Templates, Configuration: Release Win32 ------
1> main.cpp
1>c:all myс++ha level 6solution level 6solution level 64.2b - ex1. static variable for array def size. templatesarray.cpp(40): error C2724: 'Array<Type>:efaultSize' : 'static' should not be used on member functions defined at file scope
[Code] .....
I have a function like this
void foo( int i) {
...
uint8_t buf[ i];
...
}
And I don't understand why the compiler is not complaining... I'm using g++ -c -g -Wall to compile ....
Say you had:
class Foo{
public:
//...
void funky();
[Code] .....
Would each instance of Foo create a new counter variable, or would it remain the same for all of them, i.e. baz.funky() would always use the same counter variable? What if the class was a template?
What does the order of console output from your program tell you about when the static object is initialized?
#include <iostream>
using namespace std;
//class
class Firstclass {
private:
Firstclass(); //constructor
~Firstclass(); //destructor
[Code] ....
Doesn't it allocate the class static variable to the heap, thus executing its algorithm then destroying it when the program ends - or. What exactly does it tell me? When the static variable is initialized, it takes place first before any of my other functions?
A have two classes, one inheriting the other, and the parent class being abstract (I plan on adding more child classes in the future). For reasons I won't bother mentioning, I'm making use of an STL container as a way for me to access all of the child objects in the heap. I've done so by making use of a map, with key type int and value type being a pointer to the parent class:
//PARENT.H
class Parent {
protected:
static int n;
static std::map<int, Parent*> map;
public:
virtual void pureVirtual() = 0;
[code]....
The Problem:In line 5 of Parent.cpp, initializing the value of the element to new Child won't work, as according to the compiler, the Child class hasn't been declared yet, and including Child.h into the Parent.h only opens an even bigger can of worms.I also can't initialize it as new Parent, seeing as the parent class is an abstract one.
The Question:Is there a way I can initialize the static map properly. Making the Parent class abstract is not an option.
I want to know
prog1.c
#include<stdio.c>
static int c=6;
int main() {
/*code*/
}
prog2.c
#include<stdio.h>
int main(){
static int c=10;
}
what would be the difference between these two program in the fuctioning of static keyword ?