我已经编写了近二十年的C和C++代码,但这些语言的一个方面我从未真正理解过.我显然使用常规演员表,即
MyClass *m = (MyClass *)ptr;
Run Code Online (Sandbox Code Playgroud)
到处都是,但似乎有两种其他类型的演员,我不知道其中的区别.以下代码行之间有什么区别?
MyClass *m = (MyClass *)ptr;
MyClass *m = static_cast<MyClass *>(ptr);
MyClass *m = dynamic_cast<MyClass *>(ptr);
Run Code Online (Sandbox Code Playgroud) 有什么理由喜欢static_cast<>超过C风格的演员吗?它们是等价的吗?他们有什么速度差异吗?
我尝试研究和互联网之间的差异cout,但无法找到一个完美的答案.我还不清楚何时使用哪个.任何人都可以通过简单的程序向我解释并说明何时使用哪一个的完美情况?cerrclog
我访问了这个网站,其显示了一个小程序cerr,并clog,但获得的输出那边也可以使用来获得cout.所以,我对每个人的确切用法感到困惑.
Eric Lippert对这个问题的评论让我彻底糊涂了.C#中的转换和转换有什么区别?
#include <iostream>
class Car
{
private:
Car(){};
int _no;
public:
Car(int no)
{
_no=no;
}
void printNo()
{
std::cout<<_no<<std::endl;
}
};
void printCarNumbers(Car *cars, int length)
{
for(int i = 0; i<length;i++)
std::cout<<cars[i].printNo();
}
int main()
{
int userInput = 10;
Car *mycars = new Car[userInput];
for(int i =0;i < userInput;i++)
mycars[i]=new Car[i+1];
printCarNumbers(mycars,userInput);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个汽车阵列,但我收到以下错误:
cartest.cpp: In function ‘int main()’:
cartest.cpp:5: error: ‘Car::Car()’ is private
cartest.cpp:21: error: within this context
Run Code Online (Sandbox Code Playgroud)
有没有办法在不使Car()构造函数公开的情况下进行初始化?
我在c ++中使用c函数,其中在c中作为void类型参数传递的结构直接存储相同的结构类型.
例如在C.
void getdata(void *data){
Testitem *ti=data;//Testitem is of struct type.
}
Run Code Online (Sandbox Code Playgroud)
在c ++中做同样的事我使用static_cast:
void foo::getdata(void *data){
Testitem *ti = static_cast<Testitem*>(data);
}
Run Code Online (Sandbox Code Playgroud)
当我使用 reinterpret_cast它做同样的工作,铸造结构
当我使用 Testitem *it=(Testitem *)data;
这也是一样的.但是如何通过使用它们中的三个来影响结构.
是否有可能没有虚拟方法的继承?编译器说以下代码不是多态的.
例:
Class A(){
int a;
int getA(){return a;};
}
Class B(): A(){
int b;
int getB(){return b;};
}
Run Code Online (Sandbox Code Playgroud)
在另一个类中,我们试图从一个A对象向下转换为一个B对象:
A *a;
B *b = dynamic_cast<B*>(a)
Run Code Online (Sandbox Code Playgroud)
但是这会产生以下错误:
cannot dynamic_cast ... (source type is polymorphic)
Run Code Online (Sandbox Code Playgroud) 究竟什么是C/C++中的类型转换?编译器如何检查是否需要显式类型转换(并且是否有效)?它是否比较了值所需的空间?如果我有例如:
int a;
double b = 15.0;
a = (int) b;
Run Code Online (Sandbox Code Playgroud)
如果我没记错的话,double值需要更多的空间(是8字节?!)而不是整数(4字节).并且两者的内部表示完全不同(在两个/尾数上的补码).那么内部会发生什么?这里的例子非常简单,但在C/C++中有很多类型.
如果我可以将例如FOO转换为BAR,编译器如何知道(或程序员)?
有了这个C++代码,
char* a = (char*) b;
Run Code Online (Sandbox Code Playgroud)
我被警告warning: use of old-style cast.
什么是新式演员?
考虑这个简单的层次
class Base { public: virtual ~Base() { } };
class Derived : public Base { };
Run Code Online (Sandbox Code Playgroud)
试图向下转换Base* p到Derived*可能使用dynamic_cast<Derived*>(p).我曾经dynamic_cast通过将vtable指针p与Derived对象中的指针进行比较来思考作品.
但是,如果我们从中衍生出另一个类Derived呢?我们现在有:
class Derived2 : public Derived { };
Run Code Online (Sandbox Code Playgroud)
在这种情况下:
Base* base = new Derived2;
Derived* derived = dynamic_cast<Derived*>(base);
Run Code Online (Sandbox Code Playgroud)
我们仍然得到一个成功的向下转换,即使vtable指针in Derived2与vtable指针无关Derived.
它是如何实际工作的?如何dynamic_cast知道是否Derived2派生自Derived(如果Derived在不同的库中声明的话)?
我正在寻找关于它如何实际工作的具体细节(最好是在海湾合作委员会,但其他人也很好).这个问题是不是一个重复这个问题(没有指明它是如何工作).
c++ ×9
casting ×6
c ×2
arrays ×1
c# ×1
clog ×1
constructor ×1
cout ×1
dynamic-cast ×1
g++ ×1
inheritance ×1
iostream ×1
pointers ×1
polymorphism ×1
static-cast ×1
struct ×1
types ×1
vtable ×1