A teaser
-
Hi all, Consider this class
(unit1. h)
class MyClass{ public: MyClass(){}; MyClass(int nNum): m_nNum(nNum){} ~MyClass(){}; private: int m_nNum; };
in the main program
#include"unit1.h" int main() { MyClass myObject(300); }
The question how many ways to modify the property (myObject.m_nNum) to another number without using setters or friends?
-
Viorel. wrote:
You can add to the total number of ways the following unusual one:
Good, I want more.
-
Viorel. wrote:
You can add to the total number of ways the following unusual one:
Good, I want more.
i hope you understand how bad the answers to you question can be, because of bad programming practices... this is the only reason why i didn't answered you with such code sample, because i advise you seriously to never use such samples in your code... security reason is always the best ;):cool:
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
Hi all, Consider this class
(unit1. h)
class MyClass{ public: MyClass(){}; MyClass(int nNum): m_nNum(nNum){} ~MyClass(){}; private: int m_nNum; };
in the main program
#include"unit1.h" int main() { MyClass myObject(300); }
The question how many ways to modify the property (myObject.m_nNum) to another number without using setters or friends?
If anybody thinks of anything more horrible than this, let me know ;P
#define private public
#include"unit1.h"
int main()
{
MyClass myObject(300);
myObject.m_nNum = 400;
}
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Hi all, Consider this class
(unit1. h)
class MyClass{ public: MyClass(){}; MyClass(int nNum): m_nNum(nNum){} ~MyClass(){}; private: int m_nNum; };
in the main program
#include"unit1.h" int main() { MyClass myObject(300); }
The question how many ways to modify the property (myObject.m_nNum) to another number without using setters or friends?
And now to give a serious answer:
#include"unit1.h"
int main()
{
MyClass myObject(300);
MyClass temp(400);
myObject = temp;
}
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
A similar unusual way is:
int x = 400; memcpy(&myObject, &x, sizeof(x));
One more way:
class MyClass2 { public: int m_nNum; }; // // in main: // MyClass2 & myObject2 = (MyClass2&)myObject; myObject2.m_nNum = 400;
Viorel. wrote:
public: int m_nNum; }; // // in main: // MyClass2 & myObject2 = (MyClass2&)myObject; myObject2.m_nNum = 400;
I will prefer get and set function rather that public variable outside the class
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
Im not sure my answer is correct or not. we can change the attribute value by following methods 1. Set value inside the destructor 2. Through a Member pointer SaRath.
"Don't Do Different things... Do Things Differently..."SaRath C wrote:
1. Set value inside the destructor
If i set the value inside the destrutor, then how that value will be persist!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
SaRath C wrote:
1. Set value inside the destructor
If i set the value inside the destrutor, then how that value will be persist!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
ThatsAlok wrote:
If i set the value inside the destrutor, then how that value will be persist!
actually, a destructor is a function like another. the only implicit call with it is when the object looses focus. but it is exactly like when some tries to
delete this
in the constructor. it is not really the appropriate place for that. a ctor is mostly to initialize the object state, and the dtor is mostly for releasing memoring that could have been allocated in the object life ; no more, no less, but not the opposite ! :doh:
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
If anybody thinks of anything more horrible than this, let me know ;P
#define private public
#include"unit1.h"
int main()
{
MyClass myObject(300);
myObject.m_nNum = 400;
}
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
Nemanja Trifunovic wrote:
#define private public
ahemm, i'm not really sure about the level of this guy (and all the other around, reading), so is it really a good thing to point out that horror ? working though ! lol
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
ThatsAlok wrote:
If i set the value inside the destrutor, then how that value will be persist!
actually, a destructor is a function like another. the only implicit call with it is when the object looses focus. but it is exactly like when some tries to
delete this
in the constructor. it is not really the appropriate place for that. a ctor is mostly to initialize the object state, and the dtor is mostly for releasing memoring that could have been allocated in the object life ; no more, no less, but not the opposite ! :doh:
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
toxcct wrote:
the only implicit call with it is when the object looses focus.
Rather says Losses focus pernamentaly!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
Hi all, Consider this class
(unit1. h)
class MyClass{ public: MyClass(){}; MyClass(int nNum): m_nNum(nNum){} ~MyClass(){}; private: int m_nNum; };
in the main program
#include"unit1.h" int main() { MyClass myObject(300); }
The question how many ways to modify the property (myObject.m_nNum) to another number without using setters or friends?
:mad:Hi !!! Can i ask you something? Why do you want to access private members an other way than getters & setters. For what do you use an class if you want that the members are visible for everybody. I thing you should use getters & setters (if you use classes) to modify a member-variables of the class. Don't use friend accessor. By !!! -:KNOX:-
-
:mad:Hi !!! Can i ask you something? Why do you want to access private members an other way than getters & setters. For what do you use an class if you want that the members are visible for everybody. I thing you should use getters & setters (if you use classes) to modify a member-variables of the class. Don't use friend accessor. By !!! -:KNOX:-
hi knox, what you said was justified from your point of view i believe. however, you're still young (in the sense that you still have to learn about others), and shouldn't get angry just because you don't understand the OP point of interrest. there are other reason we access to private members avoiding getters/setters/friends that you certainly didn't have in mind when answered. also, don't be so categorical when saying not to use friend accessor. it is very useful in some particuliar case, and Mr. Stroustrup was far from being stupid when he defined this keywork in the C++ language definition... sincerely,
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]