CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: const pointer issue

  1. #1
    Join Date
    Oct 2009
    Posts
    1

    const pointer issue

    Hi, I'm writing a ray-tracer wherein one HitPoint object is passed between all the Intersectables in the scene. The Intersectables then store a pointer to themselves inside the HitPoint object. The issue I'm having is that the Intersectable->intersect(HitPoint) function is const, so the this pointer is a const Intersectable * const, and won't let me put it in the HitPoint object.

    tl;dr

    Code:
    class Intersectable
    {
    public:
        virtual void intersect(HitPoint &hp) const = 0;
    };
    
    class Sphere : public Intersectable
    {
    public:
        void intersect(HitPoint &hp) const
        {
            if (intersection_is_good)
            {
                hp.intersectable = this;
            }
        }
    };
    
    class HitPoint
    {
    public:
        Intersectable * const intersectable;    //I don't know what this should be
    };
    Is there a simple solution, or is this just a poor design?

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

    Re: const pointer issue

    Quote Originally Posted by flux00 View Post
    Is there a simple solution, or is this just a poor design?
    What you've posted seems like a pretty poor design to me. However, the actual problem here is that HitPoint has a const pointer (that is, not pointer to const). That means you can only set the pointer in the HitPoint constructor. If you want to be able to assign the pointer afterwards, you need
    Code:
    const Intersectable* intersectable; // pointer to const Intersectable
    // or
    Intersectable* intersectable; // pointer to Intersectable
    If you want help with your design, please explain why HitPoint needs a pointer to an Intersectable and how you are calling the Intersectable::intersect function.
    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

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)