Is this a Bug or What?
-
Hello All !! I am using visual studio 6.0 I tried to access the private class member outside the class using #define macro. here is the code. I was able to access private member outside the class? why did this work? This should be allowed or not?
#define private public class A { private: int i; public: A() { i = 44; } }; int main() { A obA; cout<<"\ni = "<
sanket
-
Hello All !! I am using visual studio 6.0 I tried to access the private class member outside the class using #define macro. here is the code. I was able to access private member outside the class? why did this work? This should be allowed or not?
#define private public class A { private: int i; public: A() { i = 44; } }; int main() { A obA; cout<<"\ni = "<
sanket
Defnitely It is not a bug. Because Pre-processing happens before the compilation process. So first the pre-processor would process your #define and replaces the text "private" with the text "public", and then it would be processed by the compiler. Cheers
-
Defnitely It is not a bug. Because Pre-processing happens before the compilation process. So first the pre-processor would process your #define and replaces the text "private" with the text "public", and then it would be processed by the compiler. Cheers
Thanks!! but dont you think this should not be allowed??
sanket
-
Thanks!! but dont you think this should not be allowed??
sanket
It is the responsibility of the developer not to use such constructs. Cheers
-
Hello All !! I am using visual studio 6.0 I tried to access the private class member outside the class using #define macro. here is the code. I was able to access private member outside the class? why did this work? This should be allowed or not?
#define private public class A { private: int i; public: A() { i = 44; } }; int main() { A obA; cout<<"\ni = "<
sanket
sanket.patel wrote:
#define private public
sanket.patel wrote:
I was able to access private member outside the class? why did this work?
of course you could !! you told it (with the #define) to replace each private keyword occurence by public... so that every private member is actually public. why on earth where you trying to do with such horror ?! :wtf:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Thanks!! but dont you think this should not be allowed??
sanket
sanket.patel wrote:
but dont you think this should not be allowed??
play VB if you're age to play with a plastic hammer. if you code in C/C++, know that you can break much more than that. don't code what you can regret later...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
sanket.patel wrote:
#define private public
sanket.patel wrote:
I was able to access private member outside the class? why did this work?
of course you could !! you told it (with the #define) to replace each private keyword occurence by public... so that every private member is actually public. why on earth where you trying to do with such horror ?! :wtf:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Actually !! I was asked to find the ways to access private members outside the class? so one method is using friends.... and i thought of this as the other way... Thanks,
sanket
-
Actually !! I was asked to find the ways to access private members outside the class? so one method is using friends.... and i thought of this as the other way... Thanks,
sanket
sanket.patel wrote:
I was asked to find the ways to access private members outside the class
and you chose the worst... if members are private, it's to deliberately hide them from the outside. look. if you have an object engine, do you know how it works internally ? certainly not. you only see the apis (accelerator, starter, etc...). anyway, have you thought to members accessors (getters, setters) ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
sanket.patel wrote:
I was asked to find the ways to access private members outside the class
and you chose the worst... if members are private, it's to deliberately hide them from the outside. look. if you have an object engine, do you know how it works internally ? certainly not. you only see the apis (accelerator, starter, etc...). anyway, have you thought to members accessors (getters, setters) ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Thank you very much! yeah I know about getters and setters and I know I choose the worst! but the the question is about that only.. i.e. the unusual ways to access the private data. Thanks,
sanket
-
Thanks!! but dont you think this should not be allowed??
sanket
VB and C# are languages written to cater to the lowest common denominator. C++ assumes a level of skill in the developer. #define is a hangover from C, which C++ is derived from. The designer of C++ would prefer it was not there, but it's still true that his design philosophy is to give the language powerful features and assume his users know how to use them.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hello All !! I am using visual studio 6.0 I tried to access the private class member outside the class using #define macro. here is the code. I was able to access private member outside the class? why did this work? This should be allowed or not?
#define private public class A { private: int i; public: A() { i = 44; } }; int main() { A obA; cout<<"\ni = "<
sanket
GIGO - if you pull stunts like that, don't be surprised when you get odd results.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?
-
Hello All !! I am using visual studio 6.0 I tried to access the private class member outside the class using #define macro. here is the code. I was able to access private member outside the class? why did this work? This should be allowed or not?
#define private public class A { private: int i; public: A() { i = 44; } }; int main() { A obA; cout<<"\ni = "<
sanket
sanket.patel wrote:
#define private public
My opinion is that you will *EXCEL* in the "Coding Horros" forum. Well, what are you waiting for? Post some of your fantastic code there! :|
Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா
-
Hello All !! I am using visual studio 6.0 I tried to access the private class member outside the class using #define macro. here is the code. I was able to access private member outside the class? why did this work? This should be allowed or not?
#define private public class A { private: int i; public: A() { i = 44; } }; int main() { A obA; cout<<"\ni = "<
sanket
To make the most of your coding experience, you probably should try: To save testing:
#define true false
this always happens anyway:#define this NULL
to avoid accidental deletion#define delete /##/
avoid wasting loop iterations:#define break continue
just in case the others are boring:#define try do #define catch while
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
-
VB and C# are languages written to cater to the lowest common denominator. C++ assumes a level of skill in the developer. #define is a hangover from C, which C++ is derived from. The designer of C++ would prefer it was not there, but it's still true that his design philosophy is to give the language powerful features and assume his users know how to use them.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Christian Graus wrote:
#define is a hangover from C, which C++ is derived from. The designer of C++ would prefer it was not there, but it's still true that his design philosophy is to give the language powerful features and assume his users know how to use them.
A hangover, maybe, but it is o so powerful. But with every powerful it should be used with care. #define is a dangerous tool in the hands of the unexperienced.:-D
codito ergo sum
-
VB and C# are languages written to cater to the lowest common denominator. C++ assumes a level of skill in the developer. #define is a hangover from C, which C++ is derived from. The designer of C++ would prefer it was not there, but it's still true that his design philosophy is to give the language powerful features and assume his users know how to use them.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
You got sig-ed. 'nce again. :-D
VB and C# are languages written to cater to the lowest common denominator. C++ assumes a level of skill in the developer. - Christian Graus
-
Christian Graus wrote:
#define is a hangover from C, which C++ is derived from. The designer of C++ would prefer it was not there, but it's still true that his design philosophy is to give the language powerful features and assume his users know how to use them.
A hangover, maybe, but it is o so powerful. But with every powerful it should be used with care. #define is a dangerous tool in the hands of the unexperienced.:-D
codito ergo sum
BadKarma wrote:
hangover, maybe, but it is o so powerful.
I agree with Stroustrup. I try to avoid using it whever I can, I think it's a poor tool. But, it is powerful, and no doubt can be used effectively at times.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
To make the most of your coding experience, you probably should try: To save testing:
#define true false
this always happens anyway:#define this NULL
to avoid accidental deletion#define delete /##/
avoid wasting loop iterations:#define break continue
just in case the others are boring:#define try do #define catch while
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
EXCELLENT! These fixed a bunch of my problems. Thanks!
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder