Hi,
The following simple piece of code, compiles with VC2008 but g++ rejects the code:
#include <iostream>
class myclass
{
protected:
void print() { std::cout << "myclass::print();"; }
};
struct access : private myclass
{
static void access_print(myclass& object)
{
// g++ and Comeau reject this line but not VC++
void (myclass::*function) () = &myclass::print;
(object.*function)();
}
};
int main()
{
myclass object;
access::access_print(object);
}
(/W4)
is turned on in VC, but it doesn't give any warning.
g++ 4.4.1 gives me an error:
correct.cpp: In static member function ‘static void access::access_print(myclass&)’:
correct.cpp:6: error: ‘void myclass::print()’ is protected
If g++ is correct, how do I access a protected member of a class? is there another way?
@Suroot Do you mean that I shouldn't pass an object of type myclass
? It doesn't matter actually, g++ gives the same error but VC compiles the code without any warning.
#include <iostream>
class myclass
{
protected:
void print() { std::cout << "myclass::print();"; }
};
struct access : private myclass
{
static void access_print()
{
myclass object;
void (myclass::*function) () = &myclass::print;
(object.*function)();
}
};
int main()
{
access::access_print();
}