Click to See Complete Forum and Search --> : extern confusing problem


Wolvorine
May 9th, 2007, 10:03 AM
hi,
i have class A ,B and D.

A class header file has a variable with static scope. Inside C and B the same name variable is defined with extern scope. When i run these objects inside the main function it doesnt change the data of that variable accept class D.

here are the class A.h and .cpp



#pragma once

static int iTheBad;

class A
{
public:
A(void);
~A(void);
};


A::A(void)
{
iTheBad=19;
}

A::~A(void)
{
}




here its class B


#include "A.h"
extern int iTheBad;
#pragma once


class B
{
public:
B(void);
~B(void);
};



B::B(void)
{

iTheBad=32;
}

B::~B(void)
{
}




here is the D class



#include "A.h"
extern int iTheBad;

#pragma once

class D
{
public:
D(void);
~D(void);
};



D::D(void)
{
iTheBad=1132;
}

D::~D(void)
{
}





here is the code, only D class changes the variable while B and A dont change anything ..... Thats really confusing why?????




#include "stdafx.h"
#include "A.h"
#include "D.h"
#include "B.h"


int _tmain(int argc, _TCHAR* argv[])
{
extern int iTheBad;

// iTheBad=0;

A Obj1;
D Obj3; //it works perfectly and change the variable to 1132
B Obj2;


return 0;
}

JohnW@Wessex
May 9th, 2007, 11:15 AM
Have you checked to see which order the constructors of A, B & D are called? The global variable will get set to the last one to be constructed.
Try moving the variable to the main code and declaring it extern in the headers.

AlbertGM
May 9th, 2007, 11:18 AM
Hi,
If I'm not wrong you cannot declare a variable both static and extern. Because an static variable is only accessible inside the function where is declared, although is not destroyed until the end of the process. But I'm not 100% sure.

Albert.

Wolvorine
May 9th, 2007, 11:47 AM
Have you checked to see which order the constructors of A, B & D are called? The global variable will get set to the last one to be constructed.
Try moving the variable to the main code and declaring it extern in the headers.

Yes I have checked the A B and D constructors call , even i moved them several times. What i found is If i remove the delcaration of extern variable from the D then B starts working and if i remove the B then A starts working. Its kinda veryyyy much confusing problem still i dont understand why.... Also what i tried is i have clean extern variable from the D , then inside the constrictor i have change the external variables data as 99, which i can see inside my watcher inside every class same but if i assign that extern variable in any class's variable then i dont see the same number unless its scope gets end. really confusing!!!!!


Hi,
If I'm not wrong you cannot declare a variable both static and extern. Because an static variable is only accessible inside the function where is declared, although is not destroyed until the end of the process. But I'm not 100% sure.Albert..

unless you dont define the static you cant use the extern variable anywhere.

Guys help me out.....(try your self also maybe its a bug or maybe we dont know the correct way how to use that)

Paul McKenzie
May 9th, 2007, 12:04 PM
unless you dont define the static you cant use the extern variable anywhere. Where did you get this information? This is not correct.

One and only one source file defines the variable. Then all other source files must use extern. This has been the case since the beginning of the 'C' language.
Guys help me out.....(try your self also maybe its a bug or maybe we dont know the correct way how to use that)It isn't a bug. It's your misunderstanding of how to declare global variables properly.

First, a variable defined in a source unit as "static" means that the variable only has scope within that source unit. So if you place in a header "static whatever x", wherever you include that header, you have different versions of "x", not the same version of "x".

If you want to use a global variable, then as I stated, only one compilation unit must declare it:

// foo.cpp
int x;

int main()
{
}

Then any other module that needs to use the "x" from foo.cpp must declare x as "extern".

// foo2.cpp
extern int x;

int foo()
{
}

Then you compile and link foo.cpp and foo2.cpp. There is now one and only one "x" -- the one declared in foo.cpp, and the one in foo2.cpp is the same one as the one in foo.cpp. What you were doing with "static" is totally going in the opposite direction of all of this, where you are declaring who-knows-how-many different iTheBad variables.

Regards,

Paul McKenzie

Wolvorine
May 10th, 2007, 03:09 AM
hi Paul McKenzie, thanks for the detail inmformation your right but if i want to declare that variable inside any header file then i cant use anywhere unless i dont define static you can check that as well. But my problem is solved when i put the variable declaration inside the CPP which is also fine for me...anyhow thanks for your reply to solve it

Thanks