From -2462444255486531910
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,8b682de414fbeb0e
X-Google-Attributes: gidf78e5,public
From: AllanW@my-dejanews.com
Subject: Re: Cpp Inheritance = Incest?
Date: 1999/01/14
Message-ID: <77jdb2$nsd$1@nnrp1.dejanews.com>#1/1
X-Deja-AN: 432361939
X-NNTP-Posting-Host: 199.125.171.8
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <77h73o$asm@bgtnsc03.worldnet.att.net>
X-Snookums: I love you
X-UID: 0000000001
X-Status: $$$T
X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)
X-Http-Proxy: 1.1 x11.dejanews.com:80 (Squid/1.1.22) for client 199.125.171.8
Organization: Deja News - The Leader in Internet Discussion
X-Article-Creation-Date: Thu Jan 14 00:22:04 1999 GMT
Newsgroups: comp.std.c++
Originator: clamage@taumet


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              ]




