作者ric2k1 (Ric)
看板EE_DSnP
標題Re: [問題] 想用 static const 製作 end 遇到的問題
時間Thu May 21 07:21:59 2009
※ 引述《FATCLOUD (A Cloud)》之銘言:
: 在這次作業的 BSTree 裡面
: 我想要給所有的 BSTree 共用一個獨立不變的 _end node
: 當有人呼叫 end() 的時候就回傳它
: 於是就宣告了 static const BSTreeNode<T> _end;
: 但是奇怪的問題來了
: 我無法初始化他
: static const BSTreeNode<T> _end = BSTreeNode<T>(..參數..);
1. Initialize 時不需要 static
2. 既然是 const, 就不能用 = 來 initialize...
Please see this example:
========================
#include <iostream>
using namespace std;
class A
{
public:
A(int i): _data(i) {}
int getData() const { return _data; }
private:
int _data;
};
class B
{
public:
static const A _a;
};
const A B::_a(10);
int main()
{
cout << B::_a.getData() << endl;
}
: 也不能使用 BSTreeNode<T> x = &_end 這種簡單的東西
: 後來我改成
&_end 就變成 const BSTreeNode<T>* 了吧?
: static const BSTreeNode<T>* _end
: 仍然不能用 BSTreeNode<T>* x = _end;
const BSTreeNode<T>* x = _end;
: 我想問一下
: const 到底有哪些限制還能怎麼用
: 我覺得我想做的事很簡單
: 可是被各種規定擋住不知道要怎麼做 ~"~
不過用 static 來宣告 _end OK 嗎?
這樣子所有的 bst 都會看到同一個 _end, 這是你要的嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.224.43.127