Static data memer

Discussion in 'C++' started by Surya Kiran, Jan 28, 2004.

  1. Surya Kiran

    Surya Kiran Guest

    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
    Surya Kiran, Jan 28, 2004
    #1
    1. Advertising

  2. Surya Kiran

    Sharad Kala Guest

    "Surya Kiran" <> wrote in message
    news:...
    > 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.


    How about posting the code and the error you get ?
    Sharad Kala, Jan 28, 2004
    #2
    1. Advertising

  3. Surya Kiran

    lilburne Guest

    Surya Kiran wrote:

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


    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.
    lilburne, Jan 28, 2004
    #3
  4. "Surya Kiran" <> skrev i en meddelelse
    news:...
    > 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 ;


    This name is confusing - it is not a type.
    >
    > 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


    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
    Peter Koch Larsen, Jan 28, 2004
    #4
  5. Surya Kiran

    Surya Kiran Guest

    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::mylist = 0;

    Please clarify.
    Thanks in advance,
    Surya



    "Peter Koch Larsen" <> wrote in message news:<RGNRb.80000$>...
    > "Surya Kiran" <> skrev i en meddelelse
    > news:...
    > > 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 ;

    >
    > This name is confusing - it is not a type.
    > >
    > > 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

    >
    > 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
    Surya Kiran, Feb 5, 2004
    #5
  6. "Surya Kiran" <> wrote...
    > My question is how do we initialize a pointer to static member.


    And your example shows a static member that is a pointer, not a pointer
    to static member. Do you actually know what you want?

    >
    > if its a ordinary class (class MyClass) we can initialize it to NULL,


    You can initialise any pointer to 0, regardless what it points to.

    > 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::mylist = 0;


    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.

    >
    > Please clarify.
    > Thanks in advance,
    > Surya
    >
    >
    >
    > "Peter Koch Larsen" <> wrote in message

    news:<RGNRb.80000$>...
    > > "Surya Kiran" <> skrev i en meddelelse
    > > news:...
    > > > 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 ;

    > >
    > > This name is confusing - it is not a type.
    > > >
    > > > 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

    > >
    > > 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
    Victor Bazarov, Feb 5, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natan

    Static vs. non-static connection

    Natan, May 24, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    7,322
    Sami Vaaraniemi
    May 26, 2004
  2. Tim
    Replies:
    2
    Views:
    1,504
  3. Tony Johansson
    Replies:
    3
    Views:
    354
    Karl Heinz Buchegger
    May 20, 2005
  4. Robin
    Replies:
    0
    Views:
    414
    Robin
    Jun 6, 2007
  5. Leslaw Bieniasz

    local static data vs. global data

    Leslaw Bieniasz, Oct 23, 2012, in forum: C++
    Replies:
    3
    Views:
    387
    Werner
    Oct 24, 2012
Loading...

Share This Page