staitc class

This is a discussion on staitc class within the C++ Programming forums, part of the General Programming Boards category; So here is the problem i want to create a class that would be something like this : Code: class ...

  1. #1
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377

    staitc class

    So here is the problem i want to create a class that would be something like this :
    Code:
    class something
    {
    private:
      static int i;
    
    public:
      static void operate()
      {
        // do something
      }
    }
    and there would be only one instance which you would acsses via :: operator. So there is only one of this class in whole project.
    Is there a better way to declare a class than write static in front of everything?

    what does
    Code:
    static class
    {
    ...
    }something;
    mean?

    I could make the constructor private, but i still need to create an instance of class...
    I just want to create a class that stands for it's own, and that you can't make others, is this possible ?
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Can you explain what a singleton class is ?
    And can you acsses it via :: ?
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Have you clicked at least one link?
    http://www.codeproject.com/gen/design/singleton.asp
    What is a Singleton Class?
    Sometimes, you may want to have only one object for a given class and this object should be easily accessible. For example, an Application should have only one Application object and the Job schedulers should share a single Job Queue object. A global variable may provide easy access, but using a global variable will not stop the user from creating more than one object and moreover it is discouraged in Object Oriented Programming. In the above example, the Application class should not allow the user to create more than one object of it and the Job Queue class should ensure that a single Job Queue object is shared by all the Job schedulers. Application and Job Queue classes are called Singleton classes. So, what is a Singleton class ? A class that assures a maximum of ONE object of its type at a given time and provides a global access point to this object is a Singleton class.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    You can declare your data members as static. No instances of your class need to be created.

    Read about static data members
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    This is one way to do it

    Code:
    class Single
    {
    public:
    	static Single &instance() 
    	{
    		static Single instance;
    
    		return instance;
    	}
    
    private:
    	Single() {}
    	Single(const Single &);
    	Single &operator=(const Single &);
    };

  7. #7
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Quote Originally Posted by Laserve
    This is one way to do it

    Code:
    class Single
    {
    public:
    	static Single &instance() 
    	{
    		static Single instance;
    
    		return instance;
    	}
    
    private:
    	Single() {}
    	Single(const Single &);
    	Single &operator=(const Single &);
    };
    so how do you acsses it ?
    You still have to create and instance of the class in every *.cpp file your using it in...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  8. #8
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    I'm wonderng if i could create a class that behaves just like the one i wrote earlyer...
    It's bothersome to write static in front of every data member and function, and you can't intialize data members inside the class.
    So can anyone explain to me what does :
    Code:
    static class xxx
    {
    } yyy;
    mean.
    Is the yyy only instance, what happens here. I saw it in other people's codes...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Just read on static data members. I reckon singletons are too complex for you at this stage.

    This:
    > and there would be only one instance which you would acsses via :: operator

    and this:
    > but i still need to create an instance of class...

    are incompatible.

    You can however create a class that only has static data members and doesn't need to be instanced. You just access the class members as class::member... if taking head of your thread title and what I understod of your overall initial post
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    just explain to me what does
    Code:
    static class xxx
    {
    } yyy;
    mean ?
    That's all i want to know?
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  11. #11
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Quote Originally Posted by Mario F.
    This:
    > and there would be only one instance which you would acsses via :: operator

    and this:
    > but i still need to create an instance of class...

    are incompatible.
    It should be incompatible, 'cause what i'm trying to say is :
    * i want to acsses via :: operator
    * i don't want to create and instance of the class
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Is that really all you want to know?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    yes, it is !
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    I'll try this one more time. Last one.

    Read on static data members. It's as easy as googling for "c++ static data members".

    And just so that we get this straight that static declaration you are showing is not what you want.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    I don't know is it what i want, 'cause i don't know what it is. And i'm asking here what is it. And I gues you know what it is, but you don't want to say what it is...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21