#### Archived

This topic is now archived and is closed to further replies.

# declaring static objects, will work in other subs??

This topic is 5218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm curious how i can get this to work for me. I am using directx, and i initialise the d3d etc, so then after thats done i can start using the d3d features. this is how ive layed my program out:
void loop()
{
//use static objects here

}

void init()
{
//i declare my static objects in here...

}

void WinMain()
{
initD3D();
}

Say i want to create a text type with d3dFont, eg: static FontClass myFont; if i did that inside the init class, is there a way i could use myFont in the loop class? Thanks, [edited by - johnnyBravo on November 8, 2003 11:26:42 PM]

##### Share on other sites
Look into Singleton classes, they do exactly what you want.

They''re a single instance of an object, that can be accessed globablly throughout the program.

Very handy.

##### Share on other sites
no, static objects in functions are not in scope anywhere except in that function ... global variable and static non-function variables (also called file variables) are accessable in multiple functions ... as are static class member variables.

but, the need to have variables accessed in more than one function, but not anywhere else is the primary goal behind creating classes in the first place, so consider that an option ... or AT LEAST consider simply using global variabel which reside in a namespace along with your functions ... to avoid name collisions.

##### Share on other sites
The problem with singletons is that i can only have one, as i might want to declare an array etc.

The idea behind what i want to do is...

Say for a font class, which is for basically drawing text to the screen.
The font class would be for two thigns, the first is to define the values, eg font, size, underlined , etc
The second bit would be to draw it at a certain location on the screen.

So to declare it i would.. for example
Font normalFont("Arial",12,false);

and to draw it
drawText(normalFont,"hello",20,20);

This is fine and all but theres one little thing.
Before i can call the font class i need to have directx 3d initialised.

And to initialise d3d i need to do it in the winmain, but to declare the font class global i need to do it at the top before d3d has been initialsed,

So any ideas on this?