Calling a variable ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mark

    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)
  • Ian

    #2
    Re: Calling a variable ?

    mark wrote:[color=blue]
    > 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 ?
    >[/color]
    Call a variable?
    [color=blue]
    > (Besides the standard way to define the variable in a header file and
    > include this header file where required)[/color]

    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

    Comment

    • Siemel Naran

      #3
      Re: Calling a variable ?

      "mark" <markstone23@ya hoo.com> wrote in message
      [color=blue]
      > 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 ?[/color]

      In file1.h declare the variable as extern. In file2.cpp include file1.h.

      // file1.h
      #if !defined(includ e_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;
      }

      [color=blue]
      > (Besides the standard way to define the variable in a header file and
      > include this header file where required)[/color]

      Is what I describe the standard? Why do you not want to do this?


      Comment

      Working...