CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2010
    Posts
    121

    Want to distribute a pointer of a transient value?

    I store a local position in my class, and I also have some uses on the world position, the world position is calculated from the local position. Some other modules may request a world position pointer from this class. I don't want to store 2 copies of the same position, I want to work out on the fly, but one of such modules need world positions to work with. It is best fit for a world position pointer, but I only got local position pointer, what would you do?
    Thanks
    Jack

  2. #2
    Join Date
    Feb 2017
    Posts
    677

    Re: Want to distribute a pointer of a transient value?

    I would introduce an inlined "free" function that converts a local pointer to a global pointer, along these lines,

    Code:
    inline GlobalPointer toGlobal(LocalPointer lp) {
        GlobalPointer gp = ..........................; // conversion
        return gp;
    }
    It may mean the toGlobal() function must put the global position on the heap. In that case it's usually best it returns an std::shared_ptr. It may then be nice (consistency, symmetry) to also store the local position on the heap and make LocalPointer an std::shared_ptr too but that of course isn't necessary.

    Another possibility to avoid the heap issue would be that toGlobal() returns a global position rather than a pointer to a global position. It could then also take a local position as parameter rather than a pointer to one.

    "Free" means the function doesn't belong to any class in particular. It's best put in a namespace together with related supporting information in a .h file which is included when needed.
    Last edited by wolle; October 6th, 2017 at 01:51 AM.

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