本文整理汇总了C++中TString::asChar方法的典型用法代码示例。如果您正苦于以下问题:C++ TString::asChar方法的具体用法?C++ TString::asChar怎么用?C++ TString::asChar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TString
的用法示例。
在下文中一共展示了TString::asChar方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: equals
// compares object with parameter value and returns true if match, otherwise false
bool TString::equals( const TString &sText ) const {
// initializes boolean to false
bool check = 0;
// compares char data targeted by mpText and parameter value
if ( strcmp( mpText, sText.asChar() ) ) {
return check; // if condition is true, returns false
}
return ++check; // returns true otherwise
}
示例2: assign
// checks for assignment to self and returns if true
void TString::assign( const TString &sText ) {
// tests for self-assignment
if ( this != &sText ) {
delete [] mpText; // frees heap memory allocated for char[] targeted by mpText
mLength = sText.length(); // sets non null-byte length of array to be targeted by mpText to that of sText
mpText = new char[ mLength + 1 ]; // initializes encapsulated char data with extra element for null byte
strncpy( mpText, sText.asChar(), mLength ); // copies sText target to new memory targeted by mpText
mpText[ mLength ] = 0; // sets last element in array targeted by mpText to null byte
}
}
示例3: strncpy
// deep copy ctor
TString::TString( const TString &sText ) {
mLength = sText.length(); // sets non null-byte length of array to be targeted by mpText to that of sText
mpText = new char[ mLength + 1 ]; // initializes encapsulated char data with extra element for null byte
strncpy( mpText, sText.asChar(), mLength ); // copies sText target to new memory targeted by mpText
mpText[ mLength ] = 0; // sets last element in array targeted by mpText to null byte
}
示例4: main
// note - you may need to change the definition of the main function to
// be consistent with what your C++ compiler expects.
int main()
{
ofstream outfile (pOutfileName, ios::out);
if( !outfile.is_open() )
{
cout << "Error - unable to open output file: " << pOutfileName << endl;
return 1;
}
//cout << "Writing output to " << pOutfileName << "... please wait" << endl;
//ostream& outstream = outfile; // uncomment to write to file
ostream& outstream = cout; // uncomment to write directly to cout
const char *mp = 0;
int step = 0;
outstream << "----- simple String class test ----- (09/16/2010)" << endl << endl;
// const checks (compile tests)
// If there are compile errors in this section, then there are probably const specifiers missing from your
// class definition, e.g. the length() member function should be defined as a const member function so
// it can be invoked on a const TString object, as should all other member functions that don't change the
// state of 'this'.
{
if (0 != 0) // only a compilation check for these methods at this point (for const). Tested further below.
{
const TString s0(pHello); // create a const TString object
int n = s0.length();
bool f = s0.equalsIgnoreCase(pHello);
f = s0.equals(pHello);
const char *p = s0.asChar();
n = s0.indexOf('h');
const TString s1(s0); // const String object parameter
TString s2;
s2.assign(s0); // const String object parameter
s2.append(s0); // const String object parameter
}
}
// Default ctor
outstream << "\n----- Step " << ++step << " - default ctor -----" << endl;
{
TString s0; // default arg value
outstream << "s0 using default ctor with default arg = \"" << s0.asChar() << "\" (length = " << s0.length() << ")" << endl;
mp = (0 == s0.length()) ? "OK: " : "ERROR: ";
outstream << mp << "length = " << s0.length() << endl;
mp = (0 != s0.asChar()) ? "OK: " : "ERROR: ";
outstream << mp << "asChar() return value " << hex << "0x" << reinterpret_cast<const int *>(s0.asChar()) << dec << endl;
mp = ('\0' == *(s0.asChar())) ? "OK: null byte " : "ERROR: null byte not ";
outstream << mp << "at position 0" << endl;
}
{
TString s0(copyToTemp(pHello)); // char * parameter
clearTemp();
outstream << endl << "s0 using default ctor with char* parameter = \"" << s0.asChar() << "\" (length = " << s0.length() << ")" << endl;
mp = (s0.length() == strlen(pHello)) ? "OK: " : "ERROR: ";
outstream << mp << "length = " << s0.length() << endl;
mp = (0 == strcmp(s0.asChar(), pHello)) ? "OK: " : "ERROR: ";
outstream << mp << "value is \"" << s0.asChar() << "\"" << endl;
mp = (s0.asChar() != pHello) ? "OK: different " : "ERROR: same ";
outstream << mp << "pointer value returned from asChar()" << endl;
mp = (s0.asChar() == s0.asChar()) ? "OK: same " : "ERROR: different ";
outstream << mp << "pointer value returned for successive calls of asChar()" << endl;
}
// Copy ctor
outstream << "\n----- Step " << ++step << " - copy ctor -----" << endl;
{
TString s0(copyToTemp(pHiMom));
clearTemp();
TString s1(s0); // copy ctor
outstream << "s1 using copy ctor = \"" << s1.asChar() << "\" (length = " << s1.length() << ")" << endl;
mp = (s1.length() == s0.length() && s1.length() == strlen(pHiMom)) ? "OK: " : "ERROR: ";
outstream << mp << "length = " << s1.length() << endl;
mp = (s1.asChar() != s0.asChar()) ? "OK: different " : "ERROR: same ";
outstream << mp << "pointer value returned from asChar()" << endl;
mp = (0 == strcmp(s1.asChar(), pHiMom)) ? "OK: " : "ERROR: ";
outstream << mp << "value is \"" << s1.asChar() << "\"" << endl;
}
{
// Check copy of empty string
TString s0;
TString s1(s0);
outstream << "s1 using copy ctor = \"" << s1.asChar() << "\" (length = " << s1.length() << ")" << endl;
mp = (0 == s1.length()) ? "OK: " : "ERROR: ";
outstream << mp << "length = " << s1.length() << endl;
mp = (0 != s1.asChar()) ? "OK: " : "ERROR: ";
outstream << mp << "asChar() return value " << hex << "0x" << reinterpret_cast<const int *>(s1.asChar()) << dec << endl;
mp = (s1.asChar() != s0.asChar()) ? "OK: different " : "ERROR: same ";
outstream << mp << "pointer value returned from asChar()" << endl;
mp = ('\0' == *(s0.asChar())) ? "OK: null byte " : "ERROR: null byte not ";
outstream << mp << "at position 0" << endl;
}
// append
outstream << "\n----- Step " << ++step << " - append -----" << endl;
//.........这里部分代码省略.........