How to change instance variable value in instance function

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

    How to change instance variable value in instance function

    Hi, guys,
    I am wondering if I could change the static variable value in one
    static function?

    If I have a class:

    class Apple
    {
    public:
    static Apple* Instance()
    {
    static Apple* pInstance(NULL) ;
    if( ! pInstance)
    {
    pInstance = new Apple();
    }
    return pInstance;
    }
    }


    Can I change the pInstance from outside?
    Something like: Apple::Instance ()::pInstance = NULL;

    Thanks!
  • Jeroen Mostert

    #2
    Re: How to change instance variable value in instance function

    Ed wrote:
    If I have a class:
    >
    class Apple
    {
    public:
    static Apple* Instance()
    {
    static Apple* pInstance(NULL) ;
    if( ! pInstance)
    {
    pInstance = new Apple();
    }
    return pInstance;
    }
    }
    >
    >
    Can I change the pInstance from outside?
    No, unless you somehow pass a pointer or reference to the variable to the
    outside world from the function. But that's such a mind-bogglingly silly
    violation of encapsulation that it's not worth considering.

    If you want this ability, then don't use function-scoped static variables.
    Use a static class member instead. If you can't modify the original class,
    then consider the possibility that there's a very good reason why you're not
    supposed to change this value from the outside.

    --
    J.

    Comment

    • David Wilkinson

      #3
      Re: How to change instance variable value in instance function

      Jeroen Mostert wrote:
      Ed wrote:
      >If I have a class:
      >>
      >class Apple
      >{
      >public:
      > static Apple* Instance()
      > {
      > static Apple* pInstance(NULL) ;
      > if( ! pInstance)
      > {
      > pInstance = new Apple();
      > }
      > return pInstance;
      > }
      >}
      >>
      >>
      >Can I change the pInstance from outside?
      >
      No, unless you somehow pass a pointer or reference to the variable to
      the outside world from the function. But that's such a mind-bogglingly
      silly violation of encapsulation that it's not worth considering.
      >
      If you want this ability, then don't use function-scoped static
      variables. Use a static class member instead. If you can't modify the
      original class, then consider the possibility that there's a very good
      reason why you're not supposed to change this value from the outside.
      Ed:

      I don't know why you want to do this, but one way would be

      static Apple* Instance(bool bReset = false)
      {
      static Apple* pInstance(NULL) ;
      if( bReset )
      {
      delete pInstance;
      pInstance = NULL;
      }
      else if( ! pInstance )
      {
      pInstance = new Apple();
      }
      return pInstance;
      }

      BTW, since this is a question on standard C++ (not C++/CLI), you would be better
      to ask it in microsoft.publi c.vc.language.

      --
      David Wilkinson
      Visual C++ MVP

      Comment

      • Ed

        #4
        Re: How to change instance variable value in instance function

        On Aug 3, 9:58 pm, David Wilkinson <no-re...@effisols. comwrote:
        Jeroen Mostert wrote:
        Ed wrote:
        If I have a class:
        >
        class Apple
        {
        public:
        static Apple* Instance()
        {
        static Apple* pInstance(NULL) ;
        if( ! pInstance)
        {
        pInstance = new Apple();
        }
        return pInstance;
        }
        }
        >
        Can I change the pInstance from outside?
        >
        No, unless you somehow pass a pointer or reference to the variable to
        the outside world from the function. But that's such a mind-bogglingly
        silly violation of encapsulation that it's not worth considering.
        >
        If you want this ability, then don't use function-scoped static
        variables. Use a static class member instead. If you can't modify the
        original class, then consider the possibility that there's a very good
        reason why you're not supposed to change this value from the outside.
        >
        Ed:
        >
        I don't know why you want to do this, but one way would be
        >
        static Apple* Instance(bool bReset = false)
        {
        static Apple* pInstance(NULL) ;
        if( bReset )
        {
        delete pInstance;
        pInstance = NULL;
        }
        else if( ! pInstance )
        {
        pInstance = new Apple();
        }
        return pInstance;
        >
        }
        >
        BTW, since this is a question on standard C++ (not C++/CLI), you would be better
        to ask it in microsoft.publi c.vc.language.
        >
        --
        David Wilkinson
        Visual C++ MVP
        Thank you!
        I just don't want the static variable to be declared as a class
        member.

        Ed.

        Comment

        • Pavel Minaev

          #5
          Re: How to change instance variable value in instance function

          On Aug 4, 6:02 am, Ed <seah...@gmail. comwrote:
          >If I have a class:
          >class Apple
          >{
          >public:
          >    static Apple* Instance()
          >    {
          >        static Apple* pInstance(NULL) ;
          >        if( ! pInstance)
          >        {
          >             pInstance = new Apple();
          >        }
          >        return pInstance;
          >    }
          >}
          >
          >Can I change the pInstance from outside?
          >
          I just don't want the static variable to be declared as a class
          member.
          Why, really?

          But anyway, here's another trick:

          class Apple
          {
          static Apple*& pInstance()
          {
          static Apple* value(NULL);
          return value;
          }

          static Apple* Instance()
          {
          if( ! pInstance())
          {
          pInstance() = new Apple();
          }
          return pInstance();
          }
          };

          Comment

          Working...