1

From what I have gathered, the 'extern' keyword in c++ can be used to tell the compiler that a variable is defined in another .cpp file. I was wondering if this definition had to be explicit, or if the definition could be changed via side-effect by a function in the .cpp file where the variable is defined.

i.e.

//a.h
extern int foo;

//a.cpp
#include <a.h>

int foo=0;
int func(int &foo) // EDIT: oops, forgot the type for the parameter and return statement
{
foo = 10;
return 5;
}
int x = func(foo); // EDIT: changed to match declaration and assigned function to dummy variable

//b.cpp
#include <a.h>

int main()
{
cout << foo;
return 0;
}

Can the program recognize that foo should be 10, or will it be 0? And if the compiler recognizes foo as 0, is there a way I can make it so that it recognizes it as 10? Also, the reason I can't just compile and test this myself is that I'm not sure how to compile when there are multiple files, I'm new =).

EDIT: Thanks for the error pointers, but I guess the main question is still if b.cpp can see if foo is 10 or 0. cheers!

Roger Wang
  • 35
  • 2
  • 6
  • You have some weirdness going on with two main functions. You only need one, and that main function can call functions from b.cpp, even if it's in a.cpp. That's what the #includes do. Also, your func() doesn't need the &foo argument to work. I'll get to my computer and post an answer soon. Oh, and compiling them together depends on your IDE/compiler. What do you have? – BrainSteel Jul 02 '13 at 19:40
  • @Alexandru No it doesn't! @Roger What's the `&foo` in `int func(&foo) { ... }` supposed to mean? – Praetorian Jul 02 '13 at 19:42
  • @Praetorian my bad, was unatentive that those are different files. – Alexandru Barbarosie Jul 02 '13 at 19:46
  • @Praetorian it was just a way to represent a function/procedure that alters a variable by reference as well as returns something else. I believe it is called a side-effect. I want to know if another function could recognize a variable in the include file if it was not explicitly defined or changed. – Roger Wang Jul 02 '13 at 19:49
  • @BrainSteel g++ and gcc – Roger Wang Jul 02 '13 at 19:52
  • @RogerWang That's all good to know, but you're inventing your own grammar trying to achieve what you want. If you want to take a reference to an `int` and return an `int` the function signature should be `int func(int&)`, and the function needs a `return` statement. Please start reading a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Praetorian Jul 02 '13 at 19:54
  • @Praetorian right right, sorry about that. I was rushing to type up an example of what I was trying to do and made a lot of syntax errors. I guess the foo variable is the important one, defining x was just so func would be called. – Roger Wang Jul 02 '13 at 20:04

2 Answers2

2

If you correct all the syntax errors and build the whole project, in the file there will be a single integer named 'foo' and it can be accessed for read and write from both sources. Setting it to a value at any place will be read in any other.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
2

It should be 10.

Explanation:

First, the statement

int x=func(foo);

will be called before entering main function, and after

int foo=0;

Second, the foo in func will hide the global foo, so the foo in the func will apply only to the incoming parameter by reference.

Third, your code won't get compiled. Reason 1: you are not using a header from the system, so you need #include "header.h" instead of #include <header.h>. Reason 2: for cout, it is necessary to #include <iostream> and write std::cout, assuming you are not using outdated VC 6.0.

xis
  • 24,330
  • 9
  • 43
  • 59