C++ Builder

La Isla Bonita



CString class
CString class


Description

Due to the fact that C strings do not have a generic assignmemt operator (only at the point where the string is declared), we have to use some functions such as strncpy() to copy a specified number of bytes from one string into another. C++ allow us to simplify the way we manipulate C strings by defining a class that implements the assignmemt operator. I will call this class CString.

This class will allow us to write something like this

    // A C string is declared and defined
    char string1[100] = "Borland C++ Builder is a great compiler";

    CString Cs;      // A CString is declared (Defaulf Constructor is called)
    Cs = string1;    // A C string is copied into a CString. 
We can now copy a C string into a CString simply by using the = sign. Now we don't have to worry about the details on how to copy a string, the assignment operator does it for us. Of course, we have used strncpy() to write the assignment operator function, but once this function is written, we can use it without thinking on the details of its implementation. Thanks to the CString class, we have simplified the process of copying a C string into a CString.

Code

CString.h


#ifndef CStringH
#define CStringH
//-----------------------------------------------------------------------
const int MAX_LEN = 32000;

class CString
{
  private:
    char string[MAX_LEN];

  public:
    CString();     // Default Constructor

    CString(const char *s);       // One Argument Constructor

    CString &operator = (const char *s);  // assignment operator

    String AnsiString();// Conversion function
};
//----------------------------------------------------------------------
#endif

CString.cpp

#include <vcl.h>
#pragma hdrstop

#include "CString.h"
#pragma package(smart_init)

CString::CString()
{
// Default Constructor

    string[0] = 0;
}

CString::CString(const char *s)
{
// One argument constructor
// Constructor used to initialize objects which are created like this:
// CString o("12345678901234567890");
// Otherwise we get a compiler error:
// [C++ Error] E2285 Could not find a match for 'CString::CString(char *)'

    strncpy(string, s, MAX_LEN-1);
}

CString& CString::operator = (const char *s)
{
// The assignment operator will allow us to do this
// char string1[100] = "Borland C++ Builder is a great compiler";
// CString Cs;
// Cs = string1;

    strncpy(string, s, MAX_LEN-1);
    return *this;
}

String CString::AnsiString()
{
// This member function will allow us to convert a C string
// into an AnsiString: Cs.AnsiString();

    return String(string);
}

//----------------------------------------------------------------------

Unit1.cpp

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "CString.h"
//----------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//----------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//----------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    char string1[100] = "Borland C++ Builder is a great compiler";
    char string2[100] = "Cool people use it a lot";

    CString Cs; // Defaulf Constructor is called
    Cs = string1;
    Label1->Caption = Cs.AnsiString();

    Cs = string2;
    Label2->Caption = Cs.AnsiString();

    // One argument Constructor is called
    CString o(" because it is the best");

    Label3->Caption = o.AnsiString();
    Label4->Caption = Cs.AnsiString() + o.AnsiString();
}
//----------------------------------------------------------------------


Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.