it falls apart is where you claimed that even tho item01 is private in the context of SubClass the struct item01 and the variables inside |
Which is exactly what I said in this comment:
You are taking a reference to a BaseClass, but you are passing in a BaseClass::SubClass::item01. Obviously that won't work. Furthermore, your item01 class is private in the SubClass (by default) so you woudn't even be able to instantiate it in line 22. |
And in this comment:
No, the class item01 ITSELF is private within the context of SubClass, because the default access specifier for class is "private" and for struct is "public". |
The item01 data members themselves, like var01 and var02 are PUBLIC. If you have a item01 instance, any code can modify var01 and var02 outside of class methods. |
The statement was
IF you had an item01 instance, you would be able to access var01 and var02 outside of class methods. However given the first statement above, you cannot DIRECTLY create an instance. Although it is still possible if SubClass had a static member function that grants access to item01 and reterns an object, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
class BaseClass
{
public:
class SubClass
{
struct item01 //still private
{
int var01 = 1;
int var02 = 2;
};
public:
static item01 return_item(){return item01{};}
};
};
template<typename T>
void printIt(T& cInput)
{
std::cout << cInput.var01;
}
int main()
{
auto myItem = BaseClass::SubClass::return_item();
myItem.var01 = 3;
printIt(myItem);
return 0;
}
|
Or even a friend function would do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
class BaseClass
{
public:
class SubClass
{
struct item01 //still private
{
int var01 = 1;
int var02 = 2;
};
friend item01 return_item(); //Also private friend declaration.
};
};
BaseClass::SubClass::item01 return_item()
{
return BaseClass::SubClass::item01{};
}
template<typename T>
void printIt(T& cInput)
{
std::cout << cInput.var01;
}
int main()
{
auto myItem = return_item(); //Simpler for client code to access.
myItem.var01 = 3;
printIt(myItem);
return 0;
}
|
So no, I was not wrong. That was intentional.
Then you ask the following:
Please post example of printing contents of myGrid.
|
Once I do so, you go completely silent and ignore it because you knew you were wrong and didn't want to embarrass yourself.
So not only are you an arrogant asshole, you also don't know what the fuck you're even saying. Judging by your previous conversation with dutch in another thread, it seems like that's more than evident. Huge superiority complex, yet with little actual substance, knowledge, nor experience on the subject.
You should just stick to asking questions in the beginners forum because it is clear you don't know what you're talking about.
Thanks for playing, though.