C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C++ programming language

What is self-referential class in C++?

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

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.

Consider the following class declaration

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

		//Member functions.

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.

Liked this article? Do share with your friends :)