CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  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.

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