64,662
社区成员
发帖
与我相关
我的任务
分享
#ifndef CHARREF
#define CHARREF
#include "myString.h"
class char_ref
{
friend class myString;
int i;
myString* s;
char_ref(myString* ss, int ii)
{
s = ss;
i = ii;
}
public:
void operator=(char c)
{
s->r[i] = c;
}
operator char()
{
return s->r[i];
}
};
#endif
#ifndef MYSTRING
#define MYSTRING
#include "char_ref.h"
class myString
{
friend class char_ref;
char *r;
public:
char_ref operator[](int i) //如果将这个方法的定义去掉,是可以编译通过的,难道匿名类不是这么写的?
{
return char_ref(this, i);
}
};
#endif
#include "char_ref.h"
#include "myString.h"
#include <iostream>
using namespace std;
int main()
{
return 0;
}
#include <iostream>
using namespace std;
#ifndef MYSTRING
#define MYSTRING
template<typename T>
class myString
{
friend class T;
T* c;
public:
char *r;
myString(T* t)
{
c = t;
}
myString(char* str): r(str)
{
}
T operator[](int i)
{
return T(this, i);
}
};
#endif
#ifndef CHARREF
#define CHARREF
template<typename T>
class char_ref
{
friend class myString<char_ref>;
int i;
myString<char_ref>* s;
T* t;
public:
char_ref(T* t)
{
lkjwalkgjwalwekjgwlkgjlwe //这边我乱写编译也不会报错, 但如果下面程序调用到了才会报错
this->t = t;
}
char_ref(myString<char_ref>* ss, int ii)
{
s = ss;
i = ii;
}
public:
void operator=(char c)
{
s->r[i] = c;
}
operator char()
{
return s->r[i];
}
};
#endif
int main()
{
int i;
char s[] = {"hello world"};
int nLen = strlen(s);
//char_ref<char> c("test"); //将这个注释去掉会报错
myString<char_ref<char> > m(s);
for(i = 0; i < nLen; i++)
{
cout << (char)m[i];
}
cout << endl;
m[4] = 'p';
m[nLen-1] = 'u';
for(i = 0; i < nLen; i++)
{
cout << (char)m[i];
}
cout << endl;
return 0;
}
char_ref myString::operator[](int i)
{
return char_ref(this, i);
}
#include <iostream>
using namespace std;
#ifndef MYSTRING
#define MYSTRING
template<typename T> class char_ref;
template<typename T>
class myString
{
friend class T;
char *r;
public:
T operator[](int i) //如果将这个方法的定义去掉,是可以编译通过的,难道匿名类不是这么写的?
{
return T(this, i);
}
};
#endif
#ifndef CHARREF
#define CHARREF
template<typename T>
class char_ref
{
friend class myString<char_ref>;
int i;
myString<char_ref>* s;
public:
char_ref(myString<char_ref>* ss, int ii)
{
s = ss;
i = ii;
}
public:
void operator=(char c)
{
s->r[i] = c;
}
operator char()
{
return s->r[i];
}
};
#endif
int main()
{
return 0;
}
class myString;
class char_ref
{
friend class myString;
int i;
myString* s;
char_ref(myString* ss, int ii);
public:
void operator=(char c);
operator char();
};
class myString
{
friend class char_ref;
char *r;
public:
myString(char* str)
: r(str)
{}
char_ref operator[](int i);
};
//-------------------------------------
// char_ref定义
//-------------------------------------
char_ref::char_ref(myString* ss, int ii)
{
s = ss;
i = ii;
}
void char_ref::operator=(char c)
{
s->r[i] = c;
}
char_ref::operator char()
{
return s->r[i];
}
//-------------------------------------
// myString定义
//-------------------------------------
char_ref myString::operator[](int i)
{
return char_ref(this, i);
}
char s[] = {"hello world"};
myString mstr(s);
cout << mstr[3] << endl;