Some simple organization questions.
-
- Posts: 41
- Joined: Thu Sep 28, 2006 4:34 am
- Location: Eaton, Colorado, United States
Some simple organization questions.
So I'm still pretty new to C++ and have a few questions.
I don't like how all the irrlicht examples are organized. I prefer to have my projects broken up into different functions and files. When trying to do this to some irrlicht examples I have problems when trying to pass variables into functions.
My question:
Would it be bad to use a lot of global variables? At the moment I only have two globals. Would it be bad if I made the majority of my irrlicht related things declared globally?
Thanks.
EDIT: Also, how do I display a number as a string? Do I have to convert it to a string?
I don't like how all the irrlicht examples are organized. I prefer to have my projects broken up into different functions and files. When trying to do this to some irrlicht examples I have problems when trying to pass variables into functions.
My question:
Would it be bad to use a lot of global variables? At the moment I only have two globals. Would it be bad if I made the majority of my irrlicht related things declared globally?
Thanks.
EDIT: Also, how do I display a number as a string? Do I have to convert it to a string?
What problems do you have passing variables to functions?
It is generally considered bad practice to have a lot of global variables. There are many memory problems involved. In the end, it depends on your organisation of the program - sometimes, you cannot avoid a few global variables. In C++, I think it is common to use data structures to store information and pass a reference to that.
It is generally considered bad practice to have a lot of global variables. There are many memory problems involved. In the end, it depends on your organisation of the program - sometimes, you cannot avoid a few global variables. In C++, I think it is common to use data structures to store information and pass a reference to that.
-
- Posts: 41
- Joined: Thu Sep 28, 2006 4:34 am
- Location: Eaton, Colorado, United States
One approach would be to create pointers to all of the objects created in "main" in the "MyEventReceiver" class.twentytortures wrote:It's kind of hard for me to describe. Do you know of any examples that shows something like example 4 organized into separate functions?
That way when you create a "MyEventReceiver" object you can reference all of your different Irrlicht objects by just passing around the "MyEventReceiver" object (pointer).
Whether this is good design or not is up for speulation I guess. If you're not catching my drift I can show you an example.
Not really... for examples I generally prefer everything to be in one function as it is easier to follow. Do you have problems passing Irrlicht objects?
Or ...
I hope that helps you out... it is hard to give specific examples without knowing what the problem is.
Code: Select all
IrrlichtDevice *device = createDevice(EDT_DIRECT3D9, dimension2d<s32>(640, 480), 16, false, false, false, &receiver);
IVideoDriver *videoDriver = device -> getVideoDriver();
ISceneManager *sceneManager = device -> getSceneManager();
DoSomething(device, videoDriver, sceneManager);
-
void DoSomething(IrrlichtDevice *device, IVideoDriver *videoDriver, ISceneManager *sceneManager) {
...
}
Code: Select all
vector3df veloc = ...;
matrix4 position = ...;
Create(veloc, position);
-
void Create(vector3df velocity, matrix4 position) {
...
}
-
- Posts: 41
- Joined: Thu Sep 28, 2006 4:34 am
- Location: Eaton, Colorado, United States
I usually like to pass data into variables by using a '&' pointer like this:
Rather than do it this way:
But irrlicht objects are declared using the '*' pointer, does this still work the same way? I.e. would this function work the same as the one above?:
I can post a more specific example when I have some more free time.
Code: Select all
void someFunction(int &a)
{
a = 2;
}
Code: Select all
int someFunction()
{
int a;
a = 2;
return a;
}
Code: Select all
void someFunction(int *a)
{
a = 2;
}
yes it works this way, but you have to do it this way:
otherwise you set the pointer (int*) to point to the address 2 and not set the value to 2 !!!
Code: Select all
void someFunction(int *a)
{
&a = 2;
}
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
i always use this and it works perfect
Code: Select all
void someFunction(int *a)
{
a = 2;
}
-
- Posts: 19
- Joined: Wed Jan 25, 2006 7:44 pm
Code: Select all
void someFunction(int *a)
{
*a = 2;
}
-
- Posts: 19
- Joined: Wed Jan 25, 2006 7:44 pm