private bool function

Discussion in 'C++' started by 2005, Oct 26, 2006.

  1. 2005

    2005 Guest

    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
    2005, Oct 26, 2006
    #1
    1. Advertising

  2. 2005

    Marcus Kwok Guest

    2005 <> wrote:
    > 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
    Marcus Kwok, Oct 26, 2006
    #2
    1. Advertising

  3. 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
    Clark S. Cox III, Oct 26, 2006
    #3
  4. 2005

    osmium Guest

    "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.
    osmium, Oct 26, 2006
    #4
  5. 2005

    2005 Guest

    Marcus Kwok wrote:
    > 2005 <> wrote:
    > > 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(MAXSIZE) { }
    ~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==MAXSIZE) ? 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?
    2005, Oct 26, 2006
    #5
  6. 2005

    Salt_Peter Guest

    2005 wrote:
    > Marcus Kwok wrote:
    > > 2005 <> wrote:
    > > > 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(MAXSIZE) { }
    > ~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==MAXSIZE) ? 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==MAXSIZE) ? 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.
    Salt_Peter, Oct 26, 2006
    #6
  7. 2005

    Marcus Kwok Guest

    2005 <> wrote:
    > #define MAXSIZE 5
    > class CAlley {
    > public:
    > CAlley () : m_pTop(0), mSize(0), mMaxSize(MAXSIZE) { }
    > ~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==MAXSIZE) ? 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
    Marcus Kwok, Oct 26, 2006
    #7
  8. 2005

    Daniel T. Guest

    "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?


    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.
    Daniel T., Oct 26, 2006
    #8
  9. 2005

    Daniel T. Guest

    "2005" <> wrote:

    > #define MAXSIZE 5
    > class CAlley {
    > public:
    > CAlley () : m_pTop(0), mSize(0), mMaxSize(MAXSIZE) { }
    > ~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==MAXSIZE) ? 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.
    Daniel T., Oct 26, 2006
    #9
  10. Daniel T. wrote:
    > "2005" <> wrote:
    >
    >> #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==MAXSIZE) ? 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
    Victor Bazarov, Oct 26, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    19
    Views:
    786
    Daniel T.
    Feb 4, 2004
  2. DaveLessnau
    Replies:
    3
    Views:
    424
    Howard
    May 16, 2005
  3. Jeremy Bowers
    Replies:
    3
    Views:
    510
    Steven Bethard
    Jan 24, 2005
  4. Daniel Finnie
    Replies:
    3
    Views:
    181
    Logan Capaldo
    Dec 16, 2006
  5. Gregor Kofler
    Replies:
    6
    Views:
    209
    Gregor Kofler
    Jun 27, 2008
Loading...

Share This Page