コード例 #1
0
// copy constructor
    // initializes this MyString to a deep copy of the original
MyString::MyString(const MyString & original)
{
	this->_capacity = original.CurrentCapacity();
	this->_length = original.Length();

	this->_string = new char[this->_capacity + 1];
	strcpy(_string, original._cstr());
	//strcpy_s(_string, _capacity + 1, original._cstr());
	//_string[_length + 1] = '\0';
}
コード例 #2
0
ファイル: Test_MyString.cpp プロジェクト: chrbilbo/MyString
int main()
{
	MyString m;

	assertEqual(16, m.CurrentCapacity(), "Default ctor: sets capacity to 16");
	assertEqual(0, m.Length(), "Default ctor: sets length to 0");
	assertEqual("", m._cstr(), "Default ctor: sets string to empty string");


	m.Assign("Bob");
	cout << "capacity is " << m.CurrentCapacity() << endl;
	cout << "length is " << m.Length() << endl;
	cout << "string points to an ascii " << (m._cstr()) << endl;


	m.Assign("123456789012345678901234567890");

	assertTrue(m.CurrentCapacity() >= 30, "Assign('123456789012345678901234567890': capacity >= 30");
	assertEqual(30, m.Length(), "Assign('123456789012345678901234567890': sets length to 30");
	assertEqual("123456789012345678901234567890", m._cstr(), "Assign('123456789012345678901234567890')");
	char buffer[100] = "";


	cout << m << endl;
	cout << "Enter some text: ";
	cin >> m;
	cout << m << endl;

	MyString m2;
	cout << "Enter some text: ";
	cin >> m2;
	cout << m2 << endl;


	system("pause");
	return 0;
}
コード例 #3
0
// copy constructor
// initializes this MyString to a deep copy of the original
MyString::MyString(const MyString & original)
{
	// Gain manipulatable version of the original MyString's _string
	const char * originalString = original._cstr();

	// Set numeric values
	_capacity = original.CurrentCapacity();
	_length = original.Length();
	_string = new char[_capacity];

	// Deep copy
	for (int i = 0; i < _length; i++)
	{
		_string[i] = originalString[i];
	}

	_string[_length] = '\0';
}
コード例 #4
0
// = (assignment - takes a MyString or a c style string)
MyString MyString::operator= (const MyString & aMyString)
{
	// Get the numeric attributes
	(*this)._length = aMyString.Length();
	(*this)._capacity = aMyString.CurrentCapacity();

	// Restart this's _string
	delete[] (*this)._string;
	(*this)._string = new char[(*this)._capacity];

	// Deep copy
	for (int i = 0; i < (*this).Length(); i++)
	{
		(*this)._string[i] = aMyString._cstr()[i];
	}

	// Add null character
	(*this)._string[(*this)._length] = '\0';

	return *this;
}