Accessing private members...

B

barcaroller

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

Ian Collins

barcaroller said:
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?

class A
{
int p() { return 0; }
int n;

public:

void pCall( A& a ) { n = a.p(); }
};
 
B

barcaroller

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?

Yes, adding a wrapper would be one solution.
 
P

Paul N

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.

Hope that helps.
Paul.
 
B

barcaroller

Paul N said:
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.
 
R

RedXV

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;
}
};

public:
Example(int exampleX);
Example& operator=(const DataWrapper& data);

DataWrapper p(int x);

int exampleX() { return _exampleX; }

private:
int _exampleX;
};

Example::Example(int exampleX)
{
_exampleX = exampleX;
}

Example::DataWrapper Example::p(int x)
{
return DataWrapper(x);
}

Example& Example::eek:perator=(const DataWrapper& data)
{
_exampleX = data._x;
return *this;
}

#endif

And then there's this bit of code to demonstrate it:
#include <iostream>

using namespace std;

#include "Example.hpp"

class OtherExample
{
public:
// This line creates a compiler error.
// ! void print(const Example::DataWrapper& data) {}
};

int main(int argc, char *argv[])
{
Example test(5), test2(10);

test = test2.p(50);

cout << test.exampleX() << endl;

// One possible pitfall: You can use this method
// to gain access to the public members of the
// private nested class.
int z = test.p(10)._x;

cout << z << endl;

return 0;
}

I'm sure there are ways to improve this, but it's at least something.
Does that help?
 
D

Daniel Pitts

barcaroller said:
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.

public members are visible to any function.
 
B

barcaroller

RedXV said:
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.
 
J

James Kanze

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.
public members are visible to any function.

Not only visible, but accessible.
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top