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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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?
Edit - Sorry if I was a little off was just trying to help :)
Last edited on
1) no, within a const function no member variables can be changed, but parameters can

2) yes, you can. please don't say things like this so confidently if you don't know because it misleads others. An Intersectable * const is a const pointer to an intersectable.

3) In the actual implementation it is initially set to NULL, but that has nothing to do with the issue.
Dude, calm down. He was only trying to help.

Could you change HitPoint::intersectable to const Intersectable * to match the type of this? It's either that or casting this: (Intersectable * const)this
But really, I think intersect() should be a member of HitPoint:
1
2
3
void HitPoint::intersect(Intersectable *i){
    this->intersectable=i;
}

Why is HitPoint::intersectable a constant pointer if it's evidently going to change at run time?
it's not like I meant it in a mean way at all, it's just that as a programmer I'm constantly searching forums for info, and when I'm misled it costs me a lot of time and confusion, especially when guesses are said as a matter of fact.

That's the heart of the issue, really. I can't have HitPoint::intersectable be a const Intersectable * because then I couldn't change it. I need a variable pointer that points to a constant object. How would I say that in code, without making the member itself constant?

I can't I can cast away the const of the object by doing (Intersectable * const)(this), I get a C2166.

I've just been messing around with modifiers trying to get it to work, that's the latest incarnation.
A variable pointer that points to a constant object is 'const T *'. 'T * const' is a constant pointer that points to a variable object.
I understand, but I need that to vary on the HitPoint object. Should I be trying to make HitPoint::intersectable a const Intersectable ** const and then being like *hitpoint.intersectable = this?
I need that to vary on the HitPoint object.
I'm not following.

*hitpoint.intersectable = this
If you do that, you'll probably cause a segmentation fault. It wouldn't give you any benefit either way when compared to const Intersectable *.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class HitPoint
{
public:
    const Intersectable * const intersectable;
};

class Sphere
{
public:
    void intersect(HitPoint &hp) const
    {
        hp.intersectable = this; //error C2166
    }
};

int main()
{
    Sphere s;
    HitPoint hp;
    s.intersect(hp);
}


If the member is const, I can't change its' value.
So... Make it non-const.
1
2
3
4
5
class HitPoint
{
public:
    const Intersectable * intersectable;
};


I still think Sphere::intersect() is very counter-intuitive.
Last edited on
oh that actually worked, thanks.

I think I was over-thinking it.

and if you saw the entirety of the program I think you'd think otherwise.
Topic archived. No new replies allowed.