Global object syntax

Discussion in 'C++' started by D, Jun 8, 2004.

  1. D

    D Guest

    ( new to C++ )
    How does one make an "object" global?
    I know from C that you can declare a variable outside
    of main and it's global. And all functions are proto'ed and
    defined outside of main (and called from within main ).
    So now ... in C++ a class that becomes an object has
    both variables and functions in it, so exactly what it the syntax
    for declaring an "object" global.
    D, Jun 8, 2004
    #1
    1. Advertising

  2. In article <>, D wrote:
    > ( new to C++ )
    > How does one make an "object" global?
    > I know from C that you can declare a variable outside
    > of main and it's global. And all functions are proto'ed and
    > defined outside of main (and called from within main ).
    > So now ... in C++ a class that becomes an object has
    > both variables and functions in it, so exactly what it the syntax
    > for declaring an "object" global.


    The same as in C:
    //a class in the global namespace:
    class foo { int i; public: void s(int j) {i=j;} };
    foo bar; // a global foo object
    int baz; // a global int object

    int main() { bar.s(3); baz = 4;}
    --
    Robert Bauck Hamar
    Robert Bauck Hamar, Jun 8, 2004
    #2
    1. Advertising

  3. D

    JKop Guest

    D posted:

    > ( new to C++ )
    > How does one make an "object" global?
    > I know from C that you can declare a variable outside
    > of main and it's global. And all functions are proto'ed and
    > defined outside of main (and called from within main ).
    > So now ... in C++ a class that becomes an object has
    > both variables and functions in it, so exactly what it the syntax
    > for declaring an "object" global.
    >
    >



    Some people call a small axe a "hatchet", although there's nothing wrong
    with calling it an axe.


    int monkey;
    SomeClass ape;


    Call them objects, call them variables, or just call "int" a variable, and
    call "ape" an object.

    Moral of the story: They're worked with in exactly the same way.


    int monkey;
    SomeClass ape;

    int main(void)
    {
    monkey = 5;

    SomeClass = 6;
    SomeClass.DoStuff(73,"Chocolate");
    }
    JKop, Jun 8, 2004
    #3
  4. D

    jeffc Guest

    "D" <NoMail@NoSpam> wrote in message news:...
    > ( new to C++ )
    > How does one make an "object" global?
    > I know from C that you can declare a variable outside
    > of main and it's global. And all functions are proto'ed and
    > defined outside of main (and called from within main ).
    > So now ... in C++ a class that becomes an object has
    > both variables and functions in it, so exactly what it the syntax
    > for declaring an "object" global.


    It's the same as with any variable.
    jeffc, Jun 8, 2004
    #4
  5. "D" <NoMail@NoSpam> wrote in message news:<>...
    > ( new to C++ )
    > How does one make an "object" global?


    Same as you do in C.
    > I know from C that you can declare a variable outside
    > of main and it's global.


    not really.

    int main(){}

    void FooBar(){int a;}

    Here, a is declared outside of main, but it is not global.


    > And all functions are proto'ed and
    > defined outside of main (and called from within main ).
    > So now ... in C++ a class that becomes an object has


    "class that becomes an object"??? What do you mean? This is like
    saying:"type that becomes a variable".

    > both variables and functions in it, so exactly what it the syntax
    > for declaring an "object" global.


    Just as in C.

    //at global scope:
    class foo{/*...*/};

    foo bar; //you have a global object (bar) of type foo

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    To iterate is human, to recurse divine.
    -L. Peter Deutsch
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Prateek R Karandikar, Jun 9, 2004
    #5
    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. gabriele renzi
    Replies:
    2
    Views:
    194
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    193
  3. Good Night Moon
    Replies:
    9
    Views:
    267
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    304
    Jacob Grover
    Jul 18, 2008
  5. User1014
    Replies:
    3
    Views:
    183
    Richard Cornford
    Dec 1, 2006
Loading...

Share This Page