|
From: worlman385 on 17 Apr 2008 19:44 ==== [Non-pointer version] ==== as a rule, for non pointer, as long as I have an object that dont' need to pass to another function / call another function. I can do the following (non-pointer) - Database db; db.open("user", "pass"); ==== [Pointer version] ==== If I need to pass the object to another function then I should use (pointer) - Database* db = new Database(); bool flag = openConnection ( db, "user", "pass" ); ==== [pointer version (reference)] ==== If I need to pass the object to another function then I can also use (reference) - Database db; bool flag = openConnection ( db, "user", "pass" ); by doing method #1 and #3, i can avoid pointer most of the time? then I will not need to worry about delete / free pointer after using it.
From: Igor Tandetnik on 17 Apr 2008 19:59 worlman385(a)yahoo.com wrote: > ==== [Non-pointer version] ==== > as a rule, for non pointer, as long as I have an object that dont' > need to pass to another function / call another function. I can do the > following (non-pointer) - > > Database db; > db.open("user", "pass"); > > > ==== [Pointer version] ==== > If I need to pass the object to another function then I should use > (pointer) - > Database* db = new Database(); > bool flag = openConnection ( db, "user", "pass" ); Not necessarily. You can do Database db; bool flag = openConnection (&db, "user", "pass" ); Whether or not to allocate on the heap depends on whether you want the object to outlive the current scope, not whether you need a pointer to it. > ==== [pointer version (reference)] ==== > If I need to pass the object to another function then I can also use > (reference) - > Database db; > bool flag = openConnection ( db, "user", "pass" ); And also Database* db = new Database(); bool flag = openConnection (*db, "user", "pass" ); Just to demonstrate again that the choice of allocating on the stack or on the heap has nothing to do with whether some function wants a pointer or a reference. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
|
Pages: 1 Prev: string vs string* Next: array variable subscript generate wrong code (2008) ? |