ويب صور الأخبار مجموعات ترجمة إجابات Gmail المزيد »
المجموعات التي قمت بزيارتها مؤخرا | مساعدة | تسجيل الدخول
الصفحة الرئيسية لمجموعات Google
Accessing private members...
هناك حالياً عدة مواضيع في هذه المجموعة يتم عرضها كأول موضوع. لجعل هذا الموضوع يظهر كأول موضوع، الرجاء إزالة هذا الخيار من المواضيع اﻷخرى.
وقع خطأ في معالجة طلبك. الرجاء حاول مجددا.
تمييز
  9‏ من الرسائل - طي الكل  -  ترجمة الكل إلى اللغة نص مترجم (عرض جميع المصادر الأصلية)
المجموعة التي تقوم بالإرسال إليها هيمجموعة Usenet. الرسائل المرسلة لهذه المجموعة ستجعل بريدك مرئيًا لأي فرد على شبكة الإنترنت
لم يتم إرسال رسالة الرد حتى الآن.
تم النشر الخاص بك بنجاح
 
من:
إلى:
نسخة إلى:
استجابةً إلى:
أضف "نسخة إلى" | أضف "استجابةً إلى" | تحرير الموضوع
الموضوع:
التحقق:
لغرض التحقق من الصحة، يرجى كتابة الحروف التي تراها في الصورة أدناه أو الأرقام التي تسمعها عند النقر فوق رمز وصول ذوي الاحتياجات الخاصة. استمع وإكتب الأرقام التي تسمعها
 
barcaroller  
عرض ملف التعريف   ترجمة إلى اللغة نص مترجم (عرض المحتوى الأصلي)
 خيارات أكثر 8 نوفمبر, 01:00
مجموعات أخبار: comp.lang.c++
من: "barcaroller" <barcarol...@music.net>
التاريخ: Sat, 7 Nov 2009 18:00:30 -0500
محلّي: ‏الأحد 8 نوفمبر 2009 01:00‏
الموضوع: ‏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?


يجب تسجيل الدخول قبل إرسال رسائلك.
لنشر رسالة يجب أولاً أن تنضم إلى هذه المجموعة.
يرجى تحديث اسم الشهرة الخاص بك على صفحة إعدادات الاشتراك قبل النشر.
لا يوجد لديك الإذن المطلوب للنشر.
Ian Collins  
عرض ملف التعريف   ترجمة إلى اللغة نص مترجم (عرض المحتوى الأصلي)
 خيارات أكثر 8 نوفمبر, 04:19
مجموعات أخبار: comp.lang.c++
من: Ian Collins <ian-n...@hotmail.com>
التاريخ: Sun, 08 Nov 2009 15:19:13 +1300
محلّي: ‏الأحد 8 نوفمبر 2009 04:19‏
الموضوع: ‏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

يجب تسجيل الدخول قبل إرسال رسائلك.
لنشر رسالة يجب أولاً أن تنضم إلى هذه المجموعة.
يرجى تحديث اسم الشهرة الخاص بك على صفحة إعدادات الاشتراك قبل النشر.
لا يوجد لديك الإذن المطلوب للنشر.
barcaroller  
عرض ملف التعريف   ترجمة إلى اللغة نص مترجم (عرض المحتوى الأصلي)
 خيارات أكثر 8 نوفمبر, 16:29
مجموعات أخبار: comp.lang.c++
من: "barcaroller" <barcarol...@music.net>
التاريخ: Sun, 8 Nov 2009 09:29:44 -0500
محلّي: ‏الأحد 8 نوفمبر 2009 16:29‏
الموضوع: ‏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.

يجب تسجيل الدخول قبل إرسال رسائلك.
لنشر رسالة يجب أولاً أن تنضم إلى هذه المجموعة.
يرجى تحديث اسم الشهرة الخاص بك على صفحة إعدادات الاشتراك قبل النشر.
لا يوجد لديك الإذن المطلوب للنشر.
Paul N  
عرض ملف التعريف   ترجمة إلى اللغة نص مترجم (عرض المحتوى الأصلي)
 خيارات أكثر 8 نوفمبر, 23:19
مجموعات أخبار: comp.lang.c++
من: Paul N <gw7...@aol.com>
التاريخ: Sun, 8 Nov 2009 13:19:47 -0800 (PST)
محلّي: ‏الأحد 8 نوفمبر 2009 23:19‏
الموضوع: ‏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.


يجب تسجيل الدخول قبل إرسال رسائلك.
لنشر رسالة يجب أولاً أن تنضم إلى هذه المجموعة.
يرجى تحديث اسم الشهرة الخاص بك على صفحة إعدادات الاشتراك قبل النشر.
لا يوجد لديك الإذن المطلوب للنشر.
barcaroller  
عرض ملف التعريف   ترجمة إلى اللغة نص مترجم (عرض المحتوى الأصلي)
 خيارات أكثر 9 نوفمبر, 02:37
مجموعات أخبار: comp.lang.c++
من: "barcaroller" <barcarol...@music.net>
التاريخ: Sun, 8 Nov 2009 19:37:11 -0500
محلّي: ‏الاثنين 9 نوفمبر 2009 02:37‏
الموضوع: ‏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.

يجب تسجيل الدخول قبل إرسال رسائلك.
لنشر رسالة يجب أولاً أن تنضم إلى هذه المجموعة.
يرجى تحديث اسم الشهرة الخاص بك على صفحة إعدادات الاشتراك قبل النشر.
لا يوجد لديك الإذن المطلوب للنشر.
RedXV  
عرض ملف التعريف   ترجمة إلى اللغة نص مترجم (عرض المحتوى الأصلي)
 خيارات أكثر 9 نوفمبر, 20:45
مجموعات أخبار: comp.lang.c++
من: RedXV <jeremy.pridem...@gmail.com>
التاريخ: Mon, 9 Nov 2009 10:45:26 -0800 (PST)
محلّي: ‏الاثنين 9 نوفمبر 2009 20:45‏
الموضوع: ‏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?

يجب تسجيل الدخول قبل إرسال رسائلك.
لنشر رسالة يجب أولاً أن تنضم إلى هذه المجموعة.
يرجى تحديث اسم الشهرة الخاص بك على صفحة إعدادات الاشتراك قبل النشر.
لا يوجد لديك الإذن المطلوب للنشر.
Daniel Pitts  
عرض ملف التعريف   ترجمة إلى اللغة نص مترجم (عرض المحتوى الأصلي)
 خيارات أكثر 10 نوفمبر, 01:09
مجموعات أخبار: comp.lang.c++
من: Daniel Pitts <newsgroup.spamfil...@virtualinfinity.net>
التاريخ: Mon, 09 Nov 2009 15:09:47 -0800
محلّي: ‏الثلاثاء 10 نوفمبر 2009 01:09‏
الموضوع: ‏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.


يجب تسجيل الدخول قبل إرسال رسائلك.
لنشر رسالة يجب أولاً أن تنضم إلى هذه المجموعة.
يرجى تحديث اسم الشهرة الخاص بك على صفحة إعدادات الاشتراك قبل النشر.
لا يوجد لديك الإذن المطلوب للنشر.
barcaroller  
عرض ملف التعريف   ترجمة إلى اللغة نص مترجم (عرض المحتوى الأصلي)
 خيارات أكثر 10 نوفمبر, 02:56
مجموعات أخبار: comp.lang.c++
من: "barcaroller" <barcarol...@music.net>
التاريخ: Mon, 9 Nov 2009 19:56:52 -0500
محلّي: ‏الثلاثاء 10 نوفمبر 2009 02:56‏
الموضوع: ‏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.

يجب تسجيل الدخول قبل إرسال رسائلك.
لنشر رسالة يجب أولاً أن تنضم إلى هذه المجموعة.
يرجى تحديث اسم الشهرة الخاص بك على صفحة إعدادات الاشتراك قبل النشر.
لا يوجد لديك الإذن المطلوب للنشر.
James Kanze  
عرض ملف التعريف   ترجمة إلى اللغة نص مترجم (عرض المحتوى الأصلي)
 خيارات أكثر 10 نوفمبر, 11:20
مجموعات أخبار: comp.lang.c++
من: James Kanze <james.ka...@gmail.com>
التاريخ: Tue, 10 Nov 2009 01:20:29 -0800 (PST)
محلّي: ‏الثلاثاء 10 نوفمبر 2009 11:20‏
الموضوع: ‏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


يجب تسجيل الدخول قبل إرسال رسائلك.
لنشر رسالة يجب أولاً أن تنضم إلى هذه المجموعة.
يرجى تحديث اسم الشهرة الخاص بك على صفحة إعدادات الاشتراك قبل النشر.
لا يوجد لديك الإذن المطلوب للنشر.
نهاية الرسائل
« الرجوع إلى المناقشات « موضوع أحدث     موضوع أقدم »

إنشاء مجموعة - مجموعات Google - صفحة Google الرئيسية - شروط الخدمة - سياسة الخصوصية
©2009 Google