Click to See Complete Forum and Search --> : One/class not global members.


Frio_Lazzlo
July 3rd, 2007, 01:06 PM
Hello people.
Im battling with this few days now and i couldnt find anything about this topic on the internet, so i decided to register here. Im having problems with my console program and i would like some help : ).
(using dev-c++ if it matters)

Problem: i want to make a class member (a file input object) that is only initialized one time when the first object of this class is created.

Some background: at the beginning, the program is creating the files i want to open in my class later if its needed.
I have tryed making these objects static members of the class but the problem is that they are initialized before main() when the files i want to open dont exist..., making them a regular member will always open them and at huge file it would take unnecesery long time because if they are open they are used frequently.
<code>
//Main.cpp
#include "a.h" // where my class is

int main()
{
createmyfiles();
if(condition)
{
for(int x = 0; x < 10; x++)
{
a* myobject = new a;
a->dosomething();
delete a;
}
}
}

//in a.h
#ifndef _a_h_
#define _a_h_

#include "myinputclass.h"

class a
{
public:
//...
myinputobject my; // the member i am talking about
};
#endif</code>

Thanks in advance.

sszd
July 3rd, 2007, 01:35 PM
One solution would be to make "myinputobject" a singleton. See this link...
http://www.codeguru.com/forum/showthread.php?t=344782

ne0n82
July 3rd, 2007, 07:29 PM
im confused why not just put the file i/o into the constructor of your inputclass that way if they request a file and you call new it opens the file?

AlbertGM
July 4th, 2007, 06:06 AM
I have tryed making these objects static members of the class but the problem is that they are initialized before main() when the files i want to open dont exist...
As far as I know, static members of a class are initialized before the first instance and not before the main.

By the way, I think your code is wrong:
for(int x = 0; x < 10; x++)
{
a* myobject = new a;
myobject->dosomething();
delete myobject;
}
Albert.

code_carnage
July 4th, 2007, 06:44 AM
As far as I know, static members of a class are initialized before the first instance and not before the main.
]
Albert.

He is right static class variables will be initialized before main..

Here is the solution...As rightly said by somebody have use singelton..But following way...This was variable will be initialized when for first time CreateInstance is called....

class SingleInstance
{
private:
SingleInstance(T1 p1, T2 p2 )
{
}
SingleInstance()
{
}

SingleInstance(const SingleInstance&)
{
}

SingleInstance& operator=(const SingleInstance&)
{
}
public:
static SingleInstance& CreateInstance()
{
static SingleInstance neo(T1 p1, T2 p2); //Use any of the constructors..
return neo;
}

}



//In your code

class YourClass
{
private:
SingleInstance st;
public:
YourClass()
{
st = SingleInstance ::CreateInstance(); //use it as much as you want..
}
}

exterminator
July 4th, 2007, 11:51 AM
Here's some thoughts on singletons - http://www.codeguru.com/forum/showthread.php?t=423850

Frio_Lazzlo
July 4th, 2007, 01:57 PM
Thanks for the replies people :)
I was trying to figure out the singleton class and at one point myinputclass was initialized just like i wanted, the problems began when i wanted to use it :P
When i called a function from a object of myinputclass that returns a std::string and displayed the returned string the program freezed.
I will try the diferent examples of singleton.
Thanks for help.