Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Global object syntax

Reply
Thread Tools

Global object syntax

 
 
D
Guest
Posts: n/a
 
      06-08-2004
( 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.


 
Reply With Quote
 
 
 
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      06-08-2004
In article <(E-Mail Removed)>, 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
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      06-08-2004
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");
}
 
Reply With Quote
 
jeffc
Guest
Posts: n/a
 
      06-08-2004

"D" <NoMail@NoSpam> wrote in message news:(E-Mail Removed)...
> ( 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.


 
Reply With Quote
 
Prateek R Karandikar
Guest
Posts: n/a
 
      06-09-2004
"D" <NoMail@NoSpam> wrote in message news:<(E-Mail Removed)>...
> ( 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
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are global functions methods of the global object? User1014 Javascript 3 12-01-2006 01:02 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments