|
-
July 3rd, 2007, 01:06 PM
#1
One/class not global members.
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.
-
July 3rd, 2007, 01:35 PM
#2
Re: One/class not global members.
One solution would be to make "myinputobject" a singleton. See this link...
http://www.codeguru.com/forum/showthread.php?t=344782
-
July 3rd, 2007, 07:29 PM
#3
Re: One/class not global members.
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?
-
July 4th, 2007, 06:06 AM
#4
Re: One/class not global members.
 Originally Posted by Frio_Lazzlo
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:
Code:
for(int x = 0; x < 10; x++)
{
a* myobject = new a;
myobject->dosomething();
delete myobject;
}
Albert.
Please, correct me. I'm just learning.... and sorry for my english :-)
-
July 4th, 2007, 06:44 AM
#5
Re: One/class not global members.
 Originally Posted by AlbertGM
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....
Code:
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..
}
}
-
July 4th, 2007, 11:51 AM
#6
Re: One/class not global members.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 4th, 2007, 01:57 PM
#7
Re: One/class not global members.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|