acces a static variable through base class

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

    acces a static variable through base class

    Hi,
    Can anyone help me out here?

    I need a construction where a static variable in a derived class is
    accessible through its base class.
    The base class is part of a framework.
    The variable in the derived class must be static,
    because the framework must access this variable when instantiation has not
    taken place yet.
    After instantiation other parts of the framework must access this variable
    through the base class.

    Is there a design pattern or neat c++ construction for this problem?

    Thanks in advance,
    Anthony Lansbergen


  • Attila Feher

    #2
    Re: acces a static variable through base class

    Anthony wrote:[color=blue]
    > Hi,
    > Can anyone help me out here?
    >
    > I need a construction where a static variable in a derived class is
    > accessible through its base class.
    > The base class is part of a framework.
    > The variable in the derived class must be static,
    > because the framework must access this variable when instantiation
    > has not taken place yet.
    > After instantiation other parts of the framework must access this
    > variable through the base class.
    >
    > Is there a design pattern or neat c++ construction for this problem?[/color]

    Give some more context. In general: it cannot be done. Initialization
    order of statics across translation units is indefined in C++.

    --
    Attila aka WW


    Comment

    • Frank Schmitt

      #3
      Re: acces a static variable through base class

      "Anthony" <anthony@EdAsys .E-is-A.A-is-E.nl> writes:
      [color=blue]
      > Hi,
      > Can anyone help me out here?
      >
      > I need a construction where a static variable in a derived class is
      > accessible through its base class.
      > The base class is part of a framework.
      > The variable in the derived class must be static,
      > because the framework must access this variable when instantiation has not
      > taken place yet.
      > After instantiation other parts of the framework must access this variable
      > through the base class.[/color]

      This sounds like a design flaw to me - a base class shouldn't need to know
      about its descendants.
      [color=blue]
      > Is there a design pattern or neat c++ construction for this problem?[/color]

      You could use a static member function in Derived returning the value.

      HTH & kind regards
      frank

      --
      Frank Schmitt
      4SC AG phone: +49 89 700763-0
      e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com

      Comment

      • Anthony

        #4
        Re: acces a static variable through base class

        "Attila Feher" <attila.feher@l mf.ericsson.se> schreef in bericht
        news:bkp2q6$8ot $1@newstree.wis e.edt.ericsson. se...[color=blue]
        > Anthony wrote:[color=green]
        > > Hi,
        > > Can anyone help me out here?
        > >
        > > I need a construction where a static variable in a derived class is
        > > accessible through its base class.
        > > The base class is part of a framework.
        > > The variable in the derived class must be static,
        > > because the framework must access this variable when instantiation
        > > has not taken place yet.
        > > After instantiation other parts of the framework must access this
        > > variable through the base class.
        > >
        > > Is there a design pattern or neat c++ construction for this problem?[/color]
        >
        > Give some more context. In general: it cannot be done. Initialization
        > order of statics across translation units is indefined in C++.
        >
        > --
        > Attila aka WW
        >
        >[/color]

        Hi,
        Here some more context for my problem:

        I have a Class BaseContainer and a class BaseComponent in my framework.
        Concrete containers and components are created by deriving from the base
        classes.
        The components must run in the containers execution thread.
        The concrete containers are static defined in the application.
        Thus to bind a component to a container I want every component to hold a
        static reference to it's container.
        This static reference must be accessible through its base class
        "BaseComponent" .
        This way I can generalize (in the framework) all components as
        BaseComponents and still reach their (base) containers.
        The reason that this Container pointer must be static, is that the framework
        also uses a templated builder class (parameterised class) .
        I use this builder mechanism to create an component a-synchrone in a
        multithreaded system.

        Thanks in advance,

        Anthony Lansbergen


        Comment

        • Attila Feher

          #5
          Re: acces a static variable through base class

          Anthony wrote:
          [SNIP][color=blue]
          > Here some more context for my problem:
          >
          > I have a Class BaseContainer and a class BaseComponent in my
          > framework. Concrete containers and components are created by deriving
          > from the base classes.
          > The components must run in the containers execution thread.
          > The concrete containers are static defined in the application.
          > Thus to bind a component to a container I want every component to
          > hold a static reference to it's container.
          > This static reference must be accessible through its base class
          > "BaseComponent" .
          > This way I can generalize (in the framework) all components as
          > BaseComponents and still reach their (base) containers.
          > The reason that this Container pointer must be static, is that the
          > framework also uses a templated builder class (parameterised class) .
          > I use this builder mechanism to create an component a-synchrone in a
          > multithreaded system.[/color]

          I must be dumb, but I still don't get it. You cannot make a static, which
          is guaranteed to be initialized before the framework tries to access it.
          Unless it is a compile time contant.

          Does the framework access you object, or directly that pointer?

          What does "The concrete containers are static defined in the application."
          means?

          etc.

          --
          Attila aka WW


          Comment

          • Anthony

            #6
            Re: acces a static variable through base class

            >[color=blue]
            > I must be dumb, but I still don't get it. You cannot make a static, which
            > is guaranteed to be initialized before the framework tries to access it.
            > Unless it is a compile time contant.
            >
            > Does the framework access you object, or directly that pointer?
            >
            > What does "The concrete containers are static defined in the application."
            > means?
            >
            > etc.
            >[/color]

            Hi, thanks for your input,

            I think the solution I have now is to complex to explain and that already
            shows that I have got a poor design.
            So the problem is more a design question than an implementation solution I
            'm looking for.
            Thanks for your time,
            I will post a new question to this group which will be more a design
            question.

            Greetings,
            Anthony Lansbergen


            Comment

            • Michel de Becdelièvre

              #7
              Re: acces a static variable through base class

              For problems of static initialisation in a multithread environment, look
              at how it is done for cout and cin (if your instantiation is done a program
              startup).

              "Anthony" <anthony@EdAsys .E-is-A.A-is-E.nl> a écrit dans le message de
              news:3f700bb5$0 $58711$e4fe514c @news.xs4all.nl ...[color=blue]
              > Hi,
              > Can anyone help me out here?
              >
              > I need a construction where a static variable in a derived class is
              > accessible through its base class.
              > The base class is part of a framework.
              > The variable in the derived class must be static,
              > because the framework must access this variable when instantiation has not
              > taken place yet.
              > After instantiation other parts of the framework must access this variable
              > through the base class.
              >
              > Is there a design pattern or neat c++ construction for this problem?
              >
              > Thanks in advance,
              > Anthony Lansbergen
              >
              >[/color]


              Comment

              Working...