例如,当我划分两个整数并想要返回一个浮点数时,我迷信地写了这样的东西:
int a = 2, b = 3;
float c = (float)a / (float)b;
Run Code Online (Sandbox Code Playgroud)
如果我不投射a和b浮动,它将进行整数除法并返回一个int.
类似地,如果我想将带符号的8位数与无符号8位数相乘,我会在乘法之前将它们转换为带符号的16位数,以免出现溢出:
u8 a = 255;
s8 b = -127;
s16 = (s16)a * (s16)b;
Run Code Online (Sandbox Code Playgroud)
在完全不进行转换或仅转换其中一个变量时,编译器在这些情况下的行为究竟如何?我是否真的需要显式地转换所有变量,或者只是左边的那个变量,还是右边的变量?
使用C在文本文件中返回随机行的最佳方法是什么?它必须使用标准I/O库(<stdio.h>),因为它适用于Nintendo DS自制程序.
澄清:
我想确保人们不能在URL中键入PHP脚本的名称并运行它.这样做的最佳方法是什么?
我可以在文件中设置一个包含此文件的变量,然后在包含的文件中检查该变量,但有更简单的方法吗?
在这三种情况下,如何调用以下类的构造函数:全局对象,对象数组和另一个类/结构中包含的对象?
具有构造函数的类(在所有三个示例中使用):
class Foo {
public:
Foo(int a) { b = a; }
private:
int b;
};
Run Code Online (Sandbox Code Playgroud)
这是我尝试调用此构造函数:
Foo global_foo(3); // works, but I can't control when the constructor is called.
int main() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
int main() {
// Array on stack
Foo array_of_foos[30](3); // doesn't work
// Array on heap
Foo *pointer_to_another_array = new Foo(3) [30]; // doesn't work
}
Run Code Online (Sandbox Code Playgroud)
在那里,我试图为数组的所有元素调用构造函数,但我也想知道如何在单个元素上调用它.
class Bar {
Foo foo(3); // doesn't work
};
int main() {
Bar …Run Code Online (Sandbox Code Playgroud) 以下是http://www.cplusplus.com/doc/tutorial/polymorphism.html(为便于阅读而编辑)的多态性示例:
// abstract base class
#include <iostream>
using namespace std;
class Polygon {
protected:
int width;
int height;
public:
void set_values(int a, int b) { width = a; height = b; }
virtual int area(void) =0;
};
class Rectangle: public Polygon {
public:
int area(void) { return width * height; }
};
class Triangle: public Polygon {
public:
int area(void) { return width * height / 2; }
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon …Run Code Online (Sandbox Code Playgroud) 这是我Note班级的一部分:
class Note
attr_accessor :semitones, :letter, :accidental
def initialize(semitones, letter, accidental = :n)
@semitones, @letter, @accidental = semitones, letter, accidental
end
def <=>(other)
@semitones <=> other.semitones
end
def ==(other)
@semitones == other.semitones
end
def >(other)
@semitones > other.semitones
end
def <(other)
@semitones < other.semitones
end
end
Run Code Online (Sandbox Code Playgroud)
在我看来,应该有一个我可以包含的模块,可以根据我的<=>方法给我我的相等和比较运算符.有吗?
我猜很多人遇到这种问题.你通常如何解决它?(你怎么让它干?)
我已经为Nintendo DS制作了许多不同的GUI系统部分,比如按钮,文本框和选择框,但是我需要一种在一个Gui类中包含这些类的方法,这样我就可以在屏幕上绘制所有内容了.一次,并立即检查所有按钮以检查是否有任何按钮被按下.我的问题是,将所有类(如按钮和文本框)组织到一个GUI类中的最佳方法是什么?
这是我想到的一种方式,但似乎不对:
编辑:我正在使用C++.
class Gui {
public:
void update_all();
void draw_all() const;
int add_button(Button *button); // Returns button id
void remove_button(int button_id);
private:
Button *buttons[10];
int num_buttons;
}
Run Code Online (Sandbox Code Playgroud)
这段代码有一些问题,但我只是想让你知道我想要什么.
有时我想写一个"主要"注释来描述一大块代码,然后编写"次要"注释来描述该代码块中的一些行:
// Major comment
// Minor comment
...
// Minor comment 2
...
Run Code Online (Sandbox Code Playgroud)
主要评论看起来很奇怪,没有代码直接在它下面,你无法直观地告诉它下面描述了多少代码.
你如何设计这些评论?
(我记得很久以前在Code Complete中读到过这个,但我没有这本书.)
我刚开始学习Ruby,今天遇到了一个问题.
numResults = /\d+/.match(ie.div(:id, 'results_label').text)
puts "Results found: "+numResults.to_s
while(numResults > 0)
.
. some more code
.
Run Code Online (Sandbox Code Playgroud)
我的输出中出现此错误:
Run Code Online (Sandbox Code Playgroud)Exception: undefined method `>' for #<MatchData:0x424c6d4>
这真的很奇怪,因为我在IRB中做了一个while循环,它工作得很好.我不能让循环中的代码执行,因为程序坚持条件.
谁知道什么是错的?
c++ ×4
c ×2
oop ×2
php ×2
ruby ×2
bots ×1
casting ×1
coding-style ×1
comments ×1
comparison ×1
constructor ×1
file ×1
loops ×1
mixins ×1
polymorphism ×1
random ×1
stdio ×1
while-loop ×1