为什么dynamic_cast<new>(old)会变old?
示例代码:
#include <iostream>
#include <string>
class MyInterface {
public:
virtual void say() = 0;
virtual ~MyInterface() { }
};
class SubInterface : public MyInterface {
public:
std::string test = "test";
virtual void say() = 0;
virtual ~SubInterface() { }
};
class Example : public SubInterface {
public:
void say() {
std::cout << test << std::endl;
}
};
int main() {
MyInterface* ex1 = new Example();
SubInterface* ex2 = dynamic_cast<SubInterface*>(ex1);
if (ex2 != nullptr) {
std::cout …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
main()
{
short vShort=3;
int *iInt=(int *)&vShort ;
printf("Value of short: %d\n",vShort);
printf("Value iof short: %d\n",*iInt);
}
Run Code Online (Sandbox Code Playgroud)
我写了这段代码,但是这个变量是打印valus,如下所示.Int-4尺寸的短尺寸 - 2
当我这样做时它也没有工作"int*iInt=&vShort ;"给出相同的输出.
输出:
短期价值:3价值短期:196608
这是我运行的程序:
#include <stdio.h>
int main(void)
{
int y = 1234;
char *p = &y;
int *j = &y;
printf("%d %d\n", *p, *j);
}
Run Code Online (Sandbox Code Playgroud)
我对输出有点困惑.我所看到的是:
-46 1234
Run Code Online (Sandbox Code Playgroud)
我把这个程序写成了一个实验,不知道它会输出什么.我期待可能有一个字节y.
这里发生了什么"幕后"?解除引用如何p给我-46?
正如其他人指出的那样,我必须进行明确的施法才能导致UB.我没有改变这一行char *p = &y;,char *p = (char *)&y;所以我没有使下面的答案无效.
此程序不会导致此处指出的任何UB行为.
我有一个我不明白的问题.这是我的代码:
String input = "3 days ago"
String firstCharacter = input[0].ToString(); //Returns 3
int firstCharacter = (int)input[0]; //Returns 51
Run Code Online (Sandbox Code Playgroud)
为什么它会回归51?
PS:我的代码来自这个帖子:C#:如何获取字符串的第一个字符串?
更多信息:
In case that input = "5 days ago", then int firstCharacter is 53.
Run Code Online (Sandbox Code Playgroud) 我将如何将文字转换/转换char '0'为int 0?
即:
char string[] = "0101";
char ch = string[0];
x = magic(ch);
if (x)
printf("int zero")
Run Code Online (Sandbox Code Playgroud) 如何将对象类型转换为不同类型?
public T Get<T>(T t)
{
if (t is TypeA)
{
TypeA a = (TypeA)t; //error
}
}
Run Code Online (Sandbox Code Playgroud) 我无法从int到double到 工作进行简单的转换
#include <iostream>
int main()
{
int i = 8;
double d1 = i;
double d2 = static_cast<double>(i);
std::cout << "i = " << i << ", d1 = " << d1 << ", d2 = " << d2 << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
d1和d2equals 8,而不是8.0,不仅在stdout,而且在 QtCreator 的调试器中。
AFAIK,d1应该d2是8.0,那么不应该吗?
谁能告诉我我做错了什么?
我正在设置 g++ 编译器,版本 7.4.0,符合 C++11。
谢谢!
我一直在阅读PHP 手册的“可见性”部分,在第一条评论中,有人提到:
外部代码可以将 Item 属性转换为任何其他 PHP 类型(布尔值、整数、浮点数、字符串、数组和对象等)——另一个巨大的错误。
考虑这个例子:
class base {
public $foo = 1;
}
$first = new base();
(string)$first->foo; //I thought just this expression would typecast
var_dump($first->foo); //but I found it still is int
$first->foo = (string)$first->foo;
var_dump($first->foo); //ok so public props can be typecasted
Run Code Online (Sandbox Code Playgroud)
是不是我们不能从外部更改其类型的受保护和私有属性?或者这也适用于公共财产吗?
在研究 C++ 中强制转换的行为时,我发现reinterpret_cast-ing from float*toint*仅适用于0:
float x = 0.0f;
printf("%i\n", *reinterpret_cast<int*>(&x));
Run Code Online (Sandbox Code Playgroud)
打印0,而
float x = 1.0f;
printf("%i\n", *reinterpret_cast<int*>(&x));
Run Code Online (Sandbox Code Playgroud)
打印1065353216。
为什么?我希望后者能1像static_cast那样打印。
在下面的代码中,ip 地址被转换为 uint8_t *。但下面再次将强制转换指针的每个索引强制转换为 uint8_t。程序员为什么要这样做呢?如果我们删除初始演员之后的所有演员,会有什么不同吗?此代码将 IPv4 IP 地址转换为 IP 号码。谢谢
uint32_t Dot2LongIP(char* ipstring)
{
uint32_t ip = inet_addr(ipstring);
uint8_t *ptr = (uint8_t *) &ip;
uint32_t a = 0;
if (ipstring != NULL) {
a = (uint8_t)(ptr[3]);
a += (uint8_t)(ptr[2]) * 256;
a += (uint8_t)(ptr[1]) * 256 * 256;
a += (uint8_t)(ptr[0]) * 256 * 256 * 256;
}
return a;
}
Run Code Online (Sandbox Code Playgroud) casting ×10
c ×4
c++ ×4
c# ×2
int ×2
pointers ×2
char ×1
dereference ×1
inheritance ×1
oop ×1
php ×1
properties ×1
short ×1
string ×1
types ×1