×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

What is self-referential class in C++?

Learn: What are self-referential classes in C++ programming language, why they are important for development purpose?

What is self-referential class in C++?

It is a special type of class. It is basically created for linked list and tree based implementation in C++. If a class contains the data member as pointer to object of similar class, then it is called a self-referential class.

Declaration

Consider the following class declaration

class node {
private:
    int data;
    node* next; //pointer to object of same type

public:
    //Member functions.
};

Explanation

In this declaration, the statement node *next; represents the self-reverential class declaration, node is the name of same class and next the pointer to class (object of class).

Normally, we use self referential structure for linked list or tree based implementation. But OOPS is always better choice for development. Structures and classes are both user define data-types. But classes provide encapsulation, inheritance and other good feature they help us to manage large code. Because at project level, it is very important to manage code properly.

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.