Static data memer

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

    Static data memer

    Hi all,
    I've 2 classes class A, and class B.
    and i want to use list of type B in class A. like this

    list<B> typeB ;

    till now its fine. But i want to make it a static member. like

    static list<B> typeB ;

    declaration is not a problem. Definition is the problem. I've 2
    constructors for class B
    1. default constructor B()
    2. B (int a, int b)

    how can i define the static member in class A, for both the
    constructors.

    Thanks in advance,
    Surya
  • Sharad Kala

    #2
    Re: Static data memer


    "Surya Kiran" <skg@fluent.co. in> wrote in message
    news:5946f326.0 401272257.49312 d40@posting.goo gle.com...[color=blue]
    > Hi all,
    > I've 2 classes class A, and class B.
    > and i want to use list of type B in class A. like this
    >
    > list<B> typeB ;
    >
    > till now its fine. But i want to make it a static member. like
    >
    > static list<B> typeB ;
    >
    > declaration is not a problem. Definition is the problem. I've 2
    > constructors for class B
    > 1. default constructor B()
    > 2. B (int a, int b)
    >
    > how can i define the static member in class A, for both the
    > constructors.[/color]

    How about posting the code and the error you get ?


    Comment

    • lilburne

      #3
      Re: Static data memer



      Surya Kiran wrote:
      [color=blue]
      >
      > how can i define the static member in class A, for both the
      > constructors.
      >[/color]

      Static members are only constructed once, so as you don't get to
      construct it multiple times, therefor choose the B ctor that is most
      relevant. Most probably the default one.

      Comment

      • Peter Koch Larsen

        #4
        Re: Static data memer


        "Surya Kiran" <skg@fluent.co. in> skrev i en meddelelse
        news:5946f326.0 401272257.49312 d40@posting.goo gle.com...[color=blue]
        > Hi all,
        > I've 2 classes class A, and class B.
        > and i want to use list of type B in class A. like this
        >
        > list<B> typeB ;[/color]

        This name is confusing - it is not a type.[color=blue]
        >
        > till now its fine. But i want to make it a static member. like
        >
        > static list<B> typeB ;
        >
        > declaration is not a problem. Definition is the problem. I've 2
        > constructors for class B
        > 1. default constructor B()
        > 2. B (int a, int b)
        >
        > how can i define the static member in class A, for both the
        > constructors.
        >
        > Thanks in advance,
        > Surya[/color]

        Well... a static member - if this is what you want - is not declared in a
        constructor. This is what you want:

        // header
        #include <list>
        class B {};

        class A
        {
        static std::list<B> list_of_b;
        };

        // source
        std::list<B> A::list_of_b;

        Kind regards
        Peter


        Comment

        • Surya Kiran

          #5
          Re: Static data memer

          My question is how do we initialize a pointer to static member.

          if its a ordinary class (class MyClass) we can initialize it to NULL,
          but my question is

          //someclass.hpp
          class someclass
          {
          ....
          static list<MyClass> *mylist ; //declaring is over.
          ....
          };

          //Lets assume i'm initialize it in someclass.cpp
          //how do i initialize it.
          //obviously not like this
          // list<MyClass>* someclass::myli st = 0;

          Please clarify.
          Thanks in advance,
          Surya



          "Peter Koch Larsen" <pklspam@mailme .dk> wrote in message news:<RGNRb.800 00$jf4.5147767@ news000.worldon line.dk>...[color=blue]
          > "Surya Kiran" <skg@fluent.co. in> skrev i en meddelelse
          > news:5946f326.0 401272257.49312 d40@posting.goo gle.com...[color=green]
          > > Hi all,
          > > I've 2 classes class A, and class B.
          > > and i want to use list of type B in class A. like this
          > >
          > > list<B> typeB ;[/color]
          >
          > This name is confusing - it is not a type.[color=green]
          > >
          > > till now its fine. But i want to make it a static member. like
          > >
          > > static list<B> typeB ;
          > >
          > > declaration is not a problem. Definition is the problem. I've 2
          > > constructors for class B
          > > 1. default constructor B()
          > > 2. B (int a, int b)
          > >
          > > how can i define the static member in class A, for both the
          > > constructors.
          > >
          > > Thanks in advance,
          > > Surya[/color]
          >
          > Well... a static member - if this is what you want - is not declared in a
          > constructor. This is what you want:
          >
          > // header
          > #include <list>
          > class B {};
          >
          > class A
          > {
          > static std::list<B> list_of_b;
          > };
          >
          > // source
          > std::list<B> A::list_of_b;
          >
          > Kind regards
          > Peter[/color]

          Comment

          • Victor Bazarov

            #6
            Re: Static data memer

            "Surya Kiran" <skg@fluent.co. in> wrote...[color=blue]
            > My question is how do we initialize a pointer to static member.[/color]

            And your example shows a static member that is a pointer, not a pointer
            to static member. Do you actually know what you want?
            [color=blue]
            >
            > if its a ordinary class (class MyClass) we can initialize it to NULL,[/color]

            You can initialise any pointer to 0, regardless what it points to.
            [color=blue]
            > but my question is
            >
            > //someclass.hpp
            > class someclass
            > {
            > ...
            > static list<MyClass> *mylist ; //declaring is over.
            > ...
            > };
            >
            > //Lets assume i'm initialize it in someclass.cpp
            > //how do i initialize it.
            > //obviously not like this
            > // list<MyClass>* someclass::myli st = 0;[/color]

            Yes, that's the way, if you want to be explicit. Static data are
            initialised to 0 anyway, even if you don't provide an initialiser.
            [color=blue]
            >
            > Please clarify.
            > Thanks in advance,
            > Surya
            >
            >
            >
            > "Peter Koch Larsen" <pklspam@mailme .dk> wrote in message[/color]
            news:<RGNRb.800 00$jf4.5147767@ news000.worldon line.dk>...[color=blue][color=green]
            > > "Surya Kiran" <skg@fluent.co. in> skrev i en meddelelse
            > > news:5946f326.0 401272257.49312 d40@posting.goo gle.com...[color=darkred]
            > > > Hi all,
            > > > I've 2 classes class A, and class B.
            > > > and i want to use list of type B in class A. like this
            > > >
            > > > list<B> typeB ;[/color]
            > >
            > > This name is confusing - it is not a type.[color=darkred]
            > > >
            > > > till now its fine. But i want to make it a static member. like
            > > >
            > > > static list<B> typeB ;
            > > >
            > > > declaration is not a problem. Definition is the problem. I've 2
            > > > constructors for class B
            > > > 1. default constructor B()
            > > > 2. B (int a, int b)
            > > >
            > > > how can i define the static member in class A, for both the
            > > > constructors.
            > > >
            > > > Thanks in advance,
            > > > Surya[/color]
            > >
            > > Well... a static member - if this is what you want - is not declared in[/color][/color]
            a[color=blue][color=green]
            > > constructor. This is what you want:
            > >
            > > // header
            > > #include <list>
            > > class B {};
            > >
            > > class A
            > > {
            > > static std::list<B> list_of_b;
            > > };
            > >
            > > // source
            > > std::list<B> A::list_of_b;
            > >
            > > Kind regards
            > > Peter[/color][/color]


            Comment

            Working...