Calling a variable ?

M

mark

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)
 
I

Ian

mark said:
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
 
S

Siemel Naran

mark said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,924
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top