您的位置:首页 > 其它

类定义(2) 三种inline 声明方式

2014-08-30 09:23 253 查看
using namespace std;
class Screen
{
public:
typedefstring::size_type index;

Screen(index ht,index wd,const string& conts);//非内联函数

Screen(int ht=0,index wd=0) //1.内联
:contents(ht*wd,'A'),cursor(0),height(ht),width(wd)
{}
char Get() const; //2.内联
inline char Get(index r,index c) const; //3.内联
private:
string contents;
string::size_type cursor;
string::size_type height,width;
};
Screen::Screen(index ht,index wd,const string &conts)
:contents(conts),cursor(0),height(ht),width(ht)
{}

inline char Screen::Get() const  //2.显式内联函数
{
return contents[cursor];
}
char Screen::Get(index r,index c) const //3.内联函数
{
index row=r*width;
return contents[row+c];
}
int main()
{
Screen a(10,16,string("hello"));
cout<<a.Get()<<endl;
getchar();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: