我有一个名为Car的类,其属性为Name。现在,每次我想更改自己的汽车名称时,都必须输入Car1.Name =“ Porka Turbo”。现在我想完成的是这样写的代码少了五个字符:
Car1 = "Porka Turbo"
Run Code Online (Sandbox Code Playgroud)
它应该像这样工作:如果我分配一个从Car类派生的类,它应该进行常规的类分配,但是当我向我的类分配字符串时,应该将此字符串重定向到名为“ Name”的属性。
这样它将与以下命令相同:
Car1.Name = "Porka Turbo"
Run Code Online (Sandbox Code Playgroud)
在C#.NET 3.5中是否可行?如果没有,您是否知道其他任何方法来减少此特定示例中的内容?
对于模糊的问题标题感到抱歉,但我在这里有这些typedef:
typedef std::list<AnimationKeyframe> Timeline;
typedef Timeline::iterator Keyframe;
Run Code Online (Sandbox Code Playgroud)
让我们说一个这样的课:
class foo
{
Timeline timeline;
Keyframe left,right;
};
Run Code Online (Sandbox Code Playgroud)
我也有一个复制/赋值构造函数,因为我存储迭代器(看起来很糟糕),麻烦就在那里.
Keyframe myTemp, hisTemp;
this->left = this->timeline.begin(); // assigns
myTemp = this->timeline.begin(); // assigns
hisTemp = that.timeline.begin() // does not assign
Run Code Online (Sandbox Code Playgroud)
一旦我试图用一个'那个'(另一个foo)分配一个Keyframe,我就会得到看起来像模糊问题的错误.
binary'=':找不到哪个运算符采用'std :: _ List_const_iterator <_Mylist>'类型的右手操作数(或者没有可接受的转换)
附加信息:
with
[
_Mylist=std::_List_val<AnimationKeyframe,std::allocator<AnimationKeyframe>>
]
could be 'std::_List_iterator<_Mylist> &std::_List_iterator<_Mylist>::operator =(const std::_List_iterator<_Mylist> &)'
with
[
_Mylist=std::_List_val<AnimationKeyframe,std::allocator<AnimationKeyframe>>
]
while trying to match the argument list '(Keyframe, std::_List_const_iterator<_Mylist>)'
with
[
_Mylist=std::_List_val<AnimationKeyframe,std::allocator<AnimationKeyframe>>
]
Run Code Online (Sandbox Code Playgroud)
是什么导致这种奇怪的行为 我怀疑使用迭代器作为状态的糟糕风格......但我不知道除了保持指针之外我还能做些什么.
我写了这段代码,因为我在写一个更大的程序时遇到了类似的问题.尽管如此,我知道问题是一样的,所以我做了一个小例子.
#include <stdio.h>
typedef struct
{
int x;
char * val;
}my_struct;
int main()
{
my_struct me = {4, " "};
puts("Initialization works.");
me.val[0] = 'a';
puts("Assignment works.");
puts(me.val);
puts("Output works.");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当使用tcc(Tiny C Compiler)编译时,它编译并执行正常.但是,使用GCC 4.6.0 20110513(预发行版)它会编译,但是,当我执行它时,我只能通过"初始化工作".在获得段错误之前.
我究竟做错了什么?是我的代码还是我的GCC编译器?
赋值是用回溯完成8皇后二维数组程序.
#include <iostream>
using namespace std;
int main() {
int b[8][8] = { 0 };
int r, c, i;
int count = 1;
b[0][0] = 1;
c = 0;
nextColumn:
c++;
if (c == 8)
goto print;
r =- 1;
nextRow:
r++;
if (r == 8)
goto back;
for (i = 0; i < c; i++) {
if (b[r][i] == 1)
goto nextRow;
}
for (i = 0; (r - i) >= 0 && (c - i) >= 0; i++) …Run Code Online (Sandbox Code Playgroud) 我必须创建一个重载=操作符的函数,这样当你把object1 = object2;它复制到object2里面的所有值到object1时.
我的班级看起来像:
class foo {
private:
int max;
int *val;
size_t *listofVal;
}
Run Code Online (Sandbox Code Playgroud)
我的重载函数声明为:
foo& foo::operator=(const foo& matrix);
foo::foo(std::istream& s); //constructor
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
在这附近晃动,看起来很简单.
有一个Seq[Tuple2[A,B]],称之为foo,我想提炼Tuple2成一个(Seq[A],Seq[B])我可以做一站式多任务.
val(a,b) = foo ??
Run Code Online (Sandbox Code Playgroud)
尝试过地图,平面地图和其他失败的变化.
如果你愿意,请把灯洒下来;-)
除了以下强力方法之外,是否有更简单的方法在给定if条件的情况下为变量赋值?
方法1:
a, b, c, d = 0.03,0.4,0.055,0.7
x = 0.2
if a < x:
a = x
if b < x:
b = x
if c < x:
c = x
if d < x:
d = x
Run Code Online (Sandbox Code Playgroud) 例如,我有以下指针:
char *ptr;
char *string;
string = malloc(sizeof(char) * 10);
//set string to some word
Run Code Online (Sandbox Code Playgroud)
以下几行有什么区别?
ptr = string;
vs.
memcpy(ptr, string, sizeof(string));
Run Code Online (Sandbox Code Playgroud)
此外,如果我尝试在这些任务中的任何一个之后释放"字符串",是否会出现问题?"ptr"仍会保留我指定给它的值吗?
我想创建一个多维对象.以下是我的示例代码.我对JavaScript不太熟悉.
示例代码
var test = {};
test[0] = {1:{a:1,b:2,c:3}};
if(true)
{
test[0] = {2: {c:1,b:2,a:3}};
}
console.log(test);
Run Code Online (Sandbox Code Playgroud)
期待结果
{
0: {
1:{a:1,b:2,c:3},
2:{c:1,b:2,a:3}
}
}
Run Code Online (Sandbox Code Playgroud)