Passing values between .cpp files
What is a good way of passing values between .cpp files?
You can obviously pass values to procedures as parameters so that you can use them, but what if you have some values that you want to keep stored in another .cpp file because they belong there. A good example would be that you have a .cpp solely for physics and you want to have an acceleration speed that you can update whenever you want by calling the ChangeAcceleration procedure in another .cpp file. You might want all of the procedures in your physics.cpp file to be able to get access to that acceleration value whenever they need, and it looks to me like you need to have these on-going values (not temporary variables) defined outside the procedures in physics.cpp like this:
void DoSomething(blah blah....)
{
Something...
}
float accel;
void ComputeSpeed(blah....)
{
Some Speed = accel * something else;
}
I thought that when you did this though, the variable became global (global in physics .cpp) and therefore took up memory because it is being stored all of the time, instead of just during calls to physics.cpp functions. Is that correct, and is that bad?
Another example:
In Main program (main.cpp) -
SetScreenSize(1024,768);
In render.cpp -
int screenwidth;
int screenheight;
void SetScreenSize(int width, int height);
{
screenwidth = width;
screenheight = height;
}
void DrawOnScreen(blah.....)
{
screen resolution x = screenwidth * 4;
screen resolution y = screenheight * 666;
DrawShape(screen resolution x, screen resolution y, shape, colour);
}
Is this a good way of passing values between .cpp files, or is there another/better way?
Paulcoz.
Would it be even worse to have screenheight defined in render.cpp and do this in main.cpp:
extern int screenheight;
screenheight = 768;
I would do something like this:
Disclaimer: This is what *I* would do (this is aimed at coding style critics), not may be popular.
That gave me an idea to add to my signature:
Fine print on bottom of students homework: "This work is not fit for any particular perpous, including, but not limited to, handing-in, and does not imply any application of brain-power on the part of the student. The student is not responsible for any grade that may be given to this work."
--------------------
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
// render.h fileextern int ScreenHeight;extern int ScreenWidth;void SetScreenSize(int, int);// render.cpp#include "render.h"int ScreenHeight = DefaultScreenHeight;int ScreenWidth = DefaultScreenWidth;void SetScreenSize(int w, int h){ ScreenHeight = h; ScreenWidth = w;}// main.cpp#include "render.h"void main(){ SetScreenSize(1024, 768);}
Disclaimer: This is what *I* would do (this is aimed at coding style critics), not may be popular.
That gave me an idea to add to my signature:
Fine print on bottom of students homework: "This work is not fit for any particular perpous, including, but not limited to, handing-in, and does not imply any application of brain-power on the part of the student. The student is not responsible for any grade that may be given to this work."
--------------------
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
Visit the ROAD Programming Website for more programming help.
--------------------
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
Yanroy,
extern int ScreenHeight;
extern int ScreenWidth;
I thought these lines made ScreenHeight and ScreenWidth in render.cpp available to main.cpp? eg. you could type ScreenWidth = 1024 in main.cpp (not using the SetScreenSize procedure at all)?
Say we wanted to pass those values back to main.cpp, would it be sensible to use the lines above (which means those variables are common to both main.cpp and render.cpp), or would you have a function in main.cpp that you could call on from render.cpp which set the global main.cpp variables accordingly?
Which is the most common/best way?
I am concerned about the way I am doing this (passing values) because I seem to be getting more and more extern statements in my program (about fifteen or more in my direct input .cpp linking to main.cpp for things like mouseclick, mousehold etc.. - I even pass a whole dims structure!!!) and I think I am going about this the wrong way.
Paulcoz.
Edited by - paulcoz on November 15, 2000 1:35:24 AM
extern int ScreenHeight;
extern int ScreenWidth;
I thought these lines made ScreenHeight and ScreenWidth in render.cpp available to main.cpp? eg. you could type ScreenWidth = 1024 in main.cpp (not using the SetScreenSize procedure at all)?
Say we wanted to pass those values back to main.cpp, would it be sensible to use the lines above (which means those variables are common to both main.cpp and render.cpp), or would you have a function in main.cpp that you could call on from render.cpp which set the global main.cpp variables accordingly?
Which is the most common/best way?
I am concerned about the way I am doing this (passing values) because I seem to be getting more and more extern statements in my program (about fifteen or more in my direct input .cpp linking to main.cpp for things like mouseclick, mousehold etc.. - I even pass a whole dims structure!!!) and I think I am going about this the wrong way.
Paulcoz.
Edited by - paulcoz on November 15, 2000 1:35:24 AM
It sounds like you want to encapsulate in a class file?
you must wrap render into a class then make functions like setscreensize and drawonscreen member functions of the class. ScreenSize and ScreenWidth would be member variables. So in main you would just create an instance of the render class and use it to call your functions. look at www.bruceeckel.com and get his thinking in C++ books - free download to learn the syntax
you must wrap render into a class then make functions like setscreensize and drawonscreen member functions of the class. ScreenSize and ScreenWidth would be member variables. So in main you would just create an instance of the render class and use it to call your functions. look at www.bruceeckel.com and get his thinking in C++ books - free download to learn the syntax
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement