对于此示例Java类:
package foo;
public class TestInterop
{ public String test(int i)
{ return "Test(int)"; }
public String test(Object i)
{ return "Test(Object)"; }
}
Run Code Online (Sandbox Code Playgroud)
当我启动Clojure并尝试调用test(int)方法时,将调用test(Object)方法,因为Clojure会自动将整数写入java.lang.Integer对象.
如何强制Clojure调用test(int)方法?
user=> (.test (new foo.TestInterop) 10)
"Test(Object)"
Run Code Online (Sandbox Code Playgroud)
我想调用类似于Component.add(Component comp, int index)AWT的方法,而是继续调用add(Component comp, Object constraints),因此工具栏上的按钮总是以错误的顺序出现.
在派生类中如果我从Base类重新定义/重载函数名,
那么这些重载函数对派生类是不可访问/可见的.
为什么是这样??
如果我们不在派生类的基类中重载oveloaded函数,那么该函数的所有重载版本都可用于派生类
对象,为什么这个?
这背后的原因是什么?如果您在编译器和链接器级别解释这
将对我更有帮助.是不是可以支持这种scinario?
Edited
For examble:
class B
{
public:
int f() {}
int f(string s) {}
};
class D : public B
{
public:
int f(int) {}
};
int main()
{
D d;
d.f(1);
//d.f(string); //hidden for D
}
Now object 'd' can't access f() and f(string). 我有一些C++代码(由其他人编写)似乎调用了错误的函数.情况如下:
UTF8InputStreamFromBuffer* cstream = foo();
wstring fn = L"foo";
DocumentReader* reader;
if (a_condition_true_for_some_files_false_for_others) {
reader = (DocumentReader*) _new GoodDocumentReader();
} else {
reader = (DocumentReader*) _new BadDocumentReader();
}
// the crash happens inside the following call
// when a BadDocumentReader is used
doc = reader->readDocument(*cstream, fn);
Run Code Online (Sandbox Code Playgroud)
条件为真的文件处理正常; 它是虚假崩溃的那些.DocumentReader的类层次结构如下所示:
class GenericDocumentReader {
virtual Document* readDocument(InputStream &strm, const wchar_t * filename) = 0;
}
class DocumentReader : public GenericDocumentReader {
virtual Document* readDocument(InputStream &strm, const wchar_t * filename) {
// some stuff …Run Code Online (Sandbox Code Playgroud) 我的域名中有FinanceRequests和CommisionTransactions.如果我有一个FinanceRequests列表,每个FinanceRequest可能包含多个需要回收的CommisionTransactions.不要担心这是怎么做到的.
下面的课程(非常底层)让我感觉自己的模糊和温暖,因为它很好地重用现有的代码.一个问题类型擦除.
public void clawBack(Collection<FinanceRequest> financeRequestList)
public void clawBack(Collection<CommissionTrns> commissionTrnsList)
Run Code Online (Sandbox Code Playgroud)
它们在擦除后都具有相同的签名,即:
Collection<FinanceRequest> --> Collection<Object>
Collection<CommissionTrns> --> Collection<Object>
Run Code Online (Sandbox Code Playgroud)
所以eclipse抱怨说:
方法clawBack(Collection)具有相同的擦除clawBack(Collection)作为类型为CommissionFacade的另一种方法
有没有重组这个的建议,以便它仍然是一个优秀的解决方案,可以很好地重用代码?
public class CommissionFacade
{
/********FINANCE REQUESTS****************/
public void clawBack(FinanceRequest financeRequest)
{
Collection<CommissionTrns> commTrnsList = financeRequest.getCommissionTrnsList();
this.clawBack(commTrnsList);
}
public void clawBack(Collection<FinanceRequest> financeRequestList)
{
for(FinanceRequest finReq : financeRequestList)
{
this.clawBack(finReq);
}
}
/********COMMISSION TRANSACTIOS****************/
public void clawBack(CommissionTrns commissionTrns)
{
//Do clawback for single CommissionTrns
}
public void clawBack(Collection<CommissionTrns> commissionTrnsList)
{
for(CommissionTrns commTrn : commissionTrnsList)
{
this.clawBack(commTrn);
}
}
}
Run Code Online (Sandbox Code Playgroud) 为什么这不允许和被视为相同的签名?
public Object myMethod(Map<String, String[]> values) {
return this;
}
public Object myMethod(Map<String, String> values) {
return this;
}
Run Code Online (Sandbox Code Playgroud) 如果派生类定义了相同的名称,派生类将隐藏基类的重载名称,但我们总是可以使用using-declaration引入该重载集:
template <class BASE>
class A : public BASE
{
public:
using BASE::some_method;
void some_method();
}
Run Code Online (Sandbox Code Playgroud)
但是如果我从可变参数基类引入所有重载集呢?我可以写这样的东西吗?
template <class... BASES>
class A : public BASES...
{
public:
using BASES::some_method...;
void some_method();
}
Run Code Online (Sandbox Code Playgroud)
我考虑过使用一个帮助类:
template <class... BASES>
struct helper;
template <>
struct helper<> {};
template <class OnlyBase>
struct helper<OnlyBase> : OnlyBase
{
using OnlyBase::some_method;
};
template <class Base1, class... OtherBases>
struct helper<Base1, OtherBases> : public Base1, public helper<OtherBases...>
{
using Base1::some_method;
using helper<OtherBases...>::some_method;
};
Run Code Online (Sandbox Code Playgroud)
它确实有效.但它需要大量的输入(当然我可以使用宏,但我尽可能尝试使用c ++的编译时功能),当我想引入更多方法时,我必须在那段代码中做出很多改变.
一个完美的答案是一个简单的语法,但如果没有,我将使用帮助类.
c++ inheritance overloading using-declaration variadic-templates
这是该Ok()方法的签名ApiController:
protected internal virtual OkResult Ok();
Run Code Online (Sandbox Code Playgroud)
这是我RestController班上的方法(从中扩展而来ApiController):
// Note that I'm not overriding base method
protected IHttpActionResult Ok(string message = null);
Run Code Online (Sandbox Code Playgroud)
从OkResultimplements开始IHttpActionResult,这两种方法都可以像这样调用:
IHttpActionResult result = Ok();
Run Code Online (Sandbox Code Playgroud)
事实上,这就是我在我的应用程序中所做的.
我的类PersistenceRestController(从中扩展而来RestController)具有以下代码行:
protected override async Task<IHttpActionResult> Delete(Key id)
{
bool deleted = //... Attempts to delete entity
if(deleted) return Ok();
else return NotFound();
}
Run Code Online (Sandbox Code Playgroud)
编译很好,没有关于方法歧义的警告.这是为什么?
PersistenceRestController还继承了受保护的方法,ApiController所以它应该有两个版本Ok()(它确实).
在执行时,执行的方法是我的方法RestController.
编译器如何知道要运行哪种方法?
看到这段代码的输出我感到很惊讶:
public class File
{
public static void main(String[] args)
{
movie();
}
static void movie(double... x)
{
System.out.println("No varargs");
}
static void movie(int... x)
{
System.out.println("One argument");
}
}
Run Code Online (Sandbox Code Playgroud)
它输出,
One argument
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我认为这段代码不能编译,因为调用movie()是模糊的,但运行正常并输出One argument.
如果我将代码修改为:
public class File
{
public static void main(String[] args)
{
movie();
}
static void movie(boolean... x) //Changed the parameter type to boolean from double
{
System.out.println("No varargs");
}
static void movie(int... x)
{
System.out.println("One argument");
}
} …Run Code Online (Sandbox Code Playgroud) 今天我遇到了这段代码(在 boost/type_index/type_index_facade.hpp中,第252-259行).
/// noexcept comparison operators for type_index_facade classes.
bool operator ==, !=, <, ... (const type_index_facade& lhs, const type_index_facade& rhs) noexcept;
/// noexcept comparison operators for type_index_facade and it's TypeInfo classes.
bool operator ==, !=, <, ... (const type_index_facade& lhs, const TypeInfo& rhs) noexcept;
/// noexcept comparison operators for type_index_facade's TypeInfo and type_index_facade classes.
bool operator ==, !=, <, ... (const TypeInfo& lhs, const type_index_facade& rhs) noexcept;
Run Code Online (Sandbox Code Playgroud)
有人能解释一下这是什么意思吗?我以前从未见过像"==,!=,<,......"之类的东西.
overloading ×10
c++ ×4
java ×4
generics ×2
.net ×1
asp.net ×1
c# ×1
clojure ×1
inheritance ×1
methods ×1
type-erasure ×1