Extern variable

A

aki

Hi i want to use a variable , who value should retain within function
calls.

File 1
..........

static int flag=0

func1()
{
func2();
if (flag)

{
do somthing....
}

}



File 2
...........

extern int flag;

func2()


{

flag=1;
}



Is this fine and work ????
 
A

aki

Hi i want to use a variable , who value should retain within function
calls.

File 1
.........

static int flag=0

func1()
{
func2();
if (flag)

{
do somthing....
}

}

File 2
..........

extern int flag;

func2()

{

flag=1;

}

Is this fine and work ????

Hiii All..


one more thing i doubt.
Should i include these declaratons and defination in .cc file or .h
file


Thanks
aki
 
I

Ian Collins

aki said:
one more thing i doubt.
Should i include these declaratons and defination in .cc file or .h
file
Put the declaration in a header and the definition in one and only one
source file.
 
A

aki

aki <[email protected]> kirjutas:









No, a static variable is seen only in one compilation unit. What you want
is:

// in a header file included by both cpp-s:
extern int flag;

// in one cpp file:
int flag = 0;

Besides, why int and not bool?

hth
Paavo



hello All ,

i tried like this ..but compilation is failing..;(

one.h
-------
extern bool flag ;


one.cc
-------
bool flag=0; // Should i place this inside fun1() ???

func1()
{
func2();
if (flag)

{
do somthing....
}

}


two.cc
---------
#include<one.h>

fun2()
{
bool=1;
}


compilatuon error //
Undefined first referenced
symbol in file
flag src/bssim/entitycom/
EntitySblCom.o
(here file name "EntitySblCom" refers to "two" )
 
A

Anonymous Infidel - Aborted Islam with a hanger

Best advice is to not use global variables at all.
This advice is however difficult to follow, because even if you don't use
global variables directly, you can easily emulate the same via other means.
Like having a billion pointers, spread out over a billion classes, all
pointing to the same data, right?
 
R

Rajib

aki said:
hello All ,

i tried like this ..but compilation is failing..;(

one.h
-------
extern bool flag ;


one.cc
-------
bool flag=0; // Should i place this inside fun1() ???

func1()
{
func2();
if (flag)

{
do somthing....
}

}


two.cc
---------
#include<one.h>

fun2()
{
bool=1;

Don't you mean flag=1;
or flag=true;
}


compilatuon error //
Undefined first referenced
symbol in file
flag src/bssim/entitycom/
EntitySblCom.o
(here file name "EntitySblCom" refers to "two" )

Looks like you're not linking the files.
You need to compile the two files separately, and then link them together.

On gcc the simple way to do it would look like:
g++ file1.cpp file2.cpp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top