AnilMS
AnilMS

Reputation: 83

Abstract class with Method with Data type defined afterwards which has pointer to the Class. Going round

I am trying to implement an abstract class 'Primitive' defined as

class Primitive
{
    virtual bool intersect(Ray& ray, float* thit, Intersection* in) = 0;
    virtual bool intersectP(Ray& ray) = 0;
    virtual void getBRDF(LocalGeo& local, BRDF* brdf) = 0;
};

My problem is that Primitive contains a Method intersect that uses Intersection type which is defined as

class Intersection
{
    LocalGeo localGeo;
    Primitive* primitive;
};

Intersection has a link to Primitive. So, I am not able to compile this as the compiler gives an error that Intersection is not defined as it comes after Primitive definition.

The problem boils down to ...

class A
{
    void afunc(B *b);
};


class B
{
    A *a;   
}

Is there a way to define classes in this way? I tried to google, but I wasn't sure what to google.

Thanks

Upvotes: 1

Views: 69

Answers (2)

juanchopanza
juanchopanza

Reputation: 227390

Use forward declarations in your header files:

class Intersection; // forward declaration

class Primitive { /* as before */ };

and

class Primitive; // forward declaration

class Intersection { /* as before */ };

Upvotes: 2

alexrider
alexrider

Reputation: 4463

You need to forward declare class B

class B;
class A
{
    void afunc(B *b);
};

Should fix compilation.

Upvotes: 0

Related Questions