473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

pointer to type all over the place

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
Nov 30 '06 #1
1 1226

Gary Wessle wrote:
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.

Nov 30 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bob Keith | last post by:
Hello, all: I am a beginner of C++, and feel confusing about the differences of the following terms regarding pointer. int *a int* a int** a int*& a int *&a
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Koster | last post by:
Something I've been puzzled over for a while. This really has no consequence at all, but I'm curious for the _reason_ behind where we commonly place the asterisk when declaring pointer variables....
52
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
49
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.