例如,使用以下代码:
function calculatingDistance(fromLat,fromLong,toLat,toLong){
var distanceDurationObject={};
var options = {
host: 'maps.googleapis.com',
path: '/maps/api/directions/json?origin='+fromLat+','+fromLong+'&destination='+toLat+','+toLong+'&key=AIzaSyBsoc1PZOItqHNYc5z2hW_ejPFi2piRR8Y',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
var req = https.get(options, function(res) {
//buffering alll data
var bodyChunks = [];
res.on('data', function(chunk) {
// You can process streamed parts here...
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
json = JSON.parse(body);
//parsing json returned
var routes =json["routes"];
var legsObject=routes[0].legs;
var legsFirstArray=legsObject[0];
//Our Required Distance between two points
var distanceValue=legsFirstArray.distance.value;
var durationValue=legsFirstArray.duration.value;
console.log(distanceValue+" "+durationValue);
distanceDurationObject.distance=distanceValue;
distanceDurationObject.duration=durationValue;
});
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
//try to add this error to a file
});
}
请注意' Quux' class有两个模板参数 - 一个也是' Bar'的模板参数。 class,以及对// Say I have this class defined in some other file
class Foo;
// This class will act as a wrapper for an integer map
// to function pointers, which will create type TFoo objects
// depending on the given input (in this case a "const char*"
template<class TFoo>
struct Bar
{
typedef TFoo foo_t;
typedef TFoo (*get_foo_f_t)(const char*);
typedef std::unordered_map<int, get_foo_f_t> foo_handler_map_t;
Bar(const foo_handler_map_t& handlers)
: handlers_(handlers)
{
}
~Bar()
{
}
const foo_handler_map_t& handlers_;
};
// Now, this class will receive an _object_ of type
// "const Bar<T>&", which will have an already initialized
// map of integers to function pointers, different
// functions will be called with different input values
// via the public method, "action()".
template<class TFoo, const Bar<TFoo>& CBar>
class Quux
{
public:
Quux()
: bar_(CBar)
{
}
~Quux()
{
}
TFoo action(int a, const char* x)
{
auto it = this->bar_.handlers_.find(a);
if (it == this->bar_.handlers_.end())
{
// no handler defined for int `a'
return TFoo();
}
// i.e. CBar.handlers_[a](x)
return it->second(x);
}
private:
const Bar<TFoo>& bar_;
};
// Here is how the map of integers to function pointers
// will be initialized...
static std::unordered_map<int, Foo (*)(const char*)> handlers
{
{ 0, _hdl_0 }, // _hdl_* functions defined in different file
{ 1, _hdl_1 },
{ 2, _hdl_2 }
};
// And then passed to a "const Bar<T>" type object here
const Bar<Foo> bar (handlers);
int main()
{
// --> HERE IS WHAT I WANT TO CHANGE <--
Quux<decltype(bar)::foo_t, bar> quux;
// -------------------------------------
// Example (trivial) use of the 'quux' object
std::cout << quux.action(0, "abc").baz() << std::endl;
std::cout << quux.action(1, "def").baz() << std::endl;
std::cout << quux.action(2, "ghi").baz() << std::endl;
return 0;
}
类型的模板对象的引用,其中T是与&#39; Foo&#39;相关的任何类。我希望能够做到以下几点:
const Bar<T>
注意:&#39; bar&#39;是Quux<bar> quux;
类型的对象,但它也应该是任何Bar<Foo>
类型。
这可能吗?我当时认为下面的内容可能会被用作快速解决方法,但我无法弄清楚应该用什么代替Bar<T>
:
/* ??? */
修改的
我将对象的引用传递给了Quux&#39;作为模板参数,因为复制效率低(我认为),而不是复制整个template<const Bar</* ??? */>& CBar>
using Nuff = Quux<decltype(CBar)::foo_t, CBar>
Nuff<bar> nuff;
对象。我只希望能够拥有一堆foo_handler_map_t
类型的对象,这些对象在某个命名空间中全局定义,并且能够初始化&#39; Quux&#39;像这样的对象:
const Bar<T>
...而且我不想将它作为构造函数参数传递。
答案 0 :(得分:2)
评论非常有用。但是,如果您希望减少模板参数的数量,可以将CBar
作为参数传递给构造函数:
template<class TFoo>
class Quux
{
public:
Quux(const Bar<TFoo>& CBar)
: bar_(CBar)
{}
~Quux()
{}
TFoo action(int a, const char* x)
{
auto it = this->bar_.handlers_.find(a);
if (it == this->bar_.handlers_.end())
{
return TFoo();
}
return it->second(x);
}
private:
const Bar<TFoo>& bar_;
};
定义一个函数来创建Quux
的实例:
template <typename TFoo>
auto make_Quux(const Bar<TFoo>& bar)
{
return Quux<TFoo>(bar);
}
然后在main()
中,您可以使用make_Quux()
:
int main()
{
auto quux = make_Quux(bar);
//...
}