作者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