private members not so private.
hi, I just thought i'd post this bit of code showing how you can access private data of a class without using interfaces, I don't know if this is legal in c++ or if it has just been overlooked but anywayz:
Code:
class Test
{
public:
void print(void)
{
std::cout << i << "\n";
}
private:
int i;
}
int main()
{
union
{
Test t;
int i;
};
i = 5; // legal
t.i = 5; // illegal
t.print();
}
output:
5
because the class and integer occupy the same stack space of the main functions stack frame, anyway I thought my compiler would have noticed this.
Re: private members not so private.
Unions allow you to do all sorts of stuff that's bad. It's up to the programmer not to abuse the ability.
Re: private members not so private.
Quote:
Originally Posted by staticVoid
hi, I just thought i'd post this bit of code showing how you can access private data of a class without using interfaces, I don't know if this is legal in c++ or if it has just been overlooked but anywayz:
After adding the requisite #include's and the terminating semicolon for the class, the code does not compile for the Comeau compiler:
Code:
#include <iostream>
class Test
{
public:
void print(void)
{
std::cout << i << "\n";
}
private:
int i;
};
int main()
{
union
{
Test t;
int i;
}
i = 5; // legal
t.i = 5; // illegal
t.print();
}
Code:
Thank you for testing your code with Comeau C/C++!
Tell others about http://www.comeaucomputing.com/tryitout !
Your Comeau C/C++ test results are as follows:
Comeau C/C++ 4.3.10.1 (May 29 2008 09:37:15) for ONLINE_EVALUATION_BETA1
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 25: error: no suitable constructor exists to convert from "int"
to "union <unnamed>"
i = 5; // legal
^
"ComeauTest.c", line 26: error: identifier "t" is undefined
t.i = 5; // illegal
^
2 errors detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
Always mention the compiler and version that you're using.
Regards,
Paul McKenzie
Re: private members not so private.
Compilers are far from perfect. If you want to get around their protections/safety mechanisms, you'll certainly find a way.
However, realize that what you've done is basically worthless for any class with more than one variable. For example
Code:
#include <iostream>
class test
{
public:
int getint( ) { return a; }
double getdouble( ) { return b; }
char getchar( ) { return c; }
private:
int a;
double b;
char c;
};
int main( )
{
union
{
test t;
int a;
double b;
char c;
};
a = 100;
b = 1.234;
c = 'x';
std::cout << t.getint( ) << " " << t.getdouble( ) << " " << t.getchar( ) << std::endl;
return 0;
}
on MSVC8, output is "-927712904 -9.25596e+061 ╠"
Pointers are another fun and far more likely way you can break private member protections:
Code:
#include <iostream>
class testptr
{
public:
testptr(int _a) : a(_a) { }
int *getptr( ) { return &a; }
int getvalue( ) { return a; }
private:
int a;
};
int main( )
{
testptr t(1234);
int *ptr = t.getptr( );
*ptr = 8388;
std::cout << t.getvalue( );
return 0;
}
Output is "8388".
Re: private members not so private.
Re: private members not so private.
Quote:
Pointers are another fun and far more likely way you can break private member protections:
if getptr() had const then you wouldn't be able to do this, getptr could do anything it like to class members, in my example, according to c++, nothing should be able to access or modeify the i member once initialized.(because the class has no member functions, static functions, etc.
Re: private members not so private.
Quote:
using visual c++ express
Which version? You know, you should provide the exact program that you tested.
Re: private members not so private.
Re: private members not so private.
Quote:
Originally Posted by staticVoid
if getptr() had const then you wouldn't be able to do this, getptr could do anything it like to class members
So then you just need a cast to get around it. Constant-ness is just another thing that you can skirt around if you really want to.
Re: private members not so private.
You dont have to complicate thing like that to be able to abuse the language.
Try putting a #define private public before including a header and you'll get full access to every private member.
However, doing "smart stuff" like this will neither make you a better coder nor your programs run better. It will only make you sink deeper and deeper into a debugging and a maintainance hell.
Re: private members not so private.
Quote:
Originally Posted by S_M_A
Try putting a #define private public before including a header and you'll get full access to every private member.
I have seen this, for debug mode only, in a professional SDK. :eek:
Re: private members not so private.
Quote:
Originally Posted by Speedo
However, realize that what you've done is basically worthless for any class with more than one variable. For example
Code:
#include <iostream>
class test
{
public:
int getint( ) { return a; }
double getdouble( ) { return b; }
char getchar( ) { return c; }
private:
int a;
double b;
char c;
};
int main( )
{
union
{
test t;
int a;
double b;
char c;
};
a = 100;
b = 1.234;
c = 'x';
std::cout << t.getint( ) << " " << t.getdouble( ) << " " << t.getchar( ) << std::endl;
return 0;
}
For that to work you'd need a struct which has exactly the same data members as the class, and make a union of the struct and the class.
Obviously there is no point in doing this at all, because the class will assume that its data members do not 'magically' change, and so defacto the union will lead to undefined behaviour.
Re: private members not so private.
The thing about private (and protected) members is that they're not there as a challenge. They exist to help the user of the class. I make members private so that coders who are using my classes will suffer fewer problems if I'm forced to make changes. Private is a good thing. Even considering the idea of subverting access control suggests a fundamental lack of understanding of what abstraction, encapsulation and data hiding are all about.
Re: private members not so private.
Quote:
Originally Posted by staticVoid
hi, I just thought i'd post this bit of code showing how you can access private data of a class without using interfaces, I don't know if this is legal in c++ or if it has just been overlooked but anywayz:
Well you didn't access private parts, you raped an object.
It's because of villains like you we now have so called "safe" languages like Java.
Re: private members not so private.
What is conclusion about this topic ? Why this will happen ?