Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom string class

Tags:

I want to create my own EMailAddress class that acts like a string class.

So I like to do this

private EMailAddress _emailAddress = "[email protected]"; 

instead of

private EMailAddress _emailAddress = new EMailAddress("[email protected]"); 

Is there any way to accomplish what I want, or do I need to use the second alternative. Since string is sealed I can't use that, and the = operator can't be overloaded so I am out of ideas how to fix this....

like image 966
Magnus Gladh Avatar asked Aug 08 '10 21:08

Magnus Gladh


People also ask

How do you create a custom string class?

Take a look at the following code please before reading further: class CString { private: char* cstr; public: CString(); CString(char* str); CString(CString& str); ~CString(); operator char*(); operator const char*(); CString operator+(const CString& q)const; CString operator=(const CString& q); };

How do you create a custom string class in Java?

Creating Strings As with any other object, you can create String objects by using the new keyword and a constructor. The String class has 11 constructors that allow you to provide the initial value of the string using different sources, such as an array of characters.

How do you declare a string class in C++?

String in C++ that is defined by the class “std::string” is a representation of the stream of characters into an object. In other words, String class is a collection of string objects. This string class is a part of the std namespace and is defined in the header “string. h”.

How string is used in C++? How can we create string object?

The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello." strcpy(s1, s2); Copies string s2 into string s1.


1 Answers

You can, with an implicit conversion:

public class EMailAddress {     private string _address;      public EMailAddress(string address)     {         _address = address;     }      public static implicit operator EMailAddress(string address)     {         // While not technically a requirement; see below why this is done.         if (address == null)             return null;          return new EMailAddress(address);     } } 

Implicit conversions should only be used if no data is lost in the conversion. Even then, I recommend you use this feature sparingly, because it can make your code more difficult to read.

In this example, the implicit operator returns null when a null string is passed. As Jon Hanna correctly commented, it is undesirable to have these two snippets of code behave differently:

// Will assign null to the reference EMailAddress x = null;  // Would create an EMailAddress object containing a null string string s = null; EMailAddress y = s; 
like image 69
Thorarin Avatar answered Oct 09 '22 14:10

Thorarin