question on instances

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Lee

    question on instances

    Hi, I am wondering if there is a way to create a class where only one
    predefined instance is allowed. I am just trying to find a way to create a
    global variable with its own member functions whhich would be accessible by
    all other classes. I could just follow the normal class thing but I'm
    wondering if there is a better way to do it.thanks .


  • Pete

    #2
    Re: question on instances

    Alan Lee wrote:[color=blue]
    > Hi, I am wondering if there is a way to create a class where only one
    > predefined instance is allowed. I am just trying to find a way to
    > create a global variable with its own member functions whhich would
    > be accessible by all other classes. I could just follow the normal
    > class thing but I'm wondering if there is a better way to do
    > it.thanks .[/color]

    Google for the "Singleton pattern", or put your data and functions into a
    namespace.

    - Pete


    Comment

    • Pete

      #3
      Re: question on instances

      Alan Lee wrote:[color=blue]
      > Hi, I am wondering if there is a way to create a class where only one
      > predefined instance is allowed. I am just trying to find a way to
      > create a global variable with its own member functions whhich would
      > be accessible by all other classes. I could just follow the normal
      > class thing but I'm wondering if there is a better way to do
      > it.thanks .[/color]

      Google for the "Singleton pattern", or put your data and functions into a
      namespace.

      - Pete


      Comment

      • jeffc

        #4
        Re: question on instances


        "Alan Lee" <alanlee@stanfo rd.edu> wrote in message
        news:c4sb6f$9rh $1@news.Stanfor d.EDU...[color=blue]
        > Hi, I am wondering if there is a way to create a class where only one
        > predefined instance is allowed. I am just trying to find a way to create[/color]
        a[color=blue]
        > global variable with its own member functions whhich would be accessible[/color]
        by[color=blue]
        > all other classes. I could just follow the normal class thing but I'm
        > wondering if there is a better way to do it.thanks .[/color]

        Search the web for the "singleton" pattern. Typically, you must make the
        constructor private or protected, and then provide a static function that
        users can call to get a new object. It will only allow 1 object to exist at
        a time.


        Comment

        • jeffc

          #5
          Re: question on instances


          "Alan Lee" <alanlee@stanfo rd.edu> wrote in message
          news:c4sb6f$9rh $1@news.Stanfor d.EDU...[color=blue]
          > Hi, I am wondering if there is a way to create a class where only one
          > predefined instance is allowed. I am just trying to find a way to create[/color]
          a[color=blue]
          > global variable with its own member functions whhich would be accessible[/color]
          by[color=blue]
          > all other classes. I could just follow the normal class thing but I'm
          > wondering if there is a better way to do it.thanks .[/color]

          Search the web for the "singleton" pattern. Typically, you must make the
          constructor private or protected, and then provide a static function that
          users can call to get a new object. It will only allow 1 object to exist at
          a time.


          Comment

          Working...