我有一个名为 的类Fstring,其中有一个wchar_t*。
我写了以下内容将字符串文字复制到Fstring:
#include <iostream>
using namespace std;
class Fstring{
wchar_t *arr;
public:
Fstring& operator = (const wchar_t temp[])
{
delete [] arr;
arr=new wchar_t[wcslen(temp)];
for(int i=0;i<=wcslen(temp);i++)
arr[i]=temp[i];
return *this;
}
};
int main()
{
Fstring test=L"Hello World";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它没有用。编译器给了我以下错误:
错误 C2440:“正在初始化”:无法从“const wchar_t [12]”转换为“Fstring”
我真的很困惑,我在谷歌上搜索了“重载运算符”,但所有结果都与我用来重载运算符的方式相同。那么为什么这不起作用呢?
哪个代码更适合使用:初始化字符串?
bool flag = /*function call...*/
string str = "abc";
if(flag)
str = "bcd";
Run Code Online (Sandbox Code Playgroud)
要么
string str;
if(flag)
str = "bcd";
else
str = "abc";
Run Code Online (Sandbox Code Playgroud)
要么
string str("abc");
if(flag)
str = "bcd";
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我不明白R中"="和"=="之间的区别.我有几个问题:
=在脚本中分配,而在函数中分配?<-时为什么要使用=?有区别吗?我们如何在运算符中编写没有 else 的单行 If 条件?
例子:
如果(计数== 0){计数= 2; }
我们怎么能像下面这样写:
计数=计数==0?2;
由于三元运算符需要 if else 条件。我想在没有 ternery 运算符的情况下做到这一点。C#中是否有可用的运算符?
谢谢。
c# conditional if-statement conditional-operator assignment-operator
我通常知道,*和&符号.但我们的老师给了我们一个例子,她说"问题发生在这里"
int *a1;
int *a2 = new int[100];
a1=a2 //What does this line mean???
delete []a2;
k=a1[0]//she said error occurs here.
Run Code Online (Sandbox Code Playgroud)
我无法理解什么是a1 = a2?为什么会出现错误?
我是Python的新手,正在尝试并运行了以下代码:
a=13
a==14
print(a)
Run Code Online (Sandbox Code Playgroud)
我希望该程序不会由于第二行而编译,尽管令人惊讶的是它可以编译(尽管我看不到它所做的任何更改)。有人可以解释为什么吗?如果我使用a===14而不是a==14错误。
c++ ×3
c# ×1
conditional ×1
if-statement ×1
pointers ×1
python ×1
python-3.x ×1
r ×1
string ×1
wchar-t ×1