Re: accessing variables from more than one .cpp file
From: John Harrison (john_andronicus_at_hotmail.com)
Date: 10/09/04
- Next message: Victor Bazarov: "Re: accessing variables from more than one .cpp file"
- Previous message: scott: "accessing variables from more than one .cpp file"
- In reply to: scott: "accessing variables from more than one .cpp file"
- Next in thread: Victor Bazarov: "Re: accessing variables from more than one .cpp file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 8 Oct 2004 23:29:44 +0100
"scott" <scottamillard@hotmail.com> wrote in message
news:ck73qu$s6s$1@news5.svr.pol.co.uk...
> hi, sorry if this has been adressed some where elss. if it has plz can you
> point me to it and ill go read it.
>
> any way, i want to declare some variables with in a header file. lets say
> int i;
>
> i then want to be able to acess that variable from multipul .cpp files
> where
> I can read it and also change the value. i would like to hold it in a
> struct or a class eventualy but i can't even get it to work on its own at
> min, so i thought id start off simple and ask how i do it just like what i
> described above.
>
> any help would be grate,
>
> thx scott
>
Put this in the header file
extern int i;
Put this in *one* .cpp file
int i;
Include the header file in every .cpp file where you need to use it.
What goes in the header file is called a declaration, what goes in the one
.cpp file is called a definition. You can have as many declarations as you
like but you must have only have one definition. This is the One Definition
Rule. Understanding the difference between declarations and definitions is
an important C++ concept to master.
BTW i is a fantastically bad name for a global variable.
john
- Next message: Victor Bazarov: "Re: accessing variables from more than one .cpp file"
- Previous message: scott: "accessing variables from more than one .cpp file"
- In reply to: scott: "accessing variables from more than one .cpp file"
- Next in thread: Victor Bazarov: "Re: accessing variables from more than one .cpp file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|