C++ :: Difference Between Static Local Variable And Static Global Variable?

Aug 5, 2013

Here 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?

View 6 Replies


ADVERTISEMENT

C/C++ :: Difference Between Declaring Static Variable Inside And Outside Of Main

Apr 6, 2012

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 ?

View 1 Replies View Related

Visual C++ :: Static Variables Local Or Global?

Mar 10, 2014

I came across the following code today and I was a bit surprised that it worked:-

Code:
std::string func_A () {
static std::string x;
if (!x.empty())
return x;

[Code] ....

I've simplified things slightly - but the basic point is that both functions are in the same source file and they both have a static std::string called 'x'. Being static, I guess they aren't (strictly) local variables. So how does the compiler know that they're different entities? Does it encode their signatures using the function name or something like that? If I call each function separately I do seem to get the correct string...

View 5 Replies View Related

C :: Transform Local Variable Into Global Variable?

Oct 25, 2014

I need to transform a local variable into a global variable so I can use it in one of my functions. I thought about passing the value as a parameter to this function but I can do this since the function is called inside the while loop and this variable counts how many times the while loop does (so the final value is outside the loop). Example to visualize better:

Code:
while(condition) {
function(parameter1, parameter2);
count = count + 1;
}
printf("%d
", count);

So, I need to transform the final value of "count" into a global variable. Can I do this?

View 5 Replies View Related

C/C++ :: Create Local Instances Of A Global Variable?

Oct 4, 2012

is it possible to have a global variable pointing to a different address depending on the thread?

Imagine one would like to use threads with the loop:

for (i=0;i<n;i++){
globalPointerVariable=getAddress(i);
DoThingsUsingThe_globalPointerVariable();
}

View 4 Replies View Related

C :: String Declaration As Global / Main Local Variable

Nov 14, 2013

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] ....

View 4 Replies View Related

C :: Static Variable Usage

Dec 27, 2013

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); }

View 7 Replies View Related

C++ :: Have 1 Static Variable With Instance Different Value

Sep 11, 2014

can i have 1 static variable with instance different value?

View 3 Replies View Related

C++ :: Static Variable Declaration

Oct 4, 2014

If 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 Replies View Related

C++ :: Static Variable In Member Function

Aug 27, 2014

I 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 Related

C++ :: Static Variable To Time Struct

Jan 21, 2013

In 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] .....

View 1 Replies View Related

C++ :: Static Constant Member Variable

Jun 6, 2013

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;

View 5 Replies View Related

C++ :: How To Reference Static Variable In Another File

Apr 15, 2014

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;

View 5 Replies View Related

C++ :: Static Variable And Function With Template

Jul 18, 2012

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] .....

View 7 Replies View Related

C++ :: Static Array Variable Size Allowed?

Mar 1, 2013

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 ....

View 2 Replies View Related

C++ :: Static Variable Inside A Member Function

Jul 20, 2013

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?

View 3 Replies View Related

C++ :: Static Variable Initialization - Console Output Of Program

Oct 11, 2013

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?

View 7 Replies View Related

C++ :: Initializing Static Map Of Variable Type Abstract Class?

Dec 3, 2014

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.

View 3 Replies View Related

Visual C++ :: Hash Table - How To Setup Static Variable

Apr 22, 2014

I am having trouble getting the cout statements that I commented out to work properly. And I can't figure out why the first movie in the movies.txt file displays at the end of the list in my output screen when it should only display once as the first item. I am also having trouble figuring out where and how to set up the static variable.

Purpose : This assignment requires that you to develop a program using the classes listed above. Specifically you will build a hash table containing movie data. The collision strategy will be to build linked lists as the array elements.

Program Specifications : Your program will read the data from the file named Movies.txt located in the StudentFiles1.zip file on the Connections Portal. Each record contains 2 fields separated by a space. They are:

FieldsData Type
Motion Picture Association Code (MPAC)Integer
Movie NameString

Your application program will read each record, create a Movie structure instance and place that structure instance into the hash table.

You will need to modify the appropriate code to provide for an audit trail of the hash table construction. To accomplish this, you will use cout statements in the above class member functions. You will need to modify them to include couts, but the modifications will be relatively small. If not, you are doing it wrong. The hashing algorithm to use is:

Index = int ( 0.618033 * MPAC) % size

Make the array size a constant and set it to 10.

Include a counter for the number of collisions that occurred in building the hash table. Including a static variable in the LinkedList or List class is the best method for doing that.

See the sample audit trail below for the first 5 records on Movies.txt and the size of the hash table set to 3. This is shown for illustration only. The full file will have different locations calculated.

1101-Casablanca is being added
The hashed location is 2
There was no collision loading 1101-Casablanca
------------------------------------------------
1200-Duck Soup is being added
The hashed location is 0
There was no collision loading 1200-Duck Soup
------------------------------------------------

[Code] ....

After the above is displayed, prompt the user to enter the MPAC of a movie to locate. Produce an audit trail when locating the requested movie as shown below: Make sure you list all movies that have collided at the hashed location. That may also cause you to modify one of the existing ADTs a little.

Will search for 6601
at the hashed location is 2
There was a collision here with 6601-Wizard of Oz
There was a collision here with 1101-Casablanca
retrieved from hash table: 6601-Wizard of Oz

[Code] ....

View 10 Replies View Related

C++ ::  Storing Static Class Members Of Dynamic Variable Type In DLL

Oct 15, 2013

How I can implement it.

Tickable.h

#include <list>
#ifdef TICKABLE_EXPORTS //Automatically defined by MSVS
#define DLL __declspec(dllexport)
#else
#define DLL __declspec(dllimport)
#pragma comment(lib, "Tickable.lib")
#endif

class DLL Tickable{

[Code] ....

error LNK2001:
unresolved external symbol "private: static class std::list<class Tickable*,SKIPPED BITS> Tickable::subs" HUGE_SYMBOL_LIST
PATHTickable.obj

I know with such a tiny and insignificant class the dll export seems pointless but this class is actually intended to be a .lib ONLY. But it is derived from by .dll style classes, and through inheritance this error is the exact same as what appears in the derived class, I just imagine that the cut down version would be easier to work with.

Is it possible to hold either a static variable in a dll which is of a dynamic type, OR would it be possible to reference an external variable which is static throughout the instances and this variable can be chucked away in a namespace of mine somewhere?

I suppose my only other option (if this is possible) would be to define a maximum instance number and create a standard array of pointers but this could both waste so much memory when not in use and cause problems if I need more memory.

View 5 Replies View Related

C++ :: Undefined Reference Error When Accessing Static Variable Inside Member Function

Feb 10, 2013

I am modifying a set of static variables inside of the class's member function. The static variables are private. An example of what I'm doing is as below,

utilities.h
-----------
class utilities {
private:
static int num_nodes;

public:
void parse_details(char* );

[Code] ....

I get a compilation error in the function void utilities::parse_details(char* filename)

which says: undefined reference to `utilities::num_nodes'

compiler: g++

View 2 Replies View Related

C++ :: Using Global Variables In Static Library

Jul 22, 2014

Is it possible to use & change global variables in a Static Library? For example:

I declare a
bool test = true;
globally.

Then later in an exported function If the user wants, he can set that test to false. So the program later when checks test if it's true, will notice that it's not true, since one of my function changed it.

Is it right?

View 3 Replies View Related

C++ :: Static Member Allocation - Global Object Creation

Jan 24, 2014

I see many time where static data member is used to count creations of objects -

i.e.

1. the static data member is init to 0

2. the static data member is incremented by 1, in the Class' constructor, every time an object is created

However, if you define a global object of a class,

How can you tell that the static data member is initialized BEFORE the constructor of the global object is called? (i.e. before the global object is created).

Because to my understanding, you do not know in advance the order of global objects' creation -

so the Global Object could be created BEFORE the static data member was created and initialized.

View 14 Replies View Related

C/C++ :: Difference Between Static And Dynamic Array?

Mar 15, 2014

what is main difference between the static array and a dynamic array....also i am confused with dynamic array and dynamic allocated array?

View 3 Replies View Related

C++ :: Difference Between Static And Dynamic Cast?

Oct 10, 2013

Is there a difference between having static and dynamic cast in this scenario? The output is the same.

Code:
Base* pb = new Derived();
if(Derived* pd2 = static_cast<Derived*>(pb)) // true {
pd2->get_price(); // calls Base::get_price()
pd2->get_rate(); // calls Derived::get_rate()

[Code] ....

View 7 Replies View Related

C++ :: Do Static Functions Have Access To Non Static Data Members Of A Class

Apr 17, 2013

From my book:

"A static function might have this prototype:

static void Afunction(int n);

A static function can be called in relation to a particular object by a statement such as the following:

aBox.Afunction(10);

The function has no access to the non-static members of aBox. The same function could also be called without reference to an object. In this case, the statement would be:

CBox::Afunction(10);

where CBox is the class name. Using the class name and the scope resolution operator tells the compiler to which class Afunction() belongs."

Why exactly cant Afunction access non-static members?

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved