Article View: alt.comp.lang.learn.c-c++
Article #373Class and pointer problem
From: Magnus Hult
Date: Tue, 29 Jun 1999 00:00
Date: Tue, 29 Jun 1999 00:00
44 lines
2258 bytes
2258 bytes
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:
<S3be3.496$nZ5.796@nntpserver.swip.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f8d38,c03b176bbf53f537,start X-Google-Attributes: gidf8d38,public X-Google-Thread: 109fba,c03b176bbf53f537,start X-Google-Attributes: gid109fba,public X-Google-Thread: 114592,c03b176bbf53f537,start X-Google-Attributes: gid114592,public From: "Magnus Hult" <hult.holmstrom@swipnet.se> Subject: Class and pointer problem Date: 1999/06/29 Message-ID: <S3be3.496$nZ5.796@nntpserver.swip.net> X-Deja-AN: 495351858 X-Sender: s-274939@dialup162-1-45.swipnet.se X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211 X-Complaints-To: news-abuse@swip.net X-Trace: nntpserver.swip.net 930692466 130.244.162.45 (Tue, 29 Jun 1999 23:41:06 MET DST) Organization: A Customer of Tele2 X-MSMail-Priority: Normal NNTP-Posting-Date: Tue, 29 Jun 1999 23:41:06 MET DST Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,comp.lang.c++