2017-06-21 12 views
2

クラスメンバー/関数でtypedefを実行できますか?以下の例では、boost bimap関数を使用して、ノードの最近隣の情報を格納しています。クラスメンバーのtypedef

typedef boost::bimap<float /*distance*/, int /*id*/> neighbor_list; 
neighbor_list node_a; 

//fill up neighbors of node_a 

//get nearest neighbor of node_a 
node_a.neighbor.left.begin()->second; 

//get distance to the nearest neighbor of node_a 
node_a.neighbor.left.begin()->first; 

しかし、上記の行は乱雑で直感的ではないかもしれません。私が、私はコードの乱雑一部をカプセル化しますが、私はだった自分自身の関数を書くことができます知っている次

typedef boost::bimap<float /*distance*/, int /*id*/> neighbor_list; 
typedef neighbor_list::left::begin()->first nearest_neighbor; 

//nearest neighbor of node_a 
node_a.nearest_neighbor; 

のような何かができるように、クラスメンバーのtypedefを行うことが可能である場合ので、私は思っていました私がクラスメンバーに別名を与えることができるかどうか疑問に思います。

+0

「typedef」はタイプ(驚き)です。関数は型ではありません。 –

+0

しかし、意図した動作を別の関数でラップすることができます。これにより 'nearest_neighbor(node_a)'と入力することができます。 –

+0

こんにちは私はそれを認識しています。私はまだC++の深いところを掘り下げていないので、私が求めたことをする方法があるかどうか疑問に思っていた。 – kong

答えて

1

ちょうど関数に厄介な解決を委譲してください。

#include <boost/bimap.hpp> 

typedef boost::bimap<float /*distance*/, int /*id*/> neighbor_list; 

float nearest_neighbor(neighbor_list const& node) 
{ 
    return node.neighbor.left.begin()->first; 
} 

void foo() 
{ 
    neighbor_list node_a; 

    nearest_neighbor(node_a); 
}