CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 43
  1. #1
    Join Date
    Apr 2013
    Posts
    33

    When to use pointers?

    I've googled the issue and read a few pretty abstract explanations.

    I was hoping you could give me a few solid examples for when one should use pointers.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: When to use pointers?

    Just to check: what material (e.g., books) are you using to learn C++ programming? How much of the material have you learned?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2013
    Posts
    33

    Re: When to use pointers?

    No books, but I covered every single word here:
    http://www.cplusplus.com/doc/tutorial/

    In case you're afraid I'm too new to programming to start learning this kind of stuff, I also have about 8 years of experience in ActionScript 3.0 (OOP).

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: When to use pointers?

    Quote Originally Posted by royibernthal View Post
    I've googled the issue and read a few pretty abstract explanations.
    1) In C++, you cannot achieve polymorphism without pointers (or references). Polymorphism is one of the cornerstones of OO programming, and you can't do that in the C++ language without using pointers/references.

    2) The allocator functions (new, malloc(), etc.), plus many standard functions return pointers. So you have no choice but to use pointers since that is what is returned to you.

    3) Calling a function that changes its argument(s) value(s) internally inside the function can require you to pass an address (i.e. pointer).

    So right there, you cannot avoid using pointers in those 3 scenarios. Any other scenario, pointer usage should be minimized in a well-written C++ program (with the exception of smart-pointer usage).

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 2013
    Posts
    33

    Re: When to use pointers?

    1) Do I have any real reason to use pointers over references here? So far I managed to pull off everything I tried with references only, maybe it could have been done more efficiently though using pointers I really have no idea.

    I see, so those are the scenarios I need to be looking for? Other than that should I just use references everywhere?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: When to use pointers?

    Quote Originally Posted by royibernthal View Post
    1) Do I have any real reason to use pointers over references here? So far I managed to pull off everything I tried with references only, maybe it could have been done more efficiently though using pointers I really have no idea.
    One rule of thumb concerning efficiency and C++:

    Unless you're writing a function that implements a general algorithm, trying to guess in C++ what code is more efficient than another code will yield you, for the most part, wrong answers.

    Too many persons new to C++, or persons who know another language but think they know C++ well enough, almost always guess wrong as to what code is more efficient than another piece of code. One main reason is that the C++ code we see is usually not the same code after the compiler finishes its optimization process. Yes, the code will execute properly, but the compiler has done "tricks" to make it more optimal than what the original source code suggests.

    Second, as far as using a pointer, maybe the function wants to accept NULL as an argument. You can't legally pass NULL references in C++, but you can pass NULL pointers.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 4th, 2013 at 01:04 PM.

  7. #7
    Join Date
    Apr 2013
    Posts
    33

    Re: When to use pointers?

    Duly noted. Thanks alot.

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: When to use pointers?

    Quote Originally Posted by royibernthal View Post
    I also have about 8 years of experience in ActionScript 3.0 (OOP).
    In that case you implicitly used a pointer each time you made new in ActionScript.

    If you used the OO programming style in ActionScript and like it you can keep it up in C++ but that requires that you start using the whole language and not just a fraction.

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: When to use pointers?

    Quote Originally Posted by royibernthal View Post
    1) Do I have any real reason to use pointers over references here? So far I managed to pull off everything I tried with references only, maybe it could have been done more efficiently though using pointers I really have no idea.
    The major difference between a reference and a pointer is that a pointer can point to NULL and you can change what a pointer points to. Neither is possible with a reference.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  10. #10
    Join Date
    Apr 2013
    Posts
    33

    Re: When to use pointers?

    nuzzle - Yes I did like OOP, what do you mean by using the whole language and not just a fraction? Got any example for that perhaps?

    D_Drmmr - Okay I understand.
    Last edited by royibernthal; April 5th, 2013 at 04:35 AM.

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: When to use pointers?

    Quote Originally Posted by royibernthal View Post
    what do you mean by using the whole language and not just a fraction?
    You must start using pointers! and the heap.

    I found myself in your situation a few years ago but coming from another OO language - Java. C++ is very versatile and I've set up a scheme based on smart pointers that quite closely mimics the memory management of Java. I'm sure you could do the same for ActionScript. I haven't used it but had a quick glance and it looks a lot like Java. And by the way, smart pointers preferrably are passed by reference so you can have it both ways, both reference and pointer.

    People are going to warn you for this approach though. Conventional wisdom has it that one should use each language for what it is, not pretend it's something else. But that's not what I'm doing. I'm just taking advantage of the exceptional powers of C++ to mold it to my liking and it works fine for me. I haven't bothered to even learn all that boring and complicated value semantics stuff everybody think they must be doing. My heap allocated objects are prevented from being copied by design so that's not even an issue (and my value objects are always kept so simple they can do with default copy construction and assignment operation).

    You won't be able to do what I suggest immediately but have it in mind and you'll find a way eventually.
    Last edited by nuzzle; April 5th, 2013 at 08:29 AM.

  12. #12
    Join Date
    Apr 2013
    Posts
    33

    Re: When to use pointers?

    Java is indeed very similar to ActionScript 3.0, I've been using it as well. There are minor syntax differences but overall once you know one it's very easy to learn the other.

    What are those boring and complicated value semantics you're talking about?
    The attitude you have sounds very tempting, I do want to make sure though that I can understand other people's code.

    Is this conventional wisdom of C++ OOP programmers? It simply sounds like you're going full OOP on C++. I can't see what's wrong with what you're saying but hey, I just got here
    Last edited by royibernthal; April 5th, 2013 at 08:27 AM.

  13. #13
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: When to use pointers?

    There are many ways you can solve a particular problem in c++

    As a general rule concerning pointers and references...
    Use references. Use pointers only when references won't do. If you do use pointers, you probably want to hide that implementation in a single function or in a single class as a private/protected member. If you have to use pointers, it's probably a good idea to manage them through one of the various smart pointer classes.


    All the above rules have their reasons. As a whole, pointers tend to confuse novice programmers, and even experienced programmers will at times be confused. If you have a pointer you get confusions about: who's responsible for filling in the pointer, is it a pointer to static data to local data or to allocated data will the pointer still point to something under all conditions, does it HAVE to point to allocated data, who's responsible for cleaning up the data if it was indeed allocated, if you cleaned up the data, what should the pointer point to ?
    You can avoid a lot of those confusions/headaches by avoiding pointers. It's also why we have container classes like vector/list/map/queue... For one they relieve you of the work to write it yourself, but it also hides a lot of the "pointer mess" behind well tested classes. And this is a good approach for yourself too (see the above general rule), if you have to use pointers, hide the "pointer mess" behind a clean interface of your own. The fewer places in your code you have to deal with pointers, the fewer places in your code you'll have to worry about them.

    Everything you can do with a reference you can do with a pointer (except when you need a type difference between the two) but not the other way around, which is why you can't always avoid pointers.

  14. #14
    Join Date
    Apr 2013
    Posts
    33

    Re: When to use pointers?

    I understand. I imagine there'll be cases where I'll have to expose my pointers, such as when a factory creates and returns a new product (OOP). Will smart pointers (scope) be good for that?
    Last edited by royibernthal; April 6th, 2013 at 03:47 AM.

  15. #15
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: When to use pointers?

    Quote Originally Posted by royibernthal View Post
    I understand. I imagine there'll be cases where I'll have to expose my pointers, such as when a factory creates and returns a new product (OOP). Will smart pointers (scope) be good for that?
    Yes, when you have a factory it is good practice to return a smart pointer (either shared_ptr if you need reference counting or (normally) a unique_ptr).

    I think it's good to make a clear(er) distinction between using pointers and managing objects that are allocated on the heap. For the latter, you should use smart pointers whenever possible, because they make life much easier. However, there is nothing wrong with using raw pointers when you are not concerned with managing memory and a reference won't do. E.g. if you need to create a wrapper for some object, it may be useful to store a pointer to the wrapped object. This will make the wrapper assignable, which it would not be had you used a reference. Another example is passing an optional argument to a function. It perfectly OK to use a raw pointer for that, where the value NULL indicates that the argument is not provided.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured