Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-23-2011, 05:52 PM   #1
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:
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
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
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
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).

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:

something::SomethingElse::Ptr thing(new something::SomethingElse);
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.


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

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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:52 PM.

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