这是代码片段:
public static void main (String[]arg)
{
char ca = 'a' ;
char cb = 'b' ;
System.out.println (ca + cb) ;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
195
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我认为'a' + 'b'是,要么"ab","12"或3.
这里发生了什么?
似乎我的编译器(GCC 4.7)不会发出隐式类型转换,如果由于基类匹配而产生的转换类型将允许函数调用,即转换结果A为a AB.
class AB {};
class A: public AB
{
public:
A(float i) {}
A() {}
A(const A& rhs) {}
A(A&& rhs) {}
};
AB operator*(const AB& lhs,const AB& rhs)
{
// Return default constructed AB
}
void foo() {
A a;
auto tmp = 2.0 * a; // This fails because no conversion rule was found.
}
Run Code Online (Sandbox Code Playgroud)
这是C++语言的一个特性吗?如果是这样,在什么情况下最好不与基类匹配.
请不要回答这个糟糕的编译器有太多的事情要做,并且与基类匹配太多了.
编辑
c.cc:51:20: error: no match for ‘operator*’ in ‘2.0e+0 * a’
c.cc:51:20: note: candidate is: c.cc:42:1: …Run Code Online (Sandbox Code Playgroud) 问题是"转换=' - '".源代码在这里,我曾评论过"printf"以避免一些问题,总是由"print"来表示:这是一个用于计算年度贷款的程序.
import java.util.*;
public class Loan{
public static void main(String args[]){
final double MIN = 0.05;
final double MAX = 0.08;
final double ADD = 0.125;
Scanner input = new Scanner(System.in);
System.out.print("Loan Amount: ");
double amount = input.nextDouble();
System.out.print("Number of Years :");
int years = input.nextInt();
/*
for(double r = MIN; r < MAX;r = r + ADD){
double R = Math.pow((1+r),years);
double monthlyPayment = r * R * amount / 12/(R-1);
double totalPayment = 12*monthlyPayment*years; …Run Code Online (Sandbox Code Playgroud) 我有以下内容:
class Goo
trait Bar[D] {
def toD : D
}
class Moo extends Bar[Goo] {
def toD : Goo = new Goo
}
object Foo {
import scala.collection.JavaConversions._
import java.util
implicit def scalaSetToJavaSet[D](bars: Set[Bar[D]]) : util.Set[D] = bars.map(_.toD)
}
trait Zoo {
import java.util
import Foo._
var javaSet : util.Set[Goo] = Set(new Moo) //compile error
var explicitJavaSet: util.Set[Goo] = scalaSetToJavaSet(Set(new Moo)) //works!!!
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译此代码时,我收到一个错误:
"error: type mismatch;
found : scala.collection.immutable.Set[Moo]
required: java.util.Set[Goo]
var javaSet : util.Set[Goo] = ***Set(new Moo)***" …Run Code Online (Sandbox Code Playgroud) 我正在维护一些代码并遇到了这个片段:
int num = 0;
float sum = 0.0;
bool value[rows][cols]; //initialized elsewhere in the code
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
if(value[i][j] == true) {
sum += value[i][j]; //shouldn't this be equivalent to "sum += 1;"
//as it is in the if block?
num++;
}
float ans = 1.0;
if(num > 12)
ans = sum / num;
Run Code Online (Sandbox Code Playgroud)
编写这段代码的人最初在这里做了一些非常聪明的事情,或者应该ans永远是1?据我所知,num并且sum应该始终是完全相同的值,不是吗?
我有这个简单的代码
def doStuff(x: Int) = {
...
}
Run Code Online (Sandbox Code Playgroud)
但是,如果x小于或等于0,则此方法不支持,因此我使用require
def doStuff(x: Int) = {
require(x > 0, "x should be greater than 0")
}
doStuff(0) // runtime exception
Run Code Online (Sandbox Code Playgroud)
但是这发生在运行时异常中,如何更改它以使用Implicit在编译时显示错误?
提前谢谢了
我有一个看起来像这样的结构.
struct puzzle {
int d[16];
};
Run Code Online (Sandbox Code Playgroud)
我听说C/C++中的数组和指针是相同的,所以我认为struct会存储一个指针,而指针指向一个int数组.但是,我使用调试器进行了简单的实验,看看它是如何存储的,我发现数组直接存储在结构中.
我有以下代码:
type RString = String
implicit def stringToRString(s:String):RString = s.reverse
val s = "The Force Awakens"
val r:RString = s
println(r)
Run Code Online (Sandbox Code Playgroud)
我有点期待r与之相反,s但它等于s.这是scala编译器采用快捷方式作为RString别名,String因此永远不会调用隐式转换吗?
根据N3337工作草案(与已发布的ISOC++ 11标准最相似的草案),答案最多只有一个.
N3337:
最多一个用户定义的转换(构造函数或转换函数)隐式应用于单个值.
Run Code Online (Sandbox Code Playgroud)[ Example: struct X { operator int(); }; struct Y { operator X(); }; Y a; int b = a; // error // a.operator X().operator int() not tried int c = X(a); // OK: a.operator X().operator int() —end example ]
但根据使用gcc(Ubuntu 4.8.4-2ubuntu1~14.04)4.8.4编译main.cpp并在Ubuntu 14.04.3 LTS中使用引用语句运行a.out的结果,答案最多不是一个.
main.cpp:
#include <iostream>
struct As
{
operator int(){ std::cout<<"operator As::int()"<<std::endl; return 1; }
};
struct Bs
{
operator int(){ std::cout<<"operator …Run Code Online (Sandbox Code Playgroud) int main()
{
unsigned int c = -1;
char *s = "Abc";
char *x = "defhe";
((strlen(s)-strlen(x))>c)? printf(s): printf(x);
}
Run Code Online (Sandbox Code Playgroud)
的值c就是4294967295和的值(strlen(s)-strlen(x))是4294967294应打印x,但在打印的S value.I上午没有得到为什么它是这样的