Next Previous Contents

6. Renaming the String class

6.1 Case 1: Simple rename

If you do not like the String class name then you can use "typedef" to rename the String class.

In all the files where you do include String.h, insert these lines:


// If you do not like the class name String, then you can rename using typedef
typedef String StringSomethingElseIwant;

// Your remaing code may be like this ....
int main()
{
        StringSomethingElseIwant aa_renstr; 
        aa_renstr = "I renamed the String Class using typedef";

        .......etc...
}

See the example_String.cpp.

6.2 Case 2: Resolve conflict

If there is a conflict with another class-name having the same name, and you want to use both this class and conflicting class then you use this technique - in all the files where you do include String.h, insert these lines:


#define  String  String_somethingelse_which_I_want
#include "String.h"
#undef String

#include "ConflictingString.h"  // This also has String class...

// All your code goes here...
main()
{
        String_somethingelse_which_I_want aa;
        String bb; // This string class from conflicting string class

        aa = " some sample string";
        bb = " another string abraka-dabraka";
        .......
}

The pre-processor will replace all literals of String to "String_somethingelse_which_I_want" and immdiately undefines String. After undef the conflicting string class header file is included which defines the "String" class.
Next Previous Contents