The Binding of a Data Member
1523 ワード
『深さ探索C++対象モデル』第三章「Data語意学」
3.1 The Binding of a Data Member
次のコードの出力は何ですか?
答えは:123
では、次のコードは?
答えは:123.3
上記の例から分かるように、防御的なプログラミングスタイルとして、例2に示すように、クラス内のtypedefを一番前に置くことを提案する.
最後のコードを見て、どんな出力になりますか?
答えは:コンパイルに失敗しました
member_binding.cpp:7: error: ‘len_t’ has not been declared
3.1 The Binding of a Data Member
次のコードの出力は何ですか?
[xiaochu.yh@OceanBase cpp]$ cat member_binding.cpp
#include <iostream>
using namespace std;
typedef int len_t;
class Point
{
public:
void mum(len_t val) { _val = val; cout << _val << endl; }
private:
typedef float len_t;
len_t _val;
};
int main()
{
Point p;
p.mum(123.3);
return 0;
}
答えは:123
では、次のコードは?
[xiaochu.yh@OceanBase cpp]$ cat member_binding.cpp
#include <iostream>
using namespace std;
typedef int len_t;
class Point2
{
private:
typedef float len_t;
public:
void mum(len_t val) { _val = val; cout << _val << endl; }
private:
len_t _val;
};
int main()
{
Point2 p2;
p2.mum(123.3);
return 0;
}
答えは:123.3
上記の例から分かるように、防御的なプログラミングスタイルとして、例2に示すように、クラス内のtypedefを一番前に置くことを提案する.
最後のコードを見て、どんな出力になりますか?
[xiaochu.yh@OceanBase cpp]$ cat member_binding.cpp
#include <iostream>
using namespace std;
class Point
{
public:
void mum(len_t val) { _val = val; cout << _val << endl; }
private:
typedef float len_t;
len_t _val;
};
int main()
{
Point p;
p.mum(123.3);
return 0;
}
答えは:コンパイルに失敗しました
member_binding.cpp:7: error: ‘len_t’ has not been declared