private bool function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 2005

    #1

    private bool function

    Hi

    I have a little trouble with this code.

    There is a calss with

    Class xxx{
    public:
    ----
    ----

    private:
    bool Full () { return (............ true : false) ;}

    };

    I want to call/use the "Full" above in a the main or in a subroutine.

    How do I go about?

    Thanks

  • Marcus Kwok

    #2
    Re: private bool function

    2005 <uws2003@yahoo. comwrote:
    There is a calss with
    >
    Class xxx{
    public:
    ----
    ----
    >
    private:
    bool Full () { return (............ true : false) ;}
    >
    };
    >
    I want to call/use the "Full" above in a the main or in a subroutine.
    >
    How do I go about?
    Since Full() is private, you can only call it from within class xxx. If
    you want to call it from outside of the class, you need to make
    something a friend of class xxx.

    Also, when posting code, please post real C++ code.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply

    Comment

    • Clark S. Cox III

      #3
      Re: private bool function

      2005 wrote:
      Hi
      >
      I have a little trouble with this code.
      >
      There is a calss with
      >
      Class xxx{
      public:
      ----
      ----
      >
      private:
      bool Full () { return (............ true : false) ;}
      >
      };
      >
      I want to call/use the "Full" above in a the main or in a subroutine.
      >
      How do I go about?
      >
      Thanks
      >
      You can't. That is the point of "private:".

      --
      Clark S. Cox III
      clarkcox3@gmail .com

      Comment

      • osmium

        #4
        Re: private bool function

        "2005" wrote:
        I have a little trouble with this code.
        >
        There is a calss with
        >
        Class xxx{
        public:
        ----
        ----
        >
        private:
        bool Full () { return (............ true : false) ;}
        >
        };
        >
        I want to call/use the "Full" above in a the main or in a subroutine.
        >
        How do I go about?
        You don't. When you said it was private you actually meant it was private
        so main (a non-member function) can not access it.

        It is just possible there may be some convoluted way; the designers make a
        point of saying the intent was to deter accidental misuse, not malicious,
        intentional spying. But that is clearly not what you want.


        Comment

        • 2005

          #5
          Re: private bool function


          Marcus Kwok wrote:
          2005 <uws2003@yahoo. comwrote:
          There is a calss with

          Class xxx{
          public:
          ----
          ----

          private:
          bool Full () { return (............ true : false) ;}

          };

          I want to call/use the "Full" above in a the main or in a subroutine.

          How do I go about?
          >
          Since Full() is private, you can only call it from within class xxx. If
          you want to call it from outside of the class, you need to make
          something a friend of class xxx.
          >
          Also, when posting code, please post real C++ code.
          >
          #define MAXSIZE 5
          class CAlley {
          public:
          CAlley () : m_pTop(0), mSize(0), mMaxSize(MAXSIZ E) { }
          ~CAlley () {}
          int Park(int); // park a car

          void Retrieve(int, CAlley *); // retrieve a car
          void Terminate(); // quit the program
          void Display(char *); // display contents af alley

          private:
          void SetTop(CarNode *p){m_pTop=p;} // assign top pointer

          // check if stack is empty
          bool Empty(){return ((mSize==0) ? true : false);}

          // check if stack is full
          bool Full() {return ((mSize==MAXSIZ E) ? true : false);}

          int Push(CarNode *); // push one node onto top of stack

          // CarNode * Pop(); // pop one node from the top of stack

          CarNode *m_pTop; // pointer to top of Allay (stack)
          int mSize; // number of nodes in Allay (stack)
          int mMaxSize; //max number of nodes in Allay (stack)
          };

          What should be the syntax to access the Fulleither in main and in
          function?

          Comment

          • Salt_Peter

            #6
            Re: private bool function


            2005 wrote:
            Marcus Kwok wrote:
            2005 <uws2003@yahoo. comwrote:
            There is a calss with
            >
            Class xxx{
            public:
            ----
            ----
            >
            private:
            bool Full () { return (............ true : false) ;}
            >
            };
            >
            I want to call/use the "Full" above in a the main or in a subroutine.
            >
            How do I go about?
            Since Full() is private, you can only call it from within class xxx. If
            you want to call it from outside of the class, you need to make
            something a friend of class xxx.

            Also, when posting code, please post real C++ code.
            #define MAXSIZE 5
            class CAlley {
            public:
            CAlley () : m_pTop(0), mSize(0), mMaxSize(MAXSIZ E) { }
            ~CAlley () {}
            int Park(int); // park a car
            >
            void Retrieve(int, CAlley *); // retrieve a car
            void Terminate(); // quit the program
            void Display(char *); // display contents af alley
            >
            private:
            void SetTop(CarNode *p){m_pTop=p;} // assign top pointer
            >
            // check if stack is empty
            bool Empty(){return ((mSize==0) ? true : false);}
            >
            // check if stack is full
            bool Full() {return ((mSize==MAXSIZ E) ? true : false);}
            >
            int Push(CarNode *); // push one node onto top of stack
            >
            // CarNode * Pop(); // pop one node from the top of stack
            >
            CarNode *m_pTop; // pointer to top of Allay (stack)
            int mSize; // number of nodes in Allay (stack)
            int mMaxSize; //max number of nodes in Allay (stack)
            };
            >
            What should be the syntax to access the Fulleither in main and in
            function?
            You could declare the function (any function) as a friend in CAlley.
            But thats a really dumb thing to do.

            Function Full() is private for a reason. Whats preventing you from
            adding a public member function like is_Full()?

            const int MAXSIZE(5);

            class CAlley
            {
            ... // members
            public:
            ...
            bool is_Full() { return Full(); }
            private:
            ...
            bool Full() {return ((mSize==MAXSIZ E) ? true : false);}
            };

            Encapsulation is not meant to be broken or bypassed, its there to be
            respected.
            Consider what happens if you make some function foo() a friend of
            CAlley.
            What if some smartass modifies the function (which has complete,
            unrestricted access to CAlley) to change a critical private part in an
            instance of CAlley?
            Guess what? Its not the fault of the smartass - its your fault. Period.

            Comment

            • Marcus Kwok

              #7
              Re: private bool function

              2005 <uws2003@yahoo. comwrote:
              #define MAXSIZE 5
              class CAlley {
              public:
              CAlley () : m_pTop(0), mSize(0), mMaxSize(MAXSIZ E) { }
              ~CAlley () {}
              int Park(int); // park a car
              >
              void Retrieve(int, CAlley *); // retrieve a car
              void Terminate(); // quit the program
              void Display(char *); // display contents af alley
              >
              private:
              void SetTop(CarNode *p){m_pTop=p;} // assign top pointer
              >
              // check if stack is empty
              bool Empty(){return ((mSize==0) ? true : false);}
              >
              // check if stack is full
              bool Full() {return ((mSize==MAXSIZ E) ? true : false);}
              >
              int Push(CarNode *); // push one node onto top of stack
              >
              // CarNode * Pop(); // pop one node from the top of stack
              >
              CarNode *m_pTop; // pointer to top of Allay (stack)
              int mSize; // number of nodes in Allay (stack)
              int mMaxSize; //max number of nodes in Allay (stack)
              };
              >
              What should be the syntax to access the Fulleither in main and in
              function?
              You can make them friends of CAlley:

              // e.g.,
              int function(bool);

              class CAlley {
              // all the stuff you already have, but add

              friend int function(bool);

              friend int main(); // if you are not using command-line parameters
              friend int main(int argc, char* argv[]); // if you are
              };

              Note, that if you do this, then function() and main() will have access
              to ALL private and protected parts of CAlley, which may or may not be
              what you really want to do.

              Another way is to move Full() to the public section of CAlley, or to
              create a new public function that calls the private version of Full().

              --
              Marcus Kwok
              Replace 'invalid' with 'net' to reply

              Comment

              • Daniel T.

                #8
                Re: private bool function

                "2005" <uws2003@yahoo. comwrote:
                I have a little trouble with this code.
                >
                There is a calss with
                >
                Class xxx{
                public:
                ----
                ----
                >
                private:
                bool Full () { return (............ true : false) ;}
                >
                };
                >
                I want to call/use the "Full" above in a the main or in a subroutine.
                >
                How do I go about?
                For whatever reason, the author of the xxx class doesn't think that
                function is safe to use outside of the class. You should contact the
                author of the class and ask him about it.

                --
                To send me email, put "sheltie" in the subject.

                Comment

                • Daniel T.

                  #9
                  Re: private bool function

                  "2005" <uws2003@yahoo. comwrote:
                  #define MAXSIZE 5
                  class CAlley {
                  public:
                  CAlley () : m_pTop(0), mSize(0), mMaxSize(MAXSIZ E) { }
                  ~CAlley () {}
                  int Park(int); // park a car
                  >
                  void Retrieve(int, CAlley *); // retrieve a car
                  void Terminate(); // quit the program
                  void Display(char *); // display contents af alley
                  >
                  private:
                  void SetTop(CarNode *p){m_pTop=p;} // assign top pointer
                  >
                  // check if stack is empty
                  bool Empty(){return ((mSize==0) ? true : false);}
                  >
                  // check if stack is full
                  bool Full() {return ((mSize==MAXSIZ E) ? true : false);}
                  >
                  int Push(CarNode *); // push one node onto top of stack
                  >
                  // CarNode * Pop(); // pop one node from the top of stack
                  >
                  CarNode *m_pTop; // pointer to top of Allay (stack)
                  int mSize; // number of nodes in Allay (stack)
                  int mMaxSize; //max number of nodes in Allay (stack)
                  };
                  >
                  What should be the syntax to access the Fulleither in main and in
                  function?
                  Again, if CAlley is your class and you are allowed to make changes and
                  you don't think it would break anything, simply make the "Full()"
                  function public.

                  --
                  To send me email, put "sheltie" in the subject.

                  Comment

                  • Victor Bazarov

                    #10
                    Re: private bool function

                    Daniel T. wrote:
                    "2005" <uws2003@yahoo. comwrote:
                    >
                    >#define MAXSIZE 5
                    >class CAlley {
                    >[..]
                    >>
                    >// check if stack is empty
                    >bool Empty(){return ((mSize==0) ? true : false);}
                    >>
                    >// check if stack is full
                    >bool Full() {return ((mSize==MAXSIZ E) ? true : false);}
                    >[..]
                    >};
                    >>
                    >What should be the syntax to access the Fulleither in main and in
                    >function?
                    >
                    Again, if CAlley is your class and you are allowed to make changes and
                    you don't think it would break anything, simply make the "Full()"
                    function public.
                    Not to mention that both 'Empty' and 'Full' members should be declared
                    'const':

                    bool Empty() const { ...
                    bool Full() const { ...

                    V
                    --
                    Please remove capital 'A's when replying by e-mail
                    I do not respond to top-posted replies, please don't ask


                    Comment

                    Working...