I have a class that has some members (data and functions) that I do not want to be public. However, I do want these private members to be accessible to objects of the same class.
Example =======
A a, b; // 'a' and 'b' are objects of the same class C c; // 'c' is not
c = a.p(); // No; 'p' is private
b = a.p(); // Okay; 'b' belongs to the same class as 'a'
Is there a way to do this in C++? If the syntax does not allow it, is there a suitable design pattern?
barcaroller wrote: > I have a class that has some members (data and functions) that I do not want > to be public. However, I do want these private members to be accessible to > objects of the same class.
They are.
> Example > =======
> A a, b; // 'a' and 'b' are objects of the same class > C c; // 'c' is not
> c = a.p(); // No; 'p' is private
> b = a.p(); // Okay; 'b' belongs to the same class as 'a'
These are are assignments in whatever scope you are using, not one object of type A accessing another.
Would a simple wrapper do what you want, or are you attempting something else?
>> b = a.p(); // Okay; 'b' belongs to the same class as 'a'
> These are are assignments in whatever scope you are using, not one object > of type A accessing another.
I'm not entirely sure what you mean. I would like the access to the members to be selective. Some objects can see some members while other objects (particularly the ones of the same class) can see others. The member p() above is private and hence the second line should cause a compiler error.
> Would a simple wrapper do what you want, or are you attempting something > else?
> >> b = a.p(); // Okay; 'b' belongs to the same class as 'a'
> > These are are assignments in whatever scope you are using, not one object > > of type A accessing another.
> I'm not entirely sure what you mean.
What he is saying is that the line c = a.p(); calls function p of the object a, and sets c to the result of this. The calling of function p has nothing to do with c, c is just where the result will go once you've got it, so you can't control the access to p by this means.
But in the example you have given, member functions of b can access private members of a, and member functions of c can't. So it should be easy to do what you want. It's just that the syntax you have written above is not the right way to do it.
"Paul N" <gw7...@aol.com> wrote in message: >What he is saying is that the line c = a.p(); calls function p of the >object a, and sets c to the result of this. The calling of function p >has nothing to do with c, c is just where the result will go once >you've got it, so you can't control the access to p by this means. >But in the example you have given, member functions of b can access >private members of a, and member functions of c can't. So it should be >easy to do what you want. It's just that the syntax you have written >above is not the right way to do it.
Yes, I now realize that the simplified syntax I used is wrong. Apologies to you and Ian. I was trying to ask if there was a way to make some members public to some objects but not to others, and in trying to write a simple (artificial) example, I completely fudged it. I will try to formulate this better (or use a real-life example) and create a new post. Thanks for your time.
>I have a class that has some members (data and functions) that I do not want >to be public. However, I do want these private members to be accessible to >objects of the same class.
> Example > =======
> A a, b; // 'a' and 'b' are objects of the same class > C c; // 'c' is not
> c = a.p(); // No; 'p' is private
> b = a.p(); // Okay; 'b' belongs to the same class as 'a'
>Is there a way to do this in C++? If the syntax does not allow it, is there >a suitable design pattern?
I think I know what you're asking. I wrote up a bit of mock code that might demonstrate one way to do this. My idea is to create a nested private class and overload the assignment operator so that you can make this assignment:
> b = a.p(); But not this one: > c = a.p();
This doesn't make A::p() a private member, but you do have two other things going for you. Obviously it can only be called from an A object, and now it's return value can only be accepted by an A object. Here's what I came up with:
#ifndef EXAMPLE_HPP #define EXAMPLE_HPP
class Example { private: // Inner class that is used to pass // data from Example object to Example // object. class DataWrapper { public: int _x; DataWrapper(int x) { _x = x; } };
barcaroller wrote: > "Paul N" <gw7...@aol.com> wrote in message:
>> What he is saying is that the line c = a.p(); calls function p of the >> object a, and sets c to the result of this. The calling of function p >> has nothing to do with c, c is just where the result will go once >> you've got it, so you can't control the access to p by this means.
>> But in the example you have given, member functions of b can access >> private members of a, and member functions of c can't. So it should be >> easy to do what you want. It's just that the syntax you have written >> above is not the right way to do it.
> Yes, I now realize that the simplified syntax I used is wrong. Apologies to > you and Ian. I was trying to ask if there was a way to make some members > public to some objects but not to others, and in trying to write a simple > (artificial) example, I completely fudged it. I will try to formulate this > better (or use a real-life example) and create a new post. Thanks for your > time.
Accessibility is not defined on objects, but on classes and functions. Objects are runtime phenomenon. Accessibility is a compile-time concept.
To clarify:
private members are visible to any function within the same class, or any friend function, or any function in a friend class.
protected members are the same as private, except they can also be accessed by its derivatives.
> I think I know what you're asking. I wrote up a bit of mock code that > might demonstrate one way to do this. My idea is to create a nested > private class and overload the assignment operator so that you can > make this assignment: > b = a.p(); > But not this one: > c = a.p();
Thank you for this example. Yes, it certainly does help. I will look into it a bit more; I will also look at some of the structural design patterns from Gamma et al and see if I can tweak them.
<newsgroup.spamfil...@virtualinfinity.net> wrote: > barcaroller wrote: > > "Paul N" <gw7...@aol.com> wrote in message: > >> What he is saying is that the line c = a.p(); calls > >> function p of the object a, and sets c to the result of > >> this. The calling of function p has nothing to do with c, c > >> is just where the result will go once you've got it, so you > >> can't control the access to p by this means. > >> But in the example you have given, member functions of b > >> can access private members of a, and member functions of c > >> can't. So it should be easy to do what you want. It's just > >> that the syntax you have written above is not the right way > >> to do it. > > Yes, I now realize that the simplified syntax I used is > > wrong. Apologies to you and Ian. I was trying to ask if > > there was a way to make some members public to some objects > > but not to others, and in trying to write a simple > > (artificial) example, I completely fudged it. I will try to > > formulate this better (or use a real-life example) and > > create a new post. Thanks for your time. > Accessibility is not defined on objects, but on classes and > functions. Objects are runtime phenomenon. Accessibility is a > compile-time concept. > To clarify: > private members are visible to any function within the same > class, or any friend function, or any function in a friend > class.
To clarify: private members are "visible" everywhere; using a name (accessing the member) which was declared private is illegal outside of members or friends. It's access control, not visibility control. And the difference is significant---private functions are considered in overload resolution, for example; if the overload resolution chooses the private function, however, the code is in error.
> protected members are the same as private, except they can > also be accessed by its derivatives.
Yes and no. A protected member can be accessed from a derived class only if the expression accessing it accesses the same derived class. Protected only gives Derived access to the Base of Derived, not to the Base of other classes which derive from Base.