Jive reference manual
List of all members | Public Member Functions | Related Functions
jem::CString Class Reference

Converts a String object to a null-terminated character array. More...

#include <jem/base/CString.h>

Public Member Functions

 CString ()
 Creates an empty CString object. More...
 
 CString (const String &str)
 Creates a CString from a String object. More...
 
 CString (const CString &rhs)
 Copy constructor. More...
 
 ~CString ()
 Deallocates resources. More...
 
CStringoperator= (const CString &rhs)
 Copies another CString object. More...
 
 operator const char * () const
 Conversion operator. More...
 
const char * addr () const
 Returns the character array encapsulated by this object. More...
 

Related Functions

(Note that these are not member functions.)

CString makeCString (const String &str)
 Converts a String object to a CString object. More...
 

Detailed Description

The CString class helps you convert String objects to null-terminated character arrays that can be passed to standard C/C++ functions. CString objects are normally created by calling the non-member function makeCString(). Here is a typical example:

String s ( "Hello World" );
::fputs ( ::stdout, makeCString(s) );

A CString object is basically a simple wrapper around a dynamically allocated null-terminated character array. Although multiple CString objects may point to the same array, only one of these objects is the owner of the array. When the owner is destroyed it will deallocate the character array. All other CString objects pointing to the same character array then become invalid and should no longer be used.

See also
String, makeCString()

Constructor & Destructor Documentation

jem::CString::CString ( )

Creates an empty CString object that points to the empty character array "". This object is not the owner of its character array.

Postcondition
this->addr()[0] == '\0'
jem::CString::CString ( const String str)
explicit

Creates a CString object that points to a null-terminated character array containing the same characters as the String object str. This CString object is the owner of its character array.

Parameters
str- a String object.
Postcondition
str == this->addr()
jem::CString::CString ( const CString rhs)

Creates a shallow copy of the CString rhs. The newly created object points to the same character array as the rhs object. Ownership of the character array is passed from the rhs object to the newly created object.

Parameters
rhs- a CString object.
Postcondition
this->addr() == rhs.addr()
jem::CString::~CString ( )

If this object is the owner of its character array, it will delete the character array. All other CString objects pointing to the same character array then become invalid.

Member Function Documentation

CString& jem::CString::operator= ( const CString rhs)

Copies the character array pointed to by the rhs object to this object. Ownership of the character array is passed from the rhs object to this object.

If this object is the owner of its current character array, it will delete that array.

Parameters
rhs- the CString object to be copied.
Returns
*this
Postcondition
this->addr() == rhs.addr()
jem::CString::operator const char * ( ) const

This conversion operator returns a pointer to the null-terminated character array encapsulated by this CString object. The pointer is valid as long as the character array is not deallocated.

Returns
this->addr()
const char* jem::CString::addr ( ) const

Returns a pointer to the null-terminated character array encapsulated by this CString object. The pointer is valid as long as the array is not deallocated.

Returns
A pointer to the character array encapsulated by this object.

Friends And Related Function Documentation

CString makeCString ( const String str)
related

Converts a the String object str to a CString object.

Parameters
str- the String to be converted.
Returns
CString( str )