private bool function

2

2005

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
 
M

Marcus Kwok

2005 said:
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.
 
C

Clark S. Cox III

2005 said:
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:".
 
O

osmium

2005 said:
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.
 
2

2005

Marcus said:
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?
 
S

Salt_Peter

2005 said:
#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.
 
M

Marcus Kwok

2005 said:
#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().
 
D

Daniel T.

2005 said:
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.
 
D

Daniel T.

2005 said:
#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.
 
V

Victor Bazarov

Daniel said:
2005 said:
#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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top