🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #375

Re: Class and pointer problem

#375
From: wienw
Date: Tue, 29 Jun 1999 00:00
101 lines
3129 bytes
Hi,

I think you can declare the ptr as a static member of the class Object.
Then it will act as a global variable in all the instances of the class.

The code will be something like:

#include <iostream.h>

class Object
{ public:
     int id;
     static Object *ptr;//declare the ptr as a static member
	 Object(int);
	 void show(){ cout<<"this is "<<id<<endl;};
 };

 Object::Object(int idnum)
 {
      id = idnum;
      ptr = this;
 }

 Object *Object::ptr=new Object(0);//define and initialize "ptr"

 main()
 {Object(1);
  Object::ptr->show();//show 1
  Object * p=new Object(2);
  Object::ptr->show();//show 2

  return 0;

 }


 Good luck



In article <S3be3.496$nZ5.796@nntpserver.swip.net>,
  "Magnus Hult" <hult.holmstrom@swipnet.se> 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
>
>


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Technical Details
Message-ID: <7lbma1$v8g$1@nnrp1.deja.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <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: 114592,c03b176bbf53f537
X-Google-Attributes: gid114592,public
X-Google-Thread: 109fba,c03b176bbf53f537
X-Google-Attributes: gid109fba,public
From: wienw@my-deja.com
Subject: Re: Class and pointer problem
Date: 1999/06/29
Message-ID: <7lbma1$v8g$1@nnrp1.deja.com>
X-Deja-AN: 495368337
References: <S3be3.496$nZ5.796@nntpserver.swip.net>
X-Http-Proxy: 1.0 x23.deja.com:80 (Squid/1.1.22) for client 128.248.141.125
Organization: Deja.com - Share what you know. Learn what you don't.
X-Article-Creation-Date: Tue Jun 29 23:53:41 1999 GMT
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,comp.lang.c++
X-Http-User-Agent: Mozilla/4.04 [en] (Win95; I)