I'd like to know if there's any way to see (and not access neither modify) a private member from outside its class ?
template <typename type_>
class OBJ1
{
//methods
};
class OBJ2
{
private:
OBJ1<int> my_obj;
};
class OBJ3
{
public:
template <typename type_>
void magic_method(OBJ1<type_> obj)
{
//with another intermediate class, call one of OBJ1's public methods
//anotherClass::call(obj);
}
};
Obviously this does not work, as G++ does not know what is my_obj
within class OBJ3
. Is there a way to make this code compile ? Like forward declaration or something ? And again, other classes just need to know that "OBJ1 declared objects" exist.
Thanks !
// call one of OBJ1's *public* methods
You can easily make that work, as show below. What's the actual problem, and what does OBJ2
have to do with anything? Post code that displays the private access error you're concerned about.
#include <iostream>
#include <typeinfo>
template <typename type_>
class OBJ1
{
public:
void print_type_name() {
std::cout << typeid(type_).name() << "\n";
}
};
/*
class OBJ2
{
private:
OBJ1<int> my_obj;
};
*/
class anotherClass {
public:
template <typename type_>
static void call(OBJ1<type_> obj) {
obj.print_type_name();
}
};
class OBJ3
{
public:
template <typename type_>
void magic_method(OBJ1<type_> obj)
{
//with another intermediate class, call one of OBJ1's public methods
anotherClass::call(obj);
}
};
int main() {
OBJ3 obj3;
OBJ1<int> obj1;
OBJ1<double> objd;
obj3.magic_method(obj1);
obj3.magic_method(objd);
}