1.

What Is Meant By Forward Referencing And When Should It Be Used?

Answer»

Forward REFERENCING is generally REQUIRED when we make a class or a function as a friend.Consider following program:

class test { public: friend void fun ( sample, test ) ; } ; class sample { public: friend void fun ( sample, test ) ; } ; void fun ( sample s, test t ) { // code } void main( ) { sample s ; test t ; fun ( s, t ) ; }

On compiling this program it gives error on the following STATEMENT of test class. It gives an error that sample is undeclared identifier. friend void fun ( sample, test ) ; This is so because the class sample is defined below the class test and we are using it before its definition. To overcome this error we need to GIVE forward reference of the class sample before the definition of class test. The following statement is the forward reference of class sample.

class sample;

Forward referencing is generally required when we make a class or a function as a friend.Consider following program:

On compiling this program it gives error on the following statement of test class. It gives an error that sample is undeclared identifier. friend void fun ( sample, test ) ; This is so because the class sample is defined below the class test and we are using it before its definition. To overcome this error we need to give forward reference of the class sample before the definition of class test. The following statement is the forward reference of class sample.



Discussion

No Comment Found