cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
the inheritance and the scoping
the inheritance and the scoping
Dec 12, 2016 at 8:31am UTC
muhammad k
(15)
What are the potential problems that you can think of (AND how would you fix it?) with the following code:
class Host { int x;
class Nested_One extends Some_Other_Class {
void add_one() {
x++;
}
}
}
Dec 12, 2016 at 9:18am UTC
muhammad k
(15)
any help?
Dec 12, 2016 at 11:14am UTC
coder777
(8444)
x
is not a member of the calss
Nested_One
, hence you cannot access
x
directly. You need to pass an instance of
Host
in order to access x.
Please use code tags: [co
de]
Your code
[/co
de]
Read this:
http://www.cplusplus.com/articles/z13hAqkS/
Dec 12, 2016 at 9:31pm UTC
muhammad k
(15)
but I thought x is defined in a global scope.. no?
Dec 13, 2016 at 7:27am UTC
coder777
(8444)
No. If you want to make the variable global use
static
:
http://en.cppreference.com/w/cpp/language/static
Note that if you declare a variable static inside a class you need to define it outside:
1
2
3
4
5
class
Host {
static
int
x; ... }
int
Host::x = 0;
Topic archived. No new replies allowed.