我很少有带有长名称的类及其成员函数。在C ++中,有一个技巧可以让您使用一个名称空间并声明函数:
$> rebar3 shell
===> Verifying dependencies...
===> Compiling bar
===> Compiling foo
Eshell V9.3 (abort with ^G)
1> ===> The rebar3 shell is a development tool; to deploy applications in production, consider using releases (http://www.rebar3.org/docs/releases)
1> ===> Booted bar
1> ===> Booted foo
1> ===> Booted sasl
1>
=PROGRESS REPORT==== 26-Jul-2018::22:07:44 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.139.0>},
{id,alarm_handler},
{mfargs,{alarm_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
1> 1>
=PROGRESS REPORT==== 26-Jul-2018::22:07:44 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.138.0>},
{id,sasl_safe_sup},
{mfargs,
{supervisor,start_link,
[{local,sasl_safe_sup},sasl,safe]}},
{restart_type,permanent},
{shutdown,infinity},
{child_type,supervisor}]
1> 1>
=PROGRESS REPORT==== 26-Jul-2018::22:07:44 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.140.0>},
{id,release_handler},
{mfargs,{release_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
1> 1>
=PROGRESS REPORT==== 26-Jul-2018::22:07:44 ===
application: sasl
started_at: nonode@nohost
2> code:lib_dir(foo).
"/usr/home/xxxx/foo/_build/default/lib/foo"
3> code:lib_dir(bar).
"/usr/home/xxxx/foo/_checkouts/bar"
4>
并这样定义它:
namespace_name::foo();
为清楚起见,我想知道是否存在类似的方法来替代函数的定义:
namespace namespace_name{
foo() {}
}
很抱歉,如果我使用的词汇不正确,但不知道如何描述问题。
答案 0 :(得分:2)
在C ++中,有一个技巧可以让您使用一个名称空间并转向
此“技巧”使您可以在不使用名称空间指定完整函数名称的情况下调用函数。对于既不适用于名称空间也不适用于类的函数定义。对于名称空间级别的函数,您要么必须将该函数定义放入名称空间中,要么要明确提及它:
const btn = document.querySelector('#btn');
const click$ = Rx.Observable.fromEvent(btn, "click");
const trigger$ =
click$.exhaustMap(r =>
click$
.take(2)
.last()
.race(click$
.startWith(0)
.debounceTime(500)
.take(1))
);
click$
.buffer(trigger$)
.map(l => l.length)
.map(x => ({
1: 'single',
2: 'double',
3: 'tripple'
}[x]))
.map(c => c + '-click')
.subscribe(log);
Rx.Observable.fromEvent(document.querySelector('#reset'), 'click')
.subscribe(clearLog);
function log(message) {
document.querySelector('.log-content').innerHTML += ('<div>' + message + '</div>');
}
function clearLog() {
document.querySelector('.log-content').innerHTML = null;
}
用于类方法定义-必须始终使用类名称(如果有的话,也可以使用名称空间),除非您在类本身内部定义它们(并隐式声明.log {
padding: 1rem;
background-color: lightgrey;
margin: 0.5rem 0;
}
.log-content {
border-top: 1px solid grey;
margin-top: 0.5rem;
min-height: 2rem;
}
.log-header {
display: flex;
justify-content: space-between;
}
):
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.7/Rx.js"></script>
<button id='btn'>Click Me</button>
<button type="button" id='reset'> Reset </button>
<div class='log' id='logpanel'>
<div class='log-header'>
Log:
</div>
<div class="log-content"></div>
</div>