Home C++ Global Variable vs Java instance variable
Reply: 0

C++ Global Variable vs Java instance variable

user929
1#
user929 Published in August 15, 2018, 11:16 am

I have a question about global variables in C++. I'm coming from a Java background where you can have an instance variable declared and used like so:

Example.java

class Example
{
   int x;

   doSomththing()
   {
      x = 1;
   }

   doSomethingElse()
   {
      x = 2;
   }
}

My question is, in C++ if i do something similar, would that be classified as a global variable or an instance variable. Observe my C++ example to clarify:

Example.h

class Example{

public:
void do_something();
void do_somethingElse();

private:
int x;

};

Example.cpp

#include Example.h

void Example::do_something()
{
    x = 1;
}

void Example::do_somethingElse()
{
    x = 2;
}

So in the C++ example, is x a global or an instance variable? If its not a global, then how can x be properly defined as a global?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.403398 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO