2

In my code, due to efficiency consideration, I place a long function in it's own file (let's name it a.cpp). I have also created a second file named b.cpp which holds another function which uses the same variables names.

I have tried to create a header file for those variables but it didn't work. Is there a way to do that (apart from placing the functions in the same file)?

A simple example:

a.cpp

double s;

void a(){
  s = 1.0;
  printf("%f\n",s);
}

b.cpp

double s;

void b(){
  s = 2.0;
  printf("%f\n",s);
}

Note Each of those file is, in effect a c but the whole program is c++.

4
  • "Each of those file is, in effect a c but the whole program is c++. " What does that mean? Commented Aug 15, 2011 at 20:15
  • I don't use (or almost don't use) any feature of c++. The files holds a single, long function which fills too complicated to hold in a larger, class file. Come to think about this. I can split the class file into several files with a single header right? Commented Aug 15, 2011 at 20:16
  • A "single, long function" sounds wrong. Commented Aug 15, 2011 at 20:17
  • C++ is not C, it's a multi-paradigm language, and even if you don't do any object-oriented, it's still different from C at details (even though most of C compiles as C++, which is intentional, but not to be relied upon). Decide! Commented Aug 15, 2011 at 20:22

3 Answers 3

6

Write extern double s; in both (or in a header). This is a declaration without being a definition.

Then write double s; in just one .cpp file — this is where the double object will physically "live".

More here.

Sign up to request clarification or add additional context in comments.

Comments

0

Put double s; in a.cpp. Write extern s; in a.h.

Also good programming practice is a function should fit onto a screen/one side of a5.

10 Comments

Syntax is wrong. Also, good answering practice is to format your posts!
I agree about the fitting part but in this way I gain one less while loop.
@Yotam: And anyone coming to work on your code after you (including your future self!) gains a huge maintenance nightmare and weeks of migraines. Is one while loop really worth it?
@Tomalak Geret'kal I know that. This the double edge of scientific programming. I have to consider both efficiency and readability. In this case I have chosen efficiency...
@Yotam: Then you've probably chosen the wrong language, too. :)
|
-3

Put double s in a header file.

At the top of each .cpp file do:

#include "filename.h"

to introduce the variable into the cpp file for use. It would be good to define it as static as well... but we don't go into that.

PS: You shouldn't use globals like this if avoidable. It's not good OO design.

2 Comments

-1: No, this is wrong. This is no different from writing double s; directly in the .cpp files. #include is just a text find-and-replace operation, pre-compilation.
I have tried that and noted so (the header file part in my question)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.