Jason on Mon, 15 Jul 2002 07:40:07 +0200 |
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 It's a little unclear what you are trying to do here. You have to be careful about when you create an object, when other objects access that object, and when you *destroy* the object. Make sure you do destroy any object that you are responsible for cleaning up (usually, just the ones that you instantiate with new). If you want to modify the object "while in another object", then don't you want the following: void SomeObject::SomeFunction() { QLabel *qBillBoard = new QLabel("unlabeled", 0, 0); voxInstallCommon(qBillBoard); } voxInstallCommon::voxInstallCommon(QLabel *qlIncoming) { // Now access the qIIncoming pointer // It will point to the same object as the qBillBoard pointer } Now, it's unclear without the other code, but maybe you really want to be doing the following: void SomeObject::SomeFunction() { QLabel *qBillBoard = NULL; voxInstallCommon(qBillBoard); } voxInstallCommon::voxInstallCommon(QLabel *&qlIncoming) { qIIncoming = new QLabel("unlabeled", 0, 0); // Now access the qIIncoming pointer // The qBillBoard pointer will now also point to the qIIncoming object. } Depends a little on what you are trying to do here. Depends on what you mean by gain access to an object within another object. Sounds more like you have a member variable, so maybe you should just have an accessor function return it like so: class voxInstallCommon { public: voxInstallCommon(); QLabel *GetLabel(); private: QLabel *m_pLabel; }; voxInstallCommon::voxInstallCommon() { m_pLabel = new QLabel("unlabeled", 0, 0); } QLabel *voxInstallCommon::GetLabel() { return m_pLabel; } If this is the case, and you don't need to worry so much about when initialization occurs, you could also implement it like so and avoid the pointers somewhat: class voxInstallCommon { public: voxInstallCommon(); const QLabel &GetLabel(); private: QLabel m_Label; }; voxInstallCommon::voxInstallCommon() : m_label(QLabel("unlabeled", 0, 0)) { } const QLabel &voxInstallCommon::GetLabel() { return m_Label; } Or, something along those lines. May not have the intialization syntax exactly right for the QLabel. HTH. Jason Nocks On Sunday 14 July 2002 15H:02, mike.h wrote: > Fred, > > You do not appear to be declaring the local pointer varible in your snip: > > voxInstallCommon::voxInstallCommon(QLabel *qlIncoming) > { > QLabel *qlBillBoard=new QLabel("unlabeled", 0, 0); > qlBillBoard=qlIncoming; > ... > -mike > > -----Original Message----- > From: plug-admin@lists.phillylinux.org > [mailto:plug-admin@lists.phillylinux.org]On Behalf Of Fred K Ollinger > Sent: Saturday, July 13, 2002 11:27 AM > To: plug@lists.phillylinux.org > Subject: [PLUG] [ON TOPIC] passing a pointer to an object to an object > (c++) > > > I have this code where I have an object and I'd like to modify it while in > another object. Is there a way to do this? I get segfaults when I do: > > // begin snippet > voxInstallCommon::voxInstallCommon(QLabel *qlIncoming) > { > qlBillBoard=new QLabel("unlabeled", 0, 0); > qlBillBoard=qlIncoming; > //end snippet > > Why? > > Is there a better way to gain access to an oject in another object? > > I have spent a lot of time reading about this and trying permutations to > no avail. I want the oject to be persistent in the other object. > > I have successfully passed a pointer to an object in the function, but the > pointer is not persistent so I'm trying to do this via a constructor and > then make a pointer to the pointer. Since all the pointers are aiming at > the same patch of memory this should work, but it doesn't. > > Fred Ollinger (follinge@sas.upenn.edu) > CCN sysadmin > -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iEYEARECAAYFAj0yXj4ACgkQ3CryLfCgqRk6cQCfW1xG0Ozq/K5h8XvO3KWVWEjD 1cMAnjCKon/hpc8pGucvz6XjyHwJDWP5 =1LVZ -----END PGP SIGNATURE----- ______________________________________________________________________ Philadelphia Linux Users Group - http://www.phillylinux.org Announcements-http://lists.phillylinux.org/mail/listinfo/plug-announce General Discussion - http://lists.phillylinux.org/mail/listinfo/plug
|
|