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

    c++ global access related

    Hi All,

    In a class , I have Init() method, which gets called if that feature is enabled.

    But somehow, i want to make sure that other methods are called, only if Init() is called earlier.
    Right now, eventhough Init() is not called, other methods are called and resulting in bad behaviour.

    What is best way to implement this ? May be add a static flag in the class and check it at each method call.

    Could c++ experts let me know the better way to implement this ?

    thanks
    pdk

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: c++ global access related

    May be add a static flag
    Why a static flag? Does init() set class static variables? Why can't initialisation be done in the class constructor?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: c++ global access related

    Quote Originally Posted by pdk5 View Post
    Hi All,

    In a class , I have Init() method, which gets called if that feature is enabled.

    But somehow, i want to make sure that other methods are called, only if Init() is called earlier.
    Right now, eventhough Init() is not called, other methods are called and resulting in bad behaviour.

    What is best way to implement this ? May be add a static flag in the class and check it at each method call.

    Could c++ experts let me know the better way to implement this ?

    thanks
    pdk
    Add a boolean member variable, set it to false in all class ctors.
    Your Init() is now supposed to reset this member to true.
    Then each other method is supposed to check this member and return immediately if it is not true.
    Victor Nijegorodov

  4. #4
    Join Date
    Feb 2017
    Posts
    674

    Re: c++ global access related

    Quote Originally Posted by pdk5 View Post
    Right now, eventhough Init() is not called, other methods are called and resulting in bad behaviour.
    If the other methods belong to the same class as Init() you can use the State pattern, a common OO design pattern,

    https://en.wikipedia.org/wiki/State_pattern

    The State pattern is a way to allow an object to change its behavior at runtime. In this case you would have two states, one initial and one after Init() has been called. In the initial state the methods do nothing so if they're called nothing happens. When Init() is called it first performs the initialization but then it also switches states so the methods start doing what they're supposed to do.

    The advantage of this approach is explained in the third paragraph of the link I posted.

    The OO implementation is somewhat involved and you could off course accomplish the same thing by simply introducing an internal boolean variable indicating whether Init() has been called or not. Since there are just two states and this is unlikely to change that may very well be the best way to do it. I just wanted to mention that there is a the standard OO way to do this indeed.
    Last edited by wolle; April 17th, 2018 at 02:05 AM.

  5. #5
    Join Date
    May 2015
    Posts
    540

    Re: c++ global access related

    sorry kaud and others , was away on some urgency.
    But the class is nested one, it is bit complex than one i shown..i;ll study further. (homework for me)

    Thanks a lot for all i/ps

Tags for this Thread

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