通过按位运算确定返回类型(Determining return type via bitwise operation)
我在java文档中游荡,突然发现这段代码:
public static <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); }
谁能解释一下返回声明后会发生什么样的魔法? 好的,该方法的结果由lambda表达式与功能界面结合确定。 但那之前写的是什么? 它是通过按位运算转换返回类型吗? 我不明白。 据我所知,bitwise仅适用于数字。 我在哪里可以更具体地阅读这个案例?
I was wandering throught java docs and suddenly found this code:
public static <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); }
Can anyone explain what kind of magic is going on after the return statement? Okay, the result of the method is determined by lambda expression in combination with functional interface. But what is it written before that? Is it casting return type via bitwise operation? I don't get it. As far I know bitwise is applicable only with numbers. Where can I read about this case more specifically?
原文:https://stackoverflow.com/questions/30061674
满意答案
你的析构函数肯定是错的。 类的析构函数不会也不能
delete
该对象的内存。如果没有虚函数,你对公共继承的反对意见是什么? (至少)有两种方法可以防止某人通过基指针意外删除派生对象。 一个是
protected
基础析构函数。另一种方法是将派生类的动态分配实例直接填充到
shared_ptr
。 这甚至可以是shared_ptr<Base>
:std::shared_ptr<Base> foo(new DerivedA(...));
由于
shared_ptr
具有捕获其参数类型的模板构造函数,因此Base*
指针将在与shared_ptr
关联的删除函数中转换为DerivedA*
,因此会被正确删除。 没有人会如此愚蠢地尝试从shared_ptr
提取指针并将其删除为Base*
。当然,如果你没有虚函数,那么当衍生类之间的唯一区别就是它们在构造函数中设置的时候,这个技巧才真正有用。 否则,您最终需要从
shared_ptr
向下转换Base*
指针,在这种情况下,您应该首先使用shared_ptr<DerivedA>
。Your destructor is definitely wrong. The destructor of a class does not and must not
delete
the memory for the object.What's your objection to public inheritance without virtual functions? There are (at least) a couple of ways to prevent someone accidentally deleting a derived object through a base pointer. One is to make the base destructor
protected
.Another is to stuff dynamically-allocated instances of the derived class straight into a
shared_ptr
. This can even be ashared_ptr<Base>
:std::shared_ptr<Base> foo(new DerivedA(...));
Because
shared_ptr
has a template constructor that captures the type of its argument, theBase*
pointer will be converted toDerivedA*
in the deleter function associated with theshared_ptr
, and hence deleted correctly. Nobody should ever be so daft as to try to extract a pointer out of ashared_ptr
and delete it asBase*
.Of course if you have no virtual functions, then the trick is only really useful when the only difference between the derived classes is what they set up in their constructors. Otherwise you'll end up needing to downcast the
Base*
pointer from theshared_ptr
, in which case you should have used ashared_ptr<DerivedA>
to begin with.
相关问答
更多C ++析构函数返回(C++ destructor with return)
C ++:CRTP析构函数?(C++ : CRTP destructor?)
这个C ++析构函数是多余的吗?(Is this C++ destructor redundant?)
CRTP(C ++)的各种错误(Various errors with CRTP (C++))
C ++析构函数示例(C++ destructor example)
C ++需要析构函数(C++ need for destructor function)
C ++中的析构函数(destructor in C++)
需要C ++ / CLI完成析构函数(The need for C++/CLI finalize destructor)
析构函数是正常的函数调用吗?(Is destructor a normal function call?)
C ++ CRTP类层次结构(C++ CRTP class hierarchy)
相关文章
更多ServletOutputStream cannot be resolved to a type
Tomcat源码里,return (factory)有特殊作用么
深入浅出dwr系列教程之2传参数及处理各种类型的返回值
The content of element type "package" must match "...
用axis开发java web service中关于服务返回复杂类型的问题
微信公众号接口添加菜单时错误(errcode":40017 invalid button type)
Java运算符
maven项目使用SOLR时报 previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest" 错的解决方法
关于两种对象类型使用
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!