469,609 Members | 1,228 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Calling a variable ?

How does one call a variable defined in a class defined in file1.cpp
from another file file2.cpp. Or can one call a class from a different
file ?

(Besides the standard way to define the variable in a header file and
include this header file where required)
Jul 22 '05 #1
2 1261
Ian
mark wrote:
How does one call a variable defined in a class defined in file1.cpp
from another file file2.cpp. Or can one call a class from a different
file ?
Call a variable?
(Besides the standard way to define the variable in a header file and
include this header file where required)


Why would you want to do it another way?

You can provide a prototype in the compilation unit you want to make the
call from but this is tedious and a maintenance headache.

Ian
Jul 22 '05 #2
"mark" <ma*********@yahoo.com> wrote in message
How does one call a variable defined in a class defined in file1.cpp
from another file file2.cpp. Or can one call a class from a different
file ?
In file1.h declare the variable as extern. In file2.cpp include file1.h.

// file1.h
#if !defined(include_file1)
#define include_file1
extern int variable;
#endif

// file1.cpp
#include "file1.h"
int variable = 0;

// file2.cpp
#include "file2.h"
#include "file1.h" // variable
void f() {
++variable;
}

(Besides the standard way to define the variable in a header file and
include this header file where required)


Is what I describe the standard? Why do you not want to do this?
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Andrew Wilkinson | last post: by
8 posts views Thread by Muthu | last post: by
30 posts views Thread by Tim Marshall | last post: by
17 posts views Thread by Bill Grigg | last post: by
5 posts views Thread by huzz | last post: by
4 posts views Thread by ST | last post: by
13 posts views Thread by santosh | last post: by
reply views Thread by Przemysław Bana¶ | last post: by
8 posts views Thread by Jeff | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.