發信人: loom.bbs@bbs.sjtu.edu.cn (良心大大的好), 信區: C
標 題: C++謎語
發信站: 飲水思源站 (Sun Jul 6 18:40:55 1997)
出 處: bbs.sjtu.edu.cn
謎語1:
下面的代碼對嗎? 對的是什麼意思?
struct foo {
operator int();
int operator()();
operator()(int);
};
謎語2:
下面的代碼對嗎? 如果是正確的,是什麼意思?
#include <iostream.h>
int main()
{
cout << 3 != 2 << 1;
}
謎語3:
下面的代碼試圖計算goo在bar中的偏移量,能成功嗎?如果
不成功,應該怎樣做呢?
#include <iostream.h>
class foo { long m_foo[3]; };
class goo { long m_goo[5]; };
class bar : public foo, public goo { long m_bar[2]; };
int main()
{
bar* x = NULL;
goo* y = x;
cout << long(y)-long(x) << endl;
}
謎語4□
程式碼正確嗎?□
#include <iostream.h>
class foo {
public:
char m_value;
foo(const char value) : m_value(value) {}
};
class goo : public foo {
public:
goo(const char value) : foo(value) {}
};
class bar : public foo, public goo {
public:
bar() : foo('A'), goo('B') {}
};
int main()
{
bar x;
cout << x.m_value << endl;
}
謎語5:
上述代碼中,如果將foo改為goo和bar的虛基類,輸出結果是什麼?
--
愛是恆久忍耐 又有恩慈 愛是不嫉妒 愛是不自誇 不張狂 ◢█◣◢█◣
不做害羞的事 不求自己的益處 不輕易發怒 ██████
不計算人的惡 不喜歡不義 只喜歡真理 ◥████◤
凡事包容 凡事相信 凡事盼望 凡事忍耐 愛是永不止息 ◥██◤
◥◤
--
Origin: 成大資工BBS站 (vlsi1.csie.ncku.edu.tw) From: p18006.ts.ncku.edu.tw
> -------------------------------------------------------------------------- <
發信人: Email.bbs@vlsi1.iie.ncku.edu.tw ( ), 看板: Programming
標 題: [轉貼] C++ 謎語 PART II
發信站: 成大資訊所_BBS (Tue Jan 12 05:36:54 1999)
轉信站: Ptt!news.ntu!ctu-gate!news.nctu!netnews.csie.nctu!netnews2.csie.nctu!n
發信人: loom.bbs@bbs.sjtu.edu.cn (良心大大的好), 信區: C
標 題: C++謎語
發信站: 飲水思源站 (Sun Jul 6 19:46:25 1997)
出 處: bbs.sjtu.edu.cn
謎語6:
下面程序的輸出是什麼?
#include <iostream.h>
class foo {
public:
void Print() { cout << "non-const\n"; }
void Print() const { cout << "const\n"; }
};
typedef foo * bar;
int main()
{
foo x;
x.Print();
((const foo *)&x)->Print();
((foo const *)&x)->Print();
((foo * const)&x)->Print();
((const bar)&x)->Print();
((bar const)&x)->Print();
}
謎語7:
下面代碼的輸出是?
#include <iostream.h>
void Type(int)
{
cout << "Is integer" << endl;
}
void Type(void *)
{
cout << "Is pointer" << endl;
}
void main()
{
Type(NULL);
}
如果將第一個函數的參數改為double類型,結果是什麼呢?
謎語8:□(easy!)
C++中指針和數組關系密切,有人說數組和指針唯一的不同是數組變量不可
賦值,而指針可以,如:
int a[10];
int * b;
b = a可以而a = b非法。
但若定義int * const b;
你能寫出一個表達式(不是函數也不包含函數)將a和b區分開嗎?也
就是:
expr(a) != expr(b)
--
愛是恆久忍耐 又有恩慈 愛是不嫉妒 愛是不自誇 不張狂 ◢█◣◢█◣
不做害羞的事 不求自己的益處 不輕易發怒 ██████
不計算人的惡 不喜歡不義 只喜歡真理 ◥████◤
凡事包容 凡事相信 凡事盼望 凡事忍耐 愛是永不止息 ◥██◤
◥◤
--
Origin: 成大資工BBS站 (vlsi1.csie.ncku.edu.tw) From: p18006.ts.ncku.edu.tw
> -------------------------------------------------------------------------- <
作者: march20 (澄) 看板: Programming
標題: Re: [轉貼] C++ 謎語
時間: Tue Jan 12 08:44:54 1999
※ 引述《Email.bbs@vlsi1.iie.ncku.edu.tw ( )》之銘言:
呵, 先猜兩個
: 謎語1:
: 下面的代碼對嗎? 對的是什麼意思?
: struct foo {
: operator int();
: int operator()();
: operator()(int);
: };
以上全對
: 謎語2:
: 下面的代碼對嗎? 如果是正確的,是什麼意思?
: #include <iostream.h>
: int main()
: {
: cout << 3 != 2 << 1;
: }
1.int main() 沒傳回值, 可能會 warning
2.依 precedence cout << 3 != 2 << 1; 會是
(cout << 3) != (2 << 1);
會變成
(cout) != (4);
這時 cout 會 cast 成 void *,
所以這一句會先印出 3;
3.不過 void * != int;
這種句子可能會 type mistake, 所以要 (int )(cout << 3)
接下來的部份沒什麼 實質上的作用
--
※ 發信站: 批踢踢實業坊(ptt.twbbs.org)
◆ From: h72.s93.ts30.hi
> -------------------------------------------------------------------------- <
作者: march20 (澄) 看板: Programming
標題: Re: [轉貼] C++ 謎語 PART II
時間: Tue Jan 12 10:38:22 1999
※ 引述《Email.bbs@vlsi1.iie.ncku.edu.tw ( )》之銘言:
: 發信人: loom.bbs@bbs.sjtu.edu.cn (良心大大的好), 信區: C
: 標 題: C++謎語
: 發信站: 飲水思源站 (Sun Jul 6 19:46:25 1997)
: 出 處: bbs.sjtu.edu.cn
: 謎語6:
: 下面程序的輸出是什麼?
: #include <iostream.h>
: class foo {
: public:
: void Print() { cout << "non-const\n"; }
: void Print() const { cout << "const\n"; }
: };
: typedef foo * bar;
: int main()
: {
: foo x;
: x.Print();
x is non-const
: ((const foo *)&x)->Print();
const foo * is pointer to const foo
: ((foo const *)&x)->Print();
foo const * is pointer to const foo
: ((foo * const)&x)->Print();
foo * const is const pointer to non-const foo
: ((const bar)&x)->Print();
const bar is a const pointer to non-const foo
: ((bar const)&x)->Print();
bar const is a const pointer to non-const foo
: }
so the above oupt will be
non-const
const
const
non-const
non-const
non-const
: 謎語7:
: 下面代碼的輸出是?
: #include <iostream.h>
: void Type(int)
: {
: cout << "Is integer" << endl;
: }
: void Type(void *)
: {
: cout << "Is pointer" << endl;
: }
: void main()
: {
: Type(NULL);
: }
NULL is #defined as 0,
so "Is integer" will be printed
: 如果將第一個函數的參數改為double類型,結果是什麼呢?
0 不是 double 型別, compiler 這時就要 cast 了,
不過 void * 和 double 同時可選,
so ambigious will occur !!!
compile error !!
: 謎語8:□(easy!)
: C++中指針和數組關系密切,有人說數組和指針唯一的不同是數組變量不可
: 賦值,而指針可以,如:
: int a[10];
: int * b;
: b = a可以而a = b非法。
: 但若定義int * const b;
: 你能寫出一個表達式(不是函數也不包含函數)將a和b區分開嗎?也
: 就是:
: expr(a) != expr(b)
sizeof(a) == 20, sizeof(b) == 2;
sizeof(a) != sizeof(b);
--
※ 發信站: 批踢踢實業坊(ptt.twbbs.org)
◆ From: aug.csie.ntu.ed