Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > private bool function

Reply
Thread Tools

private bool function

 
 
2005
Guest
Posts: n/a
 
      10-26-2006
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

 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      10-26-2006
2005 <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Clark S. Cox III
Guest
Posts: n/a
 
      10-26-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-26-2006
"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.


 
Reply With Quote
 
2005
Guest
Posts: n/a
 
      10-26-2006

Marcus Kwok wrote:
> 2005 <(E-Mail Removed)> 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?

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      10-26-2006

2005 wrote:
> Marcus Kwok wrote:
> > 2005 <(E-Mail Removed)> 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.

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      10-26-2006
2005 <(E-Mail Removed)> 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
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      10-26-2006
"2005" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      10-26-2006
"2005" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-26-2006
Daniel T. wrote:
> "2005" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pass bool values to the Python function in embedded python 3.0 BigHand Python 3 03-07-2009 11:21 PM
Replacing a private function an keeping access to private variables Gregor Kofler Javascript 6 06-27-2008 10:24 PM
bool parameter problem in DLL function k.sahici C++ 5 06-18-2007 08:50 AM
Safe bool idiom - protected function not accessible mlimber C++ 14 01-03-2007 05:22 PM
Need standard function to do (Bool and Vector) Weng Tianxiang VHDL 2 06-21-2005 12:01 AM



Advertisments