473,383 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

Oct 26 '06 #1
9 4575
2005 <uw*****@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
Oct 26 '06 #2
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
cl*******@gmail.com
Oct 26 '06 #3
"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.
Oct 26 '06 #4

Marcus Kwok wrote:
2005 <uw*****@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(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?

Oct 26 '06 #5

2005 wrote:
Marcus Kwok wrote:
2005 <uw*****@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(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.

Oct 26 '06 #6
2005 <uw*****@yahoo.comwrote:
#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
Oct 26 '06 #7
"2005" <uw*****@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.
Oct 26 '06 #8
"2005" <uw*****@yahoo.comwrote:
#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.
Oct 26 '06 #9
Daniel T. wrote:
"2005" <uw*****@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==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
Oct 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
5
by: Aakash Joshi | last post by:
is there any way we can access private member function of a class from outside. i know we can access private member data using void pointer
1
by: Edward | last post by:
I have trouble with some of the concepts of OOP, and am struggling currently with Private Shared Functions. I think I understand Private (not available outside the class). I think I understand...
4
by: sun1991 | last post by:
#include <iostream> using namespace std; class Base{ public: void ToString(){ ToStringCore(); } private: virtual void ToStringCore(){
9
by: Mike | last post by:
Hi, Just a simple question: why the compiler doesn't report error when accessing a private member function inside a function having template type ? For example: #include<iostream> using...
2
by: rajasekaranrajasekaran | last post by:
hi,,, i am having a scenario plz help me to find a soln,, its really challenging for me hope it is useful 4 u 2.... I need to access a private member function witout using a friend, virtual and...
7
by: PengYu.UT | last post by:
Hi, I want to write a test function to test a class. The class has a private member function that need to be tested. Although I could modify the member function as protected or public, I do not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.