#include <string>
#include<stdlib.h>
using namespace std;
class name {
public:
static const int kk = 16;
int arrar[kk];
enum {num= 5};
int arrs[num];
};
class TextBlock
{
public:
TextBlock(std::string id) :text(id),pText(nullptr),textLength(1),lengthValia(false){}
const char& operator[](std::size_t position) const
{
return text[position];
}
/*char& operator[](std::size_t position)
{
return text[position];
}*/
/*char& operator[](std::size_t position) const
{
return pText[position];
}*/
char& operator[](std::size_t position)
{
return const_cast<char&>(static_cast<const TextBlock&>(*this)[position]);
}
private:
std::string text;
char* pText;
mutable std::size_t textLength;
mutable bool lengthValia;
};
class Filesystem
{
public:
Filesystem();
~Filesystem();
std::size_t nemDisks() const;
private:
};
//extern Filesystem tfs;
Filesystem& tfs()//Filesystem tfs;
{
static Filesystem fs;
return fs;
}
Filesystem::Filesystem()
{
}
Filesystem::~Filesystem()
{
}
std::size_t Filesystem::nemDisks() const
{
return std::size_t();
}
class Directory
{
public:
Directory(int params);
~Directory();
private:
};
Directory::Directory(int params)
{
//std::size_t disks = tfs.nemDisks();//tfs must ;pre tempDir
std::size_t disks = tfs().nemDisks();
}
Directory::~Directory()
{
}
template<class T>
class NamedObject
{
public:
//NamedObject(const char* name, const T& value) :nameValue(name), objectValue(value) {}
//NamedObject(const string& name, const T& value) :nameValue(name), objectValue(value) {}
NamedObject(string& name, const T& value) :nameValue(name), objectValue(value) {}
NamedObject<T>& operator=(NamedObject<T>& t);
private:
string& nameValue;
T objectValue;
};
template<class T>
NamedObject<T>& NamedObject<T>::operator=(NamedObject<T>& t)
{
return t;
}
class Uncopyable {
protected:
Uncopyable() {}
~Uncopyable() {}
private:
Uncopyable(const Uncopyable&);
Uncopyable& operator=(const Uncopyable&);
};
class HomeForSale:private Uncopyable
{
};
class TimeKeeper
{
public:
TimeKeeper();
virtual ~TimeKeeper();
private:
};
//TimeKeeper kepp = TimeKeeper();
//TimeKeeper* getTimeKeeper() {
//return &(kepp);
//}
class AtomicClock :public TimeKeeper {};
class SpecialString :public std::string
{
};
class DBConnection {
public:
void Close() {};
};
class DBConn
{
public:
DBConn();
~DBConn() {
if (!closed)
{
try {
db.Close();
}
catch (...) {
}
}
}
private:
DBConnection db;
bool closed;
};
class Widget
{
public:
Widget();
~Widget();
Widget& operator= (const Widget& rhs)
{
return *this;
}
private:
};
Widget::Widget()
{
}
Widget::~Widget()
{
}
#include <iostream>
using namespace std;
void Print(const TextBlock& ctb);
int main()
{
//provision 2 defines
cout << "sifjkdsjf::" << name::kk ;
&name::kk;
name::num;
//&name::num; error
//provision 3 const
//3.1const numeber funntion
TextBlock tb("hello");
tb[0] = 'x';
std::cout << tb[0];//char& operator[](std::size_t position)
const TextBlock ctb("world");
std::cout << ctb[0];//const char& operator[](std::size_t position) const
Print(ctb);
{
const TextBlock cctb{ "hello" };
//char* pc = &cctb[0];
//*pc = 'j';
}
//4.provision obj
Directory tempDir(1);
//5. provision Constructors
//NamedObject<int> nol("smallest", 2);
//NamedObject<int> no2(nol);
string newDog("xiaohuang");
string oldDog("xiaohei");
NamedObject<int>p(newDog, 2);
NamedObject<int>s(oldDog, 36);
p = s;
//6.provision Explicitly detele copy function
HomeForSale h1;
HomeForSale h2;
//HomeForSale h3(h2);
//h1 = h2;
//7.provision virtual xigou
//TimeKeeper* ptk = getTimeKeeper();
//delete ptk;
//8. procision
DBConn dbc;//nmber closed keyi tijiao baoluo yichang
//9.provision never call virtual function during construction
//10 provision operator = renturn reference to*this
Widget wid;
Widget widget = wid;
cin.get();
return 0;
}
void Print(const TextBlock& ctb)
{
std::cout << ctb[0];
}
effective cpp 1-10
最新推荐文章于 2024-08-24 09:17:08 发布