Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Accessing private members...
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
barcaroller  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 23:00
Newsgroups: comp.lang.c++
From: "barcaroller" <barcarol...@music.net>
Date: Sat, 7 Nov 2009 18:00:30 -0500
Local: Sat 7 Nov 2009 23:00
Subject: Accessing private members...

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?


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Collins  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 02:19
Newsgroups: comp.lang.c++
From: Ian Collins <ian-n...@hotmail.com>
Date: Sun, 08 Nov 2009 15:19:13 +1300
Local: Sun 8 Nov 2009 02:19
Subject: Re: Accessing private members...

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?

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

public:

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

};

--
Ian Collins

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
barcaroller  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 14:29
Newsgroups: comp.lang.c++
From: "barcaroller" <barcarol...@music.net>
Date: Sun, 8 Nov 2009 09:29:44 -0500
Local: Sun 8 Nov 2009 14:29
Subject: Re: Accessing private members...

"Ian Collins" <ian-n...@hotmail.com> wrote in message

news:7lmo52F3d89cgU3@mid.individual.net...

>>     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.

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.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul N  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 21:19
Newsgroups: comp.lang.c++
From: Paul N <gw7...@aol.com>
Date: Sun, 8 Nov 2009 13:19:47 -0800 (PST)
Local: Sun 8 Nov 2009 21:19
Subject: Re: Accessing private members...
On 8 Nov, 14:29, "barcaroller" <barcarol...@music.net> wrote:

> "Ian Collins" <ian-n...@hotmail.com> wrote in message

> news:7lmo52F3d89cgU3@mid.individual.net...

> >>     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.

> 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.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
barcaroller  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 00:37
Newsgroups: comp.lang.c++
From: "barcaroller" <barcarol...@music.net>
Date: Sun, 8 Nov 2009 19:37:11 -0500
Local: Mon 9 Nov 2009 00:37
Subject: Re: Accessing private members...

"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.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RedXV  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 18:45
Newsgroups: comp.lang.c++
From: RedXV <jeremy.pridem...@gmail.com>
Date: Mon, 9 Nov 2009 10:45:26 -0800 (PST)
Local: Mon 9 Nov 2009 18:45
Subject: Re: Accessing private members...
On Nov 7, 4:00 pm, "barcaroller" <barcarol...@music.net> wrote:

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::operator=(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?

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Pitts  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 23:09
Newsgroups: comp.lang.c++
From: Daniel Pitts <newsgroup.spamfil...@virtualinfinity.net>
Date: Mon, 09 Nov 2009 15:09:47 -0800
Local: Mon 9 Nov 2009 23:09
Subject: Re: Accessing private members...

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.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
barcaroller  
View profile   Translate to Translated (View Original)
 More options 10 Nov, 00:56
Newsgroups: comp.lang.c++
From: "barcaroller" <barcarol...@music.net>
Date: Mon, 9 Nov 2009 19:56:52 -0500
Local: Tues 10 Nov 2009 00:56
Subject: Re: Accessing private members...

"RedXV" <jeremy.pridem...@gmail.com> wrote ...

> 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.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kanze  
View profile   Translate to Translated (View Original)
 More options 10 Nov, 09:20
Newsgroups: comp.lang.c++
From: James Kanze <james.ka...@gmail.com>
Date: Tue, 10 Nov 2009 01:20:29 -0800 (PST)
Local: Tues 10 Nov 2009 09:20
Subject: Re: Accessing private members...
On Nov 9, 11:09 pm, Daniel Pitts

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.

--
James Kanze


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google