CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2004
    Location
    Pakistan
    Posts
    260

    extern confusing problem

    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

    Code:
    #pragma once
    
    static int iTheBad;
    
    class A
    {
    public:
    	A(void);
    	~A(void);
    };
    
    
    A::A(void)
    {
    	iTheBad=19;
    }
    
    A::~A(void)
    {
    }

    here its class B

    Code:
    #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

    Code:
    #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?????


    Code:
    #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;
    }
    Last edited by Wolvorine; May 9th, 2007 at 10:31 AM.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: extern confusing problem

    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.

  3. #3
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: extern confusing problem

    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.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  4. #4
    Join Date
    Aug 2004
    Location
    Pakistan
    Posts
    260

    Re: extern confusing problem

    Quote Originally Posted by JohnW@Wessex
    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!!!!!


    Quote Originally Posted by AlbertGM
    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)

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: extern confusing problem

    Quote Originally Posted by Wolvorine
    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:
    Code:
    // foo.cpp
    int x;
    
    int  main()
    {
    }
    Then any other module that needs to use the "x" from foo.cpp must declare x as "extern".
    Code:
    // 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

  6. #6
    Join Date
    Aug 2004
    Location
    Pakistan
    Posts
    260

    Re: extern confusing problem

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured