LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-23-2011, 05:52 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
C++ - Using class pointers to use class functions?


I'm using code which has a declaration similar to this:
Code:
something::SomethingElse::Ptr thing (new something::SomethingElse);
Is this a regular convention to declare things this way? If so, how can I directly reference what the "thing" pointer points to? Say, if I wanted to use one of SomethingElse's class functions on the SomethingElse that "thing" points to? Sorry if I missed where someone has explained this before or if I worded this badly. Thanks for your time!
 
Old 04-23-2011, 06:58 PM   #2
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 209

Rep: Reputation: 65
Ptr is a typename defined in the SomethingElse class. To see how to use it you'll have to look at its definition. It might just be a pointer typedef. Not hardcoding bare pointer usage is a mixed blessing, but it can definitely have its upsides. It just depends on whether you're getting any payback for the loss of simplicity and directness. Functors and proxies can make coding more sophisticated systems bearable. I *hate* having to wade through a lot of semantically-useless scaffolding when reading code. Too bad so many people use language features as demonstrators for technological prowess, shoving their usage of every. single. feature. in your face instead of using those features to make the scaffolding disappear and leave only problem-space semantics. Whether or not "Ptr" is worth anything here depends on what kind of programming culture you're living in. If you're in academia the problem is the entire point is to learn to use the features, so different priorities have their effect. Gah. This turned into a bit of a rant, sorry.
 
Old 04-24-2011, 12:41 AM   #3
winning
Member
 
Registered: Apr 2011
Posts: 70

Rep: Reputation: 13
Having only "something::something<....>" at our disposal it may prove difficult to provide an accurate answer. Some classes provide methods which must return pointers to some data (for example, a pointer to a dynamically allocated copy of the same instance). Some issues may arise from this:
  • Must the client release the associated memory when they are done using the pointer (not all pointers point to dynamically allocated data)? Should the answer be yes, how must the client go about releasing it (free() / delete / delete[] / something else entirely)?
  • Assuming that the associated memory must be released, the class author could provide lengthy documentation on how to go about doing this. Rather than assume the client will remember to do this (and do it even when confronted with exceptions), the class author can choose to force the usage of the RAII idiom by returning some type of smart pointer instead of the fast pointer. This way, the client can concentrate on using the resource.

Now then, assume that what is being returned is a smart pointer of some sort. A typedef helps by localizing the choice of the smart pointer type (which is a good thing).

Quote:
If so, how can I directly reference what the "thing" pointer points to? Say, if I wanted to use one of SomethingElse's class functions on the SomethingElse that "thing" points to?
Again, it is difficult to say for sure without knowing the precise code, but it is likely that you are able to do what you want just as you would do it with a raw pointer:

Code:
something::SomethingElse::Ptr thing(new something::SomethingElse);
thing->undress();
(*thing).undress();
Generally it is recommended that resource handling classes also provide a method for clients to obtain access to the fast pointer. std::auto_ptr, boost::shared_ptr, boost::scoped_ptr and others name this method "get()".

Last edited by winning; 04-24-2011 at 12:52 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Function pointers to class members, cpp brazilnut Programming 2 05-26-2010 10:18 AM
C++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
PHP: Calling class functions from another class. unreal128 Programming 5 11-24-2006 03:04 AM
C++ - problem with pointers to class member functions Nylex Programming 3 01-15-2006 10:14 AM
arrays of pointers to class objects tdurden Programming 5 12-16-2002 10:47 AM


All times are GMT -5. The time now is 11:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration