View unanswered posts | View active topics It is currently Thu Oct 18, 2018 5:08 pm



Reply to topic  [ 2 posts ] 
 use basic types as class members not pointers to them 
Author Message

Joined: Tue Jan 24, 2012 11:15 am
Posts: 5
Post use basic types as class members not pointers to them
hi laumaya,
I have some suggestions about m_pNumberOfOccurence of GLC_StructOccurence.
we'd better not save a pointer to an integer number in a class.
In this way,we need an extra dynamically allocation(new) for an int number or basic type(bool,float), and it is also complex and difficult to maintain this memory.

After discussions with my workmates, we conclude that there is no need to use a pointer to a member if this class owns this member.
if the class just keeps a handle of this member (not dynamically allocate or new memory in its constructor), we will use a pointer to that type.
In others words, it is unwise that you dynamically allocate ("new") memory for a member and keep a pointer to it,because this way needs an extra call to "new" function and you have to maintain this chunk of memory.

And I have made a tiny fix patch:http://code.google.com/p/glc/source/detail?r=11&&http://code.google.com/p/glc/source/detail?r=12...


2012/2/3
hi laumaya,
I realize that sometimes we really need to use a pointer to a member, such as m_pNumberOfInstance in GLC_StructInstance.
But I think we can have better design to eliminate this strong coupling.
for example,we can create a GLC_InstanceMgr to maintain the number of all GLC_StructInstance;
or we can use a static variable instead.
:D

Above it is just my opinion and thanks for your reading.


Last edited by joewan chen on Fri Feb 17, 2012 5:08 pm, edited 1 time in total.



Fri Feb 03, 2012 5:31 am
Profile
Site Admin

Joined: Sun Jan 03, 2010 4:27 pm
Posts: 390
Post Re: use basic types as class members not potiners to them
Hi,
joewan chen wrote:
After discussions with my workmates, we conclude that there is no need to use a pointer to a member if this class owns this member.
if the class just keeps a handle of this member (not dynamically allocate or new memory in its constructor), we will use a pointer to that type.
In others words, it is unwise that you dynamically allocate ("new") memory for a member and keep a pointer to it,because this way needs an extra call to "new" function and you have to maintain this chunk of memory.

Yes, but it's not the case... In this case this pointer is used to delete a GLC_StructInstance when the last GLC_StructOccurence of this GLC_StructInstance is deleted. If you don't use a pointer.. How can you know if you delete the first created occurrence of an instance or the last occurrence of an instance ?
I'm not sure that it's the better way. But it's like a shared pointer and it's work great.

@+


Wed Feb 15, 2012 10:13 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.