-
October 22nd, 2010, 05:46 AM
#1
[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
-
October 22nd, 2010, 05:52 AM
#2
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.
-
October 22nd, 2010, 05:52 AM
#3
Re: Access member private class
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.
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 );
-
October 22nd, 2010, 06:07 AM
#4
Re: Access member private class
my teacher is wrong or correct?
-
October 22nd, 2010, 06:16 AM
#5
Re: Access member private class
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.
-
October 22nd, 2010, 06:30 AM
#6
Re: Access member private class
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.
Last edited by Skizmo; October 22nd, 2010 at 06:32 AM.
-
October 22nd, 2010, 07:14 AM
#7
Re: Access member private class
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.
-
October 22nd, 2010, 07:57 AM
#8
Re: Access member private class
This thread was a joke. Hope you enjoyed it.
Thanks everyone. Resolved
-
October 22nd, 2010, 08:02 AM
#9
Re: [RESOLVED] Access member private class
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?
-
October 22nd, 2010, 08:04 AM
#10
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.
-
October 22nd, 2010, 08:09 AM
#11
Re: [RESOLVED] Access member private class
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:
You will not post any Content that is knowingly false, misleading, or inaccurate.
Reported accordingly.
-
October 22nd, 2010, 08:34 AM
#12
Re: [RESOLVED] Access member private class
Last edited by Oper1992; October 22nd, 2010 at 08:43 AM.
-
October 22nd, 2010, 12:15 PM
#13
Re: [RESOLVED] Access member private class
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|