我写了一个小的3d矢量类.特别是我写了两个函数,一个用于旋转向量,另一个用于返回向量本身的旋转副本.所以我有以下内容:
Vector Vector::Rotate(const double angle, Vector& axis) const {
Vector b=*this;
b.Rotate(angle,axis);
return (b);
}
void Vector::Rotate(const double angle, Vector & axis) {
/* let's promote this vector to a quaternion */
Quaternion V (0,*this);
/* quaternion describing the rotation */
Quaternion q (cos(angle/2),axis*sin(angle/2));
/* actual rotation */
*this = (q*V*q.conjugate()).Vec();
}
Run Code Online (Sandbox Code Playgroud)
现在,当我写这样的东西时:
vector2 = vector1.Rotate(rbo::M_PUCKER,i);
Run Code Online (Sandbox Code Playgroud)
我得到错误:没有运算符"="匹配这些操作数操作数类型是:Vector = void
我希望编译器能够理解我想要的东西:他为什么选择void版本而不是另一个返回向量?而且,按照我的方式编写更多相同功能的版本是一种很好的做法吗?
那么std::find对象中的自定义类重载是否可能/"好" ?然后不重载相等运算符.说一个容器存储(智能)指向它的实际数据,我真的对数据布局或存储模式不感兴趣,我只是想找到一个特定的数据成员....
这是合法的,"道德的"吗?如果说我有std::vector<std::shared_ptr<myClass> >- 或"甚至"用户定义的容器?或者我应该总是依赖std::find_if这些案件?
在java中,如何重载类的相等操作和打印操作?
就像在python中一样,你可以通过做def __eq__(self):或者这样做def __str__(self):,但是如何在java中完成呢?
我知道你可以手动创建一个方法并调用它equals或类似的东西,但我想知道是否有一个与==操作符和System.out.print()函数一起使用的实际方法.
有人知道吗?
谢谢.
通常,C#编译器对方法绑定和类型参数推断很聪明.但我似乎已经难倒了.
class Obj
{
void Handler( int a, int b ) { }
Obj() { Method( "", Handler ); }
public void Method<T>( T t, Action<T> action ) { }
public void Method<T, U>( T t, Action<U, U> action ) { }
}
Run Code Online (Sandbox Code Playgroud)
该Method调用导致编译器错误:
参数2:无法从'方法组'转换为'System.Action'.
为什么编译器没有注意到调用符合第二个重载?我可以通过使调用更明确,如在Method<string, int>( "", Handler )或中编译它Method( "", (Action<int, int>)Handler ).但为什么这有必要呢?
您的位置是:http://developer.android.com/reference/java/io/PrintStream.html#print%28float%29
只有一个功能可以满足所有目的:
public void print (Object o) {
if (o == null) {
// print "null"
} else {
// print o.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
更详细的说明.例如,internal_print(String str)是一个写入打印流的函数.那么唯一需要的功能是:
public void print (Object o) {
if (o == null) {
internal_print( "null" );
} else {
internal_print( o.toString() );
}
}
Run Code Online (Sandbox Code Playgroud)
对于其他float,int,char,long,等超载,我能想象他们只是喜欢:
public void print (float o) {
if (o == null) {
internal_print( "null" );
} else {
internal_print( …Run Code Online (Sandbox Code Playgroud) 你好我试图创建一个图形和即时工作我的函数原型,在我的[]运算符重载函数我得到以下错误:这里不允许的函数数组是我的原型:
BinaryTree& ooperator[](int vertex);
Run Code Online (Sandbox Code Playgroud)
二叉树是二叉树类,我需要更改以使其工作
我偶然发现了一些我以前从未见过的东西.考虑一下你有以下课程:
class foo
{
const bar* get() const;
bar* get();
}
Run Code Online (Sandbox Code Playgroud)
foo的客户端如何决定使用哪种get()方法?
我知道原始数据类型的情况下的自动类型提升概念.但是在参考数据类型的情况下,我有下面的代码,它完美地工作.
public class Test4 {
void set(Object o) {
System.out.println("Inside Object");
}
void set(double[] a) {
System.out.println("Array");
}
public static void main(String[] args) {
new Test4().set(null);
}
}
Run Code Online (Sandbox Code Playgroud)
它给出了输出数组.
但是如果代替Object o,如果我们有任何其他类,那么这将显示编译时错误该方法对于类型Test4是不明确的
下面的代码给出了编译时错误
public class Test4 {
/*void set(Object o) {
System.out.println("Inside Object");
}*/
void set(String s) {
System.out.println("String");
}
void set(double[] a) {
System.out.println("Array");
}
public static void main(String[] args) {
new Test4().set(null);
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,每个参考数据类型(类,接口和数组)的默认值为null.
那么为什么上面的代码适用于Object o.
提前致谢
最近,我开始使用C++处理OpenGL.
我对通过声明Vertex或更改当前颜色的方式感到恼火
在glColor3f()和glVertex{2,3,4}{sdif}()方法.
那么,为什么OpenGL不会重载这些功能.
例如,输入更好glColor(),glVertex()并且它们将被参数数量及其类型识别.
最终,代码看起来会更好,更容易阅读,结果相同.
我希望有一个很好的理由不要重载类似的功能.
好的,我知道有成千上万的这样的问题,但所有的方法都不同,所以我怎么能解决这个问题这就是我要调用的方法
private void GravityPlayer(GameTime gametime)
{
while ((int)gametime.ElapsedGameTime.TotalMilliseconds < 100)
{
KeyboardState keybState;
keybState = Keyboard.GetState();
if (keybState.IsKeyDown(Keys.Space) || keybState.IsKeyDown(Keys.Up))
playerPos.Y = 100;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我将从中调用它的方法
protected override void Update(GameTime gameTime)
{
GravityPlayer();
base.Update(gameTime);
}
Run Code Online (Sandbox Code Playgroud)