Article View: alt.comp.lang.learn.c-c++
Article #356Re: Class and pointer problem
From: Lorg
Date: Sun, 11 Jul 1999 00:00
Date: Sun, 11 Jul 1999 00:00
71 lines
2880 bytes
2880 bytes
Hi!
i read what you wrote, and i read what other ppl wrote, and there is a VERy
simple way to do it, in fact two ways.
1. declare the object:
class Object;
typedef *Object Objpoint; /* i think thats how typdef go, long time i havnt
used it :) */
class object
{
.
.
.
Objpoint ptr;
}
thats way one. way two, i can remember from old pascal compilers, when u wanted
to create chained lists,
you declared the pointer just before the class, and the compiler 'knew' to go
the class just afterwards.
thats how it went in the old days.
-Lorg
Lorgandon@hotmail.com
btw!! i forget, i can also declare the pointer void * !!!
Magnus Hult wrote:
> Hello,
> I've got this class, Object. It looks like this (well, it's more
> complicated, but you'll get the essence):
>
> class Object {
> public:
> int id;
> Object(int idnum) {
> id = idnum;
> ptr = this; // Let the pointer point to the newly
> created instance
> }
> };
>
> And ptr is supposed to be a global varible declared like "Object *ptr". The
> problem is that I can't declare it before the class definition, cause then
> my old Borland 4.51 doesn't know what an "Object" is. But I can't declare it
> after the class definition either, cause then I've already used the pointer
> in the class without telling C++.
> The only way I've been able to get through this problem is to do like this:
>
> class Object {
> public:
> int id;
> Object(int);
> };
>
> Object *ptr; // Here C++ knows what an Object is, and I haven't
> used ptr yet
>
> Object::Object(int idnum) {
> id = idnum;
> ptr = this;
> }
>
> which isn't a very smooth solution, since I might be needing more variables
> and classes like this. I thought it didn't matter where in the code I
> declare variables and so on, but apparently it does. Is my compiler too old,
> or is there a better way to solve the problem?
>
> thanks,
> Magnus Hult
> hult.holmstrom@swipnet.se
> http://www.ovenproof.com
Technical Details
Message-ID:
<3788DD34.E8FD6911@hotmail.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<S3be3.496$nZ5.796@nntpserver.swip.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f8d38,c03b176bbf53f537 X-Google-Attributes: gidf8d38,public X-Google-Thread: 109fba,c03b176bbf53f537 X-Google-Attributes: gid109fba,public X-Google-Thread: 114592,c03b176bbf53f537 X-Google-Attributes: gid114592,public From: Lorg <Lorgandon@hotmail.com> Subject: Re: Class and pointer problem Date: 1999/07/11 Message-ID: <3788DD34.E8FD6911@hotmail.com> X-Deja-AN: 499819695 X-NNTP-Posting-Host: r016.racs.surfree.net.il Content-Transfer-Encoding: 7bit References: <S3be3.496$nZ5.796@nntpserver.swip.net> Content-Type: text/plain; charset=us-ascii X-Trace: 11 Jul 1999 21:09:05 +0200, r016.racs.surfree.net.il MIME-Version: 1.0 Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,comp.lang.c++