Class wide object declaration question

Discussion in 'C++' started by jeff_j_dunlap@yahoo.com, May 15, 2007.

  1. Guest

    Hello,

    Whenever I need class wide access to an object, I declare it
    dynamically:

    class myClass
    {
    ...
    myObject* obj; // declared dynamically
    ...


    Then I usually create an instance of the object within the
    constructor:

    myClass::myClass()
    {
    ...
    string str = "whatever";
    obj = new myObj(str); // parameter passed to obj
    ...


    Doing this, I am able to access myObj::eek:bj anywhere from within my
    class. Is it possible to declare obj as a static member variable? If
    so, I have not been able to figure out how to do it.

    Thanks in Advance
    , May 15, 2007
    #1
    1. Advertising

  2. anon Guest

    wrote:
    > Hello,
    >
    > Whenever I need class wide access to an object, I declare it


    What is "class wide access to an object"?

    > dynamically:
    >
    > class myClass
    > {
    > ...
    > myObject* obj; // declared dynamically
    > ...
    >
    >
    > Then I usually create an instance of the object within the
    > constructor:
    >
    > myClass::myClass()
    > {
    > ...
    > string str = "whatever";
    > obj = new myObj(str); // parameter passed to obj
    > ...
    >
    >
    > Doing this, I am able to access myObj::eek:bj anywhere from within my


    What is myObj?

    > class. Is it possible to declare obj as a static member variable? If
    > so, I have not been able to figure out how to do it.


    I do not see why not. You can do it like this:
    class myClass
    {
    public:
    static myObject obj;
    };

    myObject myClass::eek:bj(arguments to myObject constructor);
    anon, May 15, 2007
    #2
    1. Advertising

  3. Lionel B Guest

    On Tue, 15 May 2007 01:27:19 -0700, jeff_j_dunlap wrote:

    > Hello,
    >
    > Whenever I need class wide access to an object, I declare it
    > dynamically:
    >
    > class myClass
    > {
    > ...
    > myObject* obj; // declared dynamically ...
    >
    >
    > Then I usually create an instance of the object within the constructor:
    >
    > myClass::myClass()
    > {
    > ...
    > string str = "whatever";
    > obj = new myObj(str); // parameter passed to obj ...
    >
    >
    > Doing this, I am able to access myObj::eek:bj anywhere from within my
    > class. Is it possible to declare obj as a static member variable? If
    > so, I have not been able to figure out how to do it.


    class myClass
    {
    ...
    static myObject* obj;
    ...
    };

    Note that this is just a declaration; you will also have to *define*
    myClass::eek:bj in some compilation unit:

    myObject* myClass::eek:bj;

    myClass::eek:bj will be accessible to all class member functions, including
    static ones. Where/how you allocate/deallocate myClass::eek:bj is up to you,
    depending on what you are trying to do.

    --
    Lionel B
    Lionel B, May 15, 2007
    #3
  4. Salt_Peter Guest

    On May 15, 4:27 am, wrote:
    > Hello,
    >
    > Whenever I need class wide access to an object, I declare it
    > dynamically:
    >
    > class myClass
    > {
    > ...
    > myObject* obj; // declared dynamically
    > ...
    >
    > Then I usually create an instance of the object within the
    > constructor:
    >
    > myClass::myClass()
    > {
    > ...
    > string str = "whatever";
    > obj = new myObj(str); // parameter passed to obj
    > ...
    >
    > Doing this, I am able to access myObj::eek:bj anywhere from within my
    > class. Is it possible to declare obj as a static member variable? If
    > so, I have not been able to figure out how to do it.
    >
    > Thanks in Advance


    I'ld suggest reading the FAQ

    [10.10] Why can't I initialize my static member data in my
    constructor's initialization list?
    [10.11] Why are classes with static data members getting linker
    errors?
    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.10
    Salt_Peter, May 15, 2007
    #4
  5. Lionel B Guest

    On Tue, 15 May 2007 10:47:52 +0200, anon wrote:

    > wrote:
    >> Hello,
    >>
    >> Whenever I need class wide access to an object, I declare it

    >
    > What is "class wide access to an object"?
    >
    >> dynamically:
    >>
    >> class myClass
    >> {
    >> ...
    >> myObject* obj; // declared dynamically ...
    >>
    >>
    >> Then I usually create an instance of the object within the constructor:
    >>
    >> myClass::myClass()
    >> {
    >> ...
    >> string str = "whatever";
    >> obj = new myObj(str); // parameter passed to obj ...
    >>
    >>
    >> Doing this, I am able to access myObj::eek:bj anywhere from within my

    >
    > What is myObj?
    >
    >> class. Is it possible to declare obj as a static member variable? If
    >> so, I have not been able to figure out how to do it.

    >
    > I do not see why not. You can do it like this: class myClass
    > {
    > public:
    > static myObject obj;
    > };
    >
    > myObject myClass::eek:bj(arguments to myObject constructor);


    Couple of points: I agree it is not clear what the OP is trying to
    achieve, but (1) why make obj public, since the OP only seems to require
    "class wide access" (which I take as meaning access for class member/
    static functions)? and (2) in the OP's code obj was a *pointer* to a
    myObject ... not sure why, but maybe there is a valid reason (which he
    hasn't told us).

    --
    Lionel B
    Lionel B, May 15, 2007
    #5
  6. On May 15, 6:27 pm, wrote:
    > Hello,
    >
    > Whenever I need class wide access to an object, I declare it
    > dynamically:
    >
    > class myClass
    > {
    > ...
    > myObject* obj; // declared dynamically
    > ...
    >
    > Then I usually create an instance of the object within the
    > constructor:
    >
    > myClass::myClass()
    > {
    > ...
    > string str = "whatever";
    > obj = new myObj(str); // parameter passed to obj
    > ...
    >
    > Doing this, I am able to access myObj::eek:bj anywhere from within my
    > class. Is it possible to declare obj as a static member variable? If
    > so, I have not been able to figure out how to do it.


    When you say "statically", what do you mean. If you mean the old
    (somewhat deprecated) meaning of local to the compilation unit, then
    you can't do that for a class. If you mean that there is one instance
    per class, then that's the meaning of static in a class/struct.

    I think this code below covers all the sorts of "storage class" that
    exist.

    static int s;

    namespace { int y; /* anonymous namespace for y */}

    // for most purposes, s and y are the same storage class - i.e.
    visible in this
    // compilation unit only.

    struct A
    {
    int a; // one a per instance of A
    static int b; // one b - ever
    const static int c = 5; // only allowed to do this for integral
    types.

    A * instance()
    {
    int f; // created every time execution passes this point.
    (auto)
    static A v; // only one v in the entire program
    // and only created once - the first time
    through.
    return &v;
    }
    };

    int A::b = 2; // need to define b somewhere. usually in one place only

    // special ones - these may show up in the new revision of the
    standard

    void f()
    {
    register int i; // this is like auto but you can't take the address
    of i
    // which allows the compiler to do optimizations
    for i
    }

    __thread int t; // one instance of t per thread.

    .... did I miss one ?
    Gianni Mariani, May 15, 2007
    #6
  7. anon Guest

    Lionel B wrote:
    > On Tue, 15 May 2007 10:47:52 +0200, anon wrote:
    >
    >> wrote:
    >>> Whenever I need class wide access to an object, I declare it

    >> What is "class wide access to an object"?
    >>
    >>> dynamically:
    >>>
    >>> class myClass
    >>> {
    >>> ...
    >>> myObject* obj; // declared dynamically ...
    >>>
    >>>
    >>> Then I usually create an instance of the object within the constructor:
    >>>
    >>> myClass::myClass()
    >>> {
    >>> ...
    >>> string str = "whatever";
    >>> obj = new myObj(str); // parameter passed to obj ...
    >>>
    >>>
    >>> Doing this, I am able to access myObj::eek:bj anywhere from within my

    >> What is myObj?
    >>
    >>> class. Is it possible to declare obj as a static member variable? If
    >>> so, I have not been able to figure out how to do it.

    >> I do not see why not. You can do it like this: class myClass
    >> {
    >> public:
    >> static myObject obj;
    >> };
    >>
    >> myObject myClass::eek:bj(arguments to myObject constructor);

    >
    > Couple of points: I agree it is not clear what the OP is trying to
    > achieve, but (1) why make obj public, since the OP only seems to require
    > "class wide access" (which I take as meaning access for class member/
    > static functions)? and (2) in the OP's code obj was a *pointer* to a
    > myObject ... not sure why, but maybe there is a valid reason (which he
    > hasn't told us).
    >


    Salt_Peter gave much better response then both of us did ;)

    Anyway, that was just a simple example, therefore you can put it to
    private, make it a pointer, reference or whatever you like.
    anon, May 15, 2007
    #7
    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. Web Developer

    char 8bit wide or 7bit wide in c++?

    Web Developer, Jul 31, 2003, in forum: C++
    Replies:
    2
    Views:
    581
    John Harrison
    Jul 31, 2003
  2. Ovidesvideo
    Replies:
    4
    Views:
    504
    Andrey Tarasevich
    Dec 10, 2004
  3. Disc Magnet
    Replies:
    2
    Views:
    711
    Jukka K. Korpela
    May 15, 2010
  4. Disc Magnet
    Replies:
    2
    Views:
    787
    Neredbojias
    May 14, 2010
  5. Martin Rinehart

    80 columns wide? 132 columns wide?

    Martin Rinehart, Oct 31, 2008, in forum: Javascript
    Replies:
    16
    Views:
    176
    John W Kennedy
    Nov 13, 2008
Loading...

Share This Page