[RESOLVED] Access member private class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: [RESOLVED] Access member private class

  1. #1
    Join Date
    Oct 2010
    Posts
    6

    [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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,090

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    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 );
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Oct 2010
    Posts
    6

    Re: Access member private class

    my teacher is wrong or correct?

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Access member private class

    Quote Originally Posted by Oper1992 View Post
    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.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,090

    Re: Access member private class

    Quote Originally Posted by Oper1992 View Post
    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.

  8. #8
    Join Date
    Oct 2010
    Posts
    6

    Re: Access member private class

    This thread was a joke. Hope you enjoyed it.

    Thanks everyone. Resolved

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Oct 2010
    Posts
    6

    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.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    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:
    You will not post any Content that is knowingly false, misleading, or inaccurate.
    Reported accordingly.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Oct 2010
    Posts
    6

    Re: [RESOLVED] Access member private class

    tell-tale
    Last edited by Oper1992; October 22nd, 2010 at 08:43 AM.

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,090

    Re: [RESOLVED] Access member private class

    Quote Originally Posted by Oper1992 View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center