[RESOLVED] Access member private class
hi
i have a class so
Code:
class CFood
{
private:
BOOL isnice;
BOOL isbad;
public:
CFood( BOOL a, BOOL b );
void Eat();
};
CFood::CFood( BOOL a, BOOL b )
{
isnice = a;
isbad =b;
}
now another in main:
Code:
int main()
{
CFood food = new CFood( TRUE, FALSE );
}
how access isnice?
my teacher tell me, to get isbad, need to do this
Code:
int main()
{
CFood food = new CFood( TRUE, FALSE );
BOOL isbad = *(BOOL*)(((char*)food)+sizeof(BOOL))
}
now, how to access isnice????
thx
Re: Access member private class
Get a new teacher. The point of private is to prevent access from outside the class. I can't imagine why anybody would teach you stuff like that.
Re: Access member private class
Quote:
Originally Posted by Oper1992
my teacher tell me, to get isbad, need to do this
Err... no. That is a horrible way of doing things. The member variable is private in order to restrict direct access to it. Attempting to circumvent access in that way is nonsense.
Quote:
Originally Posted by Oper1992
how access isnice?
Provide a member function that returns the value of isnice.
By the way, this is likely to result in a compile error:
Code:
CFood food = new CFood( TRUE, FALSE );
It should be:
Code:
CFood food( TRUE, FALSE );
Re: Access member private class
my teacher is wrong or correct?
Re: Access member private class
Quote:
Originally Posted by Oper1992
my teacher is wrong or correct?
Your teacher might be factually correct, i.e., by stating that something can be done, but "morally" wrong, i.e., by teaching you something that should not be done.
Re: Access member private class
Quote:
Originally Posted by
Oper1992
my teacher is wrong or correct?
he is not isnice, but insane. As GCDEF said, get a new teacher.
Code:
class CFood
{
private:
BOOL isnice;
BOOL isbad;
public:
BOOL getIsnice () {return (isnice);}
void setIsnice (BOOL bIsnice) {isnice = bIsnice;}
CFood( BOOL a, BOOL b );
void Eat();
};
Something like this is much better...
What your teacher is showing you is not only morally wrong, but also technically. Your teacher really hasn't understood the idea behind object oriented programming.
Re: Access member private class
Quote:
Originally Posted by
Oper1992
my teacher is wrong or correct?
He's teaching really bad practices. For example, what happens if somebody inserts another variable before isnice? Your code breaks. One of the main design features of object oriented programming is encapsulation, which in part means that users of a class don't need to know about the inner workings of the class. I have no idea why your teacher is teaching you such a hideous hack, but you should NEVER do anything like that in real code. Never.
You should send him to this thread and let him explain himself.
Re: Access member private class
This thread was a joke. Hope you enjoyed it.
Thanks everyone. Resolved :)
Re: [RESOLVED] Access member private class
Quote:
Originally Posted by Oper1992
This thread was a joke. Hope you enjoyed it.
As in your teacher did not actually tell you to do this, if you have such a teacher at all?
Re: [RESOLVED] Access member private class
No such teacher doesn't exist, yet I thought it would be funny to see everyone's reactions about such a teacher teaching bad practices.
Re: [RESOLVED] Access member private class
Quote:
Originally Posted by Oper1992
No such teacher doesn't exist, yet I thought it would be funny to see everyone's reactions about such a teacher teaching bad practices.
You should have read the Acceptable Use Policy:
Quote:
You will not post any Content that is knowingly false, misleading, or inaccurate.
Reported accordingly.
Re: [RESOLVED] Access member private class
Re: [RESOLVED] Access member private class
Quote:
Originally Posted by
Oper1992
No such teacher doesn't exist, yet I thought it would be funny to see everyone's reactions about such a teacher teaching bad practices.
You thought incorrectly.