printf("ho_tari\n");
StringRep (reference counting 참조 계수) 본문
<main.cpp>
#include <iostream>
#include "string.h"
int main()
{
String s1;
String s2 = "hello, world"; // String s2("hello, world");
//String s2 = 0;
String s3 = s2; // String s3(s2);
s1 = s2;
// s1 = s1; // self assignment??
s1 = "hello, "; // String tmp("hello, "); s1 = tmp; tmp.~String();
String s4 = " IoT";
s1 = s2 + s4;
if (s1 == s2)
{
std::cout << "s1 and s2 are equal" << std::endl;
}
else
{
std::cout << "s1 and s2 are not equal" << std::endl;
}
s4 += s4;
s1 != s2;
std::cout << "s1 : " << s1.c_str() << std::endl;
std::cout << "s2 : " << s2.c_str() << std::endl;
std::cout << "s2 len : " << s2.size() << std::endl;
std::cout << s3 << std::endl;
const String s5 = "hello";
//s5 = "world";
std::cout << "s3 len : " << s5.size() << std::endl;
return 0;
}
<string.h>
#ifndef STRING_H
#define STRING_H
#include <iostream>
#include "stringRep.h"
class String {
friend class StringRep;
friend std::ostream& operator<<(std::ostream& out, const String& rhs);
private:
//char *str;
//int len;
//void init_str(const char *str); // tool func. helper func.
StringRep *rep_;
public:
//String();
String(const char *str = 0);
String(const String& rhs);
~String();
String& operator=(const String& rhs);
String& operator=(const char *str);
String& operator+=(const String& rhs);
bool operator==(const String& rhs) const; // const member func.
bool operator!=(const String& rhs) const;
// +
const String operator+(const String& rhs) const;
const char *c_str() const;
int size() const;
};
#endif
<string.cpp>
#include "string.h"
#include <cassert>
#include <cstring>
std::ostream& operator<<(std::ostream& out, const String& rhs)
{
//return out << rhs.rep_->str_;
return out << rhs.c_str();
}
String::String(const char *str)
{
rep_ = new StringRep(str);
rep_->rc_ = 1;
}
String::String(const String& rhs)
{
rep_ = rhs.rep_;
++rep_->rc_;
}
String::~String()
{
--rep_->rc_;
if (rep_->rc_ == 0)
{
delete rep_;
}
}
/*
void String::init_str(const char *str)
{
if (str )
{
this->str = new char[strlen(str) + 1];
assert(this->str );
strcpy(this->str, str);
this->len = strlen(str);
}
else
{
this->str = new char[1];
assert(this->str );
this->str[0] = '\0';
this->len = 0;
}
}
*/
String& String::operator=(const String& rhs)
{
if (this != &rhs) // self-assignment test!
{
--rep_->rc_;
if (rep_->rc_ == 0)
{
delete rep_;
}
rep_ = rhs.rep_;
++rep_->rc_;
}
return *this;
}
String& String::operator=(const char *str)
{
--rep_->rc_;
if (rep_->rc_ == 0)
{
delete rep_;
}
rep_ = new StringRep(str);
rep_->rc_ = 1;
return *this;
}
String& String::operator+=(const String& rhs)
{
*this = *this + rhs;
return *this;
}
bool String::operator==(const String& rhs) const
{
return strcmp(rep_->str_, rhs.rep_->str_) == 0;
}
bool String::operator!=(const String& rhs) const
{
return !this->operator==(rhs);
}
const String String::operator+(const String& rhs) const
{
char *buf = new char[rep_->len_ + rhs.rep_->len_ + 1];
assert(buf );
strcpy(buf, rep_->str_);
strcat(buf, rhs.rep_->str_);
String result(buf);
delete [] buf;
return result;
}
const char *String::c_str() const
{
return rep_->str_;
}
int String::size() const
{
return rep_->len_;
}
<stringRep.h>
#ifndef STRINGREP_H
#define STRINGREP_H
class StringRep {
friend class String;
private:
char *str_;
int len_;
int rc_;
StringRep(const StringRep& rhs);
StringRep& operator=(const StringRep& rhs);
public:
StringRep(const char *str = 0);
~StringRep();
};
#endif
<stringRep.cpp>
#include "stringRep.h"
#include <cassert>
#include <cstring>
StringRep::StringRep(const char *str)
{
if (str )
{
str_ = new char[strlen(str) + 1];
assert(str_ );
strcpy(str_, str);
len_ = strlen(str);
}
else
{
str_ = new char[1];
assert(str_ );
str_[0] = '\0';
len_ = 0;
}
}
StringRep::StringRep(const StringRep& rhs)
{
}
StringRep::~StringRep()
{
delete [] str_;
}
<compile 결과>
'C++' 카테고리의 다른 글
Linked List (0) | 2023.09.05 |
---|---|
String (클래스 개선) (0) | 2023.09.05 |
Complex (istream 입력 함수) (0) | 2023.09.05 |
Rational3 (istream 입력 함수) (0) | 2023.09.05 |
add (0) | 2023.09.05 |