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

    accessing variable of a class without instantiating or using static keyword

    I have a public/private variable (var) in class X, which I need to access from class Y.
    The constraints are that class X is instantiated only once from the application. It cannot be instantiated in class Y again.
    and
    the variable (var) cannot be made static, as 2 different applications instantiating this class X should see the variable differently.
    and cannot make the class Y nested in class X, also in class Y the func() arguments cannot be added, because this function is being called from somewhere in the state machine which passes on only 2 arguments.

    class X
    {
    public:
    int a;
    };

    class Y
    {
    static func(int aa, int bb)
    {
    a = 1; //want to perform the set/get operations from this class for 'a'...
    }

    };

    require experts help in solving this,
    thanks in advance.

  2. #2
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: accessing variable of a class without instantiating or using static keyword

    You should have a look at the Singleton Pattern.
    your humble savant

  3. #3
    Join Date
    Sep 2010
    Posts
    2

    Re: accessing variable of a class without instantiating or using static keyword

    hi, class X cannot be singleton, as another application should be able to create one more instance of class X

    i.e. app1 should create instance of class X
    and app2 should also create its own instance of class X.
    to be more clear... applications are accessing the One Network stack and class X is defined as part of network stack state machine.

  4. #4
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: accessing variable of a class without instantiating or using static keyword

    A Singleton is unique to a process.
    You need pay special attention if you have a multithreaded envirnoment, see the section Singleton_pattern - Implementation
    your humble savant

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: accessing variable of a class without instantiating or using static keyword

    Quote Originally Posted by g97468 View Post
    I don't know if it fits the bill but for simple Singleton needs I often use a so called Meyer's Singleton

    Code:
    class X
    {
       public:
       int a;
    };
    //
    X& ONE_X(); // X Singleton
    //
    class Y
    {
       static func(int aa, int bb)
       {
          ONE_X().a = 1;  //want to perform the set/get operations from this class for 'a'...           
       } 
    };
    // implementation on a .cpp file
    #include X.h
    X& ONE_X() {
       static X x; // instantiated once
       return x;
    }
    Last edited by nuzzle; September 20th, 2010 at 06:59 AM.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: accessing variable of a class without instantiating or using static keyword

    So... you want two different programs, running different threads/process to access a same variable. Sounds to me like you need to store said variable outside of the process.

    Depending on your needs, anything from text file, database, environment variable or even register could suit your needs.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: accessing variable of a class without instantiating or using static keyword

    hi, class X cannot be singleton, as another application should be able to create one more instance of class X
    That statement makes no sense. A singleton is instantiated once in every application program instance. Also singleton provides access to the one and only instance via a static member function which returns the one and only instance to the user. So it guarantees one instance per application and provides easy access to the one and only object.

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