pointer to type all over the place

G

Gary Wessle

Hi

I created a class Info which holds variables and their values, all
public, I then created an instance of this Info in main

int main() {
Info info;

and made
class myType{
Info* info;
public:
myType::myType(Info* info) : info(info)

Type2::Type2(Info*) ...

and passed a pointer to it all over the place like

myType t1(&info);

void myType::myMethod(){
Type2 t2(info);
....


is this a safe practice.
I need the variables in Info mostly through out.

thanks
 
N

Noah Roberts

Gary said:
Hi

I created a class Info which holds variables and their values, all
public, I then created an instance of this Info in main

int main() {
Info info;

and made
class myType{
Info* info;
public:
myType::myType(Info* info) : info(info)

Type2::Type2(Info*) ...

and passed a pointer to it all over the place like

myType t1(&info);

void myType::myMethod(){
Type2 t2(info);
...


is this a safe practice.

As it stands sure, but it is fragile. For instance, what if you later
decide to instantiate myType with a different info, one created on the
heap...I would make myType's constructor accept a shared_ptr (from
boost or tr1) so that the same object is available in both places but
the design isn't as fragile.

I believe you can create a shared_ptr with a stack item like info above
if you pass it a deleter that doesn't do anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top