什么方法mkNumericOps和mkOrderingOps的scala.math.Integral做,我们如何使用它们?
我知道可以声明函数和对象方法implicit并将其用于隐式转换.但是我不明白为什么会声明traits方法implicit.
BTW,也可以声明类方法implicit吗?
我正在写一个String类.我希望能够分配我的字符串,如;
a = "foo";
printf(a);
a = "123";
printf(a);
int n = a; // notice str -> int conversion
a = 456; // notice int -> str conversion
printf(a);
Run Code Online (Sandbox Code Playgroud)
我已经为string到整数转换分配了operator =()方法.如何声明另一个operator =()以便我可以执行反向方法?
当我宣布另一个时,它似乎覆盖了前一个.
String::operator const char *() {
return cpStringBuffer;
}
String::operator const int() {
return atoi(cpStringBuffer);
}
void String::operator=(const char* s) {
ResizeBuffer(strlen(s));
strcpy(cpStringBuffer, s);
}
bool String::operator==(const char* s) {
return (strcmp(cpStringBuffer, s) != 0);
}
//void String::operator=(int n) {
// char _cBuffer[33];
// char* s = itoa(n, …Run Code Online (Sandbox Code Playgroud) c++ operators conversion-operator implicit-conversion explicit-conversion
在C++中调整旧代码编译器不传递类型表达式"if(a&b)"
int a = 32;
int b = 1;
if (a & b) {} <--- Compiler Error
Run Code Online (Sandbox Code Playgroud)
从Int到Bool的隐式转换.我可以改为if ((a & b)! = 0) {}
但是在许多地方阅读和发现它更糟糕.
有没有办法解决?
我经常看到:
string str = "123";
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果string是一个类类型,为什么我们可以直接将它设置为等于"123"而不是使用new或直接初始化它?
我习惯看到类似的东西
classType *pt = new classType;
classType pt = classType();
Run Code Online (Sandbox Code Playgroud)
不
classType s = value;
Run Code Online (Sandbox Code Playgroud) 为什么
if (x) {
f();
}
Run Code Online (Sandbox Code Playgroud)
f()如果x是一个空字符串调用""?
不应该将D中的空字符串隐式转换为bool false类似于Python中的字符串,并且当空数组执行时(在D中)?
更新:我修正了问题.我错误地颠倒了推理逻辑.幸运的是,聪明的D头脑无论如何理解我的意思;)
我正在使用一个结构,需要隐式运算符对字符串,并遇到一个我没有想过的基本问题.
public static implicit operator Version (string value) {...}
Run Code Online (Sandbox Code Playgroud)
我可以理解只有显式运算符来强制执行转换,但如果隐式运算符已经过载,则无法想到需要它的情况.有吗?
.net c# operator-overloading implicit-conversion explicit-conversion
我构建了以下最小的示例:
class A
{
public:
A(const char *s);
private:
const char *p;
};
A::A(const char *s)
: p(s)
{
}
A foo()
{
return false;
}
A bar()
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
与g++ (Debian 4.7.2-5) 4.7.2我一起编译得到以下内容:
t.cc: In function 'A foo()':
t.cc:17:10: warning: converting 'false' to pointer type for argument 1 of 'A::A(const char*)' [-Wconversion-null]
t.cc: In function 'A bar()':
t.cc:23:10: error: could not convert 'true' from 'bool' to 'A'
Run Code Online (Sandbox Code Playgroud)
据我所知,如果类有构造函数,则可以使用一种类型T而不是类的实例.在这种情况下,T的值/实例由编译器在对构造函数的调用中包装.AAA(T)A(T)
此外,只有一个直接隐式转换是允许的,即没有链 …
请考虑以下代码.
#include <functional>
int main(void)
{
std::function<void()> f1;
if (f1) { /* ok */
...
}
bool b = f1; /* compile-error */
bool B = !f1; /* ok */
...
}
Run Code Online (Sandbox Code Playgroud)
std::function<>在某些情况下隐式转换为bool但不是全部转换为bool.将其分配给bool-variable不起作用,而操作的结果或在if()-statement中使用它是可以的.
为什么会这样?看来我们必须对它进行布尔运算,然后转换才有效.
我做的工作b = f1是什么 - 线是好的'双重爆炸:!!.它看起来像现代C++中的古董 - 代码.
编辑:这也编译:
bool b = f1 || f1; /* OK */
Run Code Online (Sandbox Code Playgroud) 我无法理解这个三元运算符的转换逻辑(这里是一个例子):
#include <iostream>
#include <typeinfo>
#include <unistd.h>
#include <cxxabi.h>
#include <climits>
template<typename T>
struct singletime
{
private:
T value;
public:
T& operator()() {return this->value;}
operator const T& () const {return value;}
unsigned char flag_needed_for_all_types;
};
static void getvalue1 (uint64_t value, const char *call)
{
std::cout << call << ": \t" << value << std::endl << std::endl;
}
#define getvalue(x, str) \
std::cout << typeid(x).name() << std::endl; \
getvalue1(x, str);
int main (int argc, char *argv[])
{
bool flag = true; …Run Code Online (Sandbox Code Playgroud) 我正在尝试检查文件是否成功打开。我发现这种方法可以打开要读取的文件:
char path[]="data/configuration.txt";
std::ifstream configFile(path, std::ifstream::in);
if(configFile) {
std::cout<<"Successfully opened file: "<<path<<std::endl;
} else {
std::cout<<"Error, Could not open file: "<<path<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
问题是究竟要if检查什么?
因为我还发现了以下检查文件是否打开的方法:
char path[]="data/configuration.txt";
std::ifstream configFile;
configFile.open(path, std::ifstream::in);
if(configFile.is_open()) {
std::cout<<"Successfully opened file: "<<path<<std::endl;
} else {
std::cout<<"Error, Could not open file: "<<path<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我还有其他问题。例如,两种打开文件的方法有什么区别?另外,这两个if条件有什么区别?
我认为这些是相似的方法,最终会得到相同的结果,因为我可以使用两种打开方法std::ifstream一样的is_open方法:
std::ifstream configFile(path, std::ifstream::in);
configFile.open(path, std::ifstream::in);
Run Code Online (Sandbox Code Playgroud)