在C中,如果我有两种类型的指针:
typeB *q;
typeA *p;
在以下情况下,是否没有必要将显式强制转换(typeA *)
1 p= q is fine, no need to use p = (typeA *)q;
2 define: void func(typeA *p){}
call: func(q); is fine, no need to use func((typeA *)q)
3 p=malloc(sizeof(typeA)); is fine, no need to use p=(typeA *)mall
我有一个hack程序;它将一些函数注入到目标进程中来控制它。该程序是使用内联汇编用C++编写的。
class GameProcMain {
// this just a class
};
GameProcMain* mainproc; // there is no problem I can do =(GameProcMain*)0xC1EA90
现在,我想定义一个类函数(将ecx设置为类指针),而不是编写程序集。
PPLYDATA GetNearblyMob(__Vector3* cordinate) {
__asm {
mov ecx, 0xC1EA90
ent
问题1>在以下情况下,我应该选中NULL吗?
public interface INewClass {}
public class NewClass : INewClass {}
public void FunMeA(INewClass obj)
{
NewClass n = (NewClass) obj;
... // Should I check (n == null) here?
// call methods defined inside INewClass (updated from NewClass to INewClass)
...
}
当使用使用C风格继承的casts时(利用C-结构的标准布局),比如,我们通常使用C风格的类型转换来向下转换:
struct base_object
{
int x;
int y;
int z;
};
struct derived_object
{
base_object base;
int foo;
int bar;
};
void func(base_object* b)
{
derived_object* d = (derived_object*) b; /* Downcast */
}
但是,如果我们正在编写使用这样的C-API
在C#中,您可以使用as转换类型或获取null:
Object o = Whatever();
String s = o as String;
在C++中有没有类似的简单方法来实现这一点?
如果这很重要的话,我正在使用Visual Studio 2010。
更新:请记住,强制转换和使用as之间有一个非常重要的区别。如果类型不匹配,类型转换(至少在C#中)将抛出异常:
Object o = null;
String s = (String)o; // Will crash.
C++将static_cast、const_cast、reinterpret_cast转换为类似于模板的语法,例如
long foo = 3;
int bar = static_cast<int>(foo);
我看过标准,它说类型转换是表达式,而不是我想的模板函数。
这让我想知道:在幕后,这些强制转换只是具有特权状态的模板,还是碰巧借用了模板语法的关键字?
我在网上搜索了如何将JLong (JavaType)转换为Double,以便在C++代码中使用它,但没有找到示例。我尝试了各种方法来转换这个JLong,但是都没有效果。
function TimeSinceBoot(const aContext: JContext): Double;
begin
result := (((TJSystemClock.JavaClass.ELAPSED_REALTIME div 1000(JLong)) div 60(JLong))(JDouble))(Double);
end;
它说仍然是操作符或;缺少。有什么建议吗?