A class in another class can reach the first class private members, right? But how? I don't get it to work
Like:
class C1
{
private:
int i;
class C2 {};
};
A class in another class can reach the first class private members, right? But how? I don't get it to work
Like:
class C1
{
private:
int i;
class C2 {};
};
You can access the private members of the surrounding class from the inner class only if you declare inner classes as a friend of the surround class. You will probably need forward declarations as well.
for example
class C1
{
class C2; // forward declaration
friend class C2;
private:
int i;
class C2 {};
};
You cannot do that directly in C++ because that requires static scoping and C++ supports only dynamic scoping. You may however use this in Java.
K. Can I reach it with just i or do I have to refer to the class?
i thought that the "restricted" keywork allowed this
I think that Darrok is right on this one. This is a big part of c++ and is known as inheritance. Therefore, if u want to know more about it, search inheritance and c++ on a search engine.
Actually what I say works for static variables. I don't think that restricted keyword (?) or inheritance will do any difference either. In C++, inner classes are simply name hiding mechanisms. There is no a link to an enclosing object and no implied permission in C++.
All I know to get the value from outer class is:
class C1
{
class C2; // forward declaration
friend class C2;
private:
static int i;
class C2 {
void foo() {
C1::i = 3;
};
};
Yes, but I don't want to have a static variable...
Yes, but I don't think there is another way either. You may use an instance of C1 in C2, which I'm sure you don't want either.
No, because it couldn't be guaranteed that the outer class would be constructed first, so the inner class may be accessing data that doesn't exist.A class in another class can reach the first class private members, right?
You can either seperate the two classes, derive C2 from C1 or as ozgulker has pointed out create an instance of C1 in C2 (C2 can then access C1's members because C1 is guaranteed to be created first).
zen
After playing around with some code, there is a way to do what you want, but you'll have to pass in an outer class object or reference to each of the inner class functions. Something like this -
Code:#include <iostream> using namespace std; class C1 { private: int i; public: class C2 { public: C2(C1 &ref) { ref.i=99;}; void PrintI(C1 &ref){cout << ref.i;} }; friend class C2; }; int main() { C1 c1; C1::C2 c2(c1); c2.PrintI(c1); return 0; }
zen
There is no "restricted" keyword.
offcourse it canbe done
Code:class A { int i; public: class B;//just a declaration friend class B; void interface_functions();// }; class A::B//because B is in A's scope { int j; public: int fn(); }; void A::interface_functions() { } int A::B::fn() { A a; a.i+=j;//allows private member access return a.i; }
also if for somereason you want to hide the implementation in this inner class then you can put that part in a cpp file and the first cass declaration in a header file this way you can distribute yr library wth the header file and just the obj for the cpp file
jv