Circular Pointers

Circular Pointers

Question:
I’m trying to make two classes point to one another. I know the syntax is weird, but it’s just to illustrate:

 class x{ y* ptry; }class y{ x* ptrx; }

It won’t compile. I get the error “missing storage class.” How do I get around this?

Answer:
You need to forward-declare a class before you can create a pointer to it:

 class y; //fwd declarationclass x{ y* ptry; };class y{ x* ptrx; };

Without the forward declaration, the compiler doesn’t recognize y as a class name because the declaration of y hasn’t been seen. A forward declaration instructs the compiler to accept the pointer definition even if it hasn’t seen yet the declaration of y. Note that you don’t need a forward declaration for ptrx because by the time the compiler sees its definition, class x has already been seen.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several