Global object syntax

D

D

( 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.
 
R

Robert Bauck Hamar

( 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;}
 
J

JKop

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");
}
 
J

jeffc

D said:
( 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.
 
P

Prateek R Karandikar

D said:
( 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
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top