Topic: Cpp Inheritance = Incest?


Author: "Anthony Clay" <zarthrag@softhome.net>
Date: 1999/01/13
Raw View
Hello,

    My name is Anthony and here is my dillema:

    While writing my own array class in standard ANSI C++ I came across an
intersting question.  I understand that two different classes can declare to
be friends with each other, can I class declare itself a friend.

    In my class each node in the array has pointers that point to the next
and previous elements in the array.  However, when constructing and
'stringing' the seperate nodes together, I want to this object to have
access to the private members of another member of the same class or
whatever.

    This should allow me to initalize the entire array while staying within
the constructor, without having to use iteration.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1999/01/14
Raw View
In article <77h73o$asm@bgtnsc03.worldnet.att.net>,
  "Anthony Clay" <zarthrag@softhome.net> wrote:
> Hello,
Hi!

>     My name is Anthony and here is my dillema:
>
>     While writing my own array class in standard ANSI C++ I came across an
> intersting question.  I understand that two different classes can declare to
> be friends with each other, can I class declare itself a friend.

A class already has access to all members of itself in other objects.
The exception is for base class members when the base is not public.

>     In my class each node in the array has pointers that point to the next
> and previous elements in the array.  However, when constructing and
> 'stringing' the seperate nodes together, I want to this object to have
> access to the private members of another member of the same class or
> whatever.

    class LinkData {
        LinkData *next;
        LinkData *prev;
        char data[64];

    // ...
    public:
        void Insert(LinkData *before) {
            assert(!next && !prev); // Can't insert if already on the list!
            next = before->next;
            prev = before;
            if (next) next->prev = this;
            prev->next = this;
        }
    };

Note that we didn't need any "friend" declaration to access
before->next or next->prev.

>     This should allow me to initalize the entire array while staying within
> the constructor, without having to use iteration.

I don't see how -- unless you mean that you're declaring a C-array
of some class, and you want the constructor to deal with other
array elements. This probably isn't a good idea, because then that
one array would be the only safe place to instanciate the class.

    class MyData {
        int i;
    public:
        MyData();
    }
    MyData myArray[100]; // Initialized with values 0..99
    MyData::MyData() {
        if (myArray == this) i=0;
        else i = 1 + (this-1).i;
    }

myArray probably initializes okay, but
    MyData x;
either initializes x.i to gibberish, or else it reads from
an invalid address and crashes.

---
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1999/01/14
Raw View
In article <77h73o$asm@bgtnsc03.worldnet.att.net>,
Anthony Clay <zarthrag@softhome.net> wrote:
>    While writing my own array class in standard ANSI C++ I came across an
>intersting question.  I understand that two different classes can declare to
>be friends with each other, can I class declare itself a friend.

Classes are already friends with themselves.

>    In my class each node in the array has pointers that point to the next
>and previous elements in the array.  However, when constructing and
>'stringing' the seperate nodes together, I want to this object to have
>access to the private members of another member of the same class or
>whatever.

C++ access control is based on classes, not objects.  A member function of
a class can access the private data of that class in any object of that
class, not just the "this" object.

--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]