CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    5

    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.

  2. #2
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    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

  3. #3
    Join Date
    Jun 2007
    Posts
    105

    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?

  4. #4
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: One/class not global members.

    Quote 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 :-)

  5. #5
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: One/class not global members.

    Quote 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..
    }
    }

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  7. #7
    Join Date
    Jul 2007
    Posts
    5

    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
  •  





Click Here to Expand Forum to Full Width

Featured