以下代码编译并正常工作:
#include<iostream>
class Base {
protected:
int _a;
public:
virtual ~Base()=default;
Base(int a) : _a{a} {};
int getit() const { return _a; }
};
class Derived : public Base {
public:
Derived(int a) : Base{a} {};
int get2() const { return 2*this->_a; }
};
int main() {
Base D{2};
auto* ptr = &D;
auto* ptr2 = static_cast<Derived*>(ptr);
std::cout << ptr2->get2() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出
4
Run Code Online (Sandbox Code Playgroud)
如果我改变static_cast了dynamic_cast它出现segfaults。
我的问题:
使用static_cast强制转换为不添加任何数据成员的派生类是否安全?
我正在使用一个代码库,我在其中看到以下代码行
auto a = static_cast<custom_type*>(obj.get())->a();
auto b = static_cast<custom_type*>(obj.get())->b();
auto c = static_cast<custom_type*>(obj.get())->c();
Run Code Online (Sandbox Code Playgroud)
期望编译器优化一系列get()andstatic_cast调用是否合理,或者最好执行以下操作:
auto temp = static_cast<custom_type*>(obj.get());
auto a = temp->a();
auto b = temp->b();
auto c = temp->c();
Run Code Online (Sandbox Code Playgroud) 我想知道在没有未定义行为的static_cast情况下应用向下转型的简短代码示例。我环顾四周,发现很多文本(帖子、问答等)引用了标准的片段、解释等。但我没有找到任何例子来说明这一点(这让我感到惊讶)。
谁能提供一个这样的例子吗?
考虑在类模板func中声明以下函数模板:MyClass
template < T >
class MyClass {
public:
MyClass() = default;
// ...
template < typename oT >
oT func() {
return static_cast< oT >(_m); // alternative, return oT(_m);
}
// ...
protected:
T _m;
};
Run Code Online (Sandbox Code Playgroud)
static_cast< oT >(_m)调用显式实例化有什么优势吗oT(_m)?
这是我的班级:
class AComponent : public nts::IComponent
{
public:
AComponent(const size_t &maxInputs, const size_t &maxOutputs, const size_t &value);
AComponent(nts::AComponent &);
virtual ~AComponent();
virtual nts::Tristate Compute(size_t pin_num_this = 1);
virtual void SetLink(size_t pin_num_this,
nts::IComponent &component,
size_t pin_num_target);
void setComponent(const size_t &components, nts::Tristate &state);
virtual void Dump(void) const;
nts::Tristate &getComponent(const size_t &pin);
protected:
std::vector <nts::Tristate *> _components;
size_t _maxInputs;
size_t _maxOutputs;
};
Run Code Online (Sandbox Code Playgroud)
当我尝试拨打这一行时:
this->_components[pin_num_this] =
&static_cast<nts::AComponent>(component).getComponent(pin_num_target);
Run Code Online (Sandbox Code Playgroud)
我发生了以下编译错误:
sources/AComponant.cpp:33:76: error: no matching function for call to ‘nts::AComponent::AComponent(nts::IComponent&)’
this->_components[pin_num_this] = &static_cast<nts::AComponent>(component).getComponent(pin_num_target);
Run Code Online (Sandbox Code Playgroud)
如果我实现构造函数,它就在这里.问题是,我不想操纵IComponent,我想操纵AComponent …
此处的代码用于创建学生成绩单项目。在尝试理解时,我们无法弄清楚以下代码的用途和功能:
File.read(reinterpret_cast<char *> (&st), sizeof(student));
int pos=(-1)*static_cast<int>(sizeof(st));
Run Code Online (Sandbox Code Playgroud)
File.read(reinterpret_cast<char *> (&st), sizeof(student));
if(st.retrollno()==n)
{
st.showdata();
cout<<"\n\nPlease Enter The New Details of student"<<endl;
st.getdata();
int pos=(-1)*static_cast<int>(sizeof(st));
File.seekp(pos,ios::cur);
File.write(reinterpret_cast<char *> (&st), sizeof(student));
cout<<"\n\n\t Record Updated";
found=true;
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读有关static_cast运算符的内容.
考虑以下示例:
#include <iostream>
class B { };
class D : public B
{
public:
void fun()
{
std::cout<<"fun() is called\n";
}
};
void f(B* pb,D* pd)
{
D* pd2=static_cast<D*>(pb);
B* pb2=static_cast<B*>(pd);
pd2->fun();
}
int main()
{
B b;
D d;
f(&b,&d);
}
Run Code Online (Sandbox Code Playgroud)
它说:
在下面的示例中,行D*pd2 = static_cast(pb); 是不安全的,因为D可以有不在B中的字段和方法.但是,行B*pb2 = static_cast(pd); 是一个安全的转换,因为D总是包含所有的B.
与dynamic_cast相反,没有对pb的static_cast转换进行运行时检查.pb指向的对象可能不是D类型的对象,在这种情况下使用*pd2可能是灾难性的.例如,调用作为D类成员但不是B类成员的函数可能会导致访问冲突.
我在gcc 4.8.1和MSVS 2010上尝试了它并获得输出fun()被调用.那么这个程序会调用未定义的行为吗?我的程序可以在运行时崩溃吗?C++标准对此有何看法?如果我理解错误,请纠正我.
c++ inheritance static-cast undefined-behavior language-lawyer
因为与malloc/free相关的分段错误发生,我想将malloc/free转换为new/delete.当malloc/free转换为下面时发生错误.让我知道如何解决它.
(原版的)
char *only_valid_data = static_cast<char*> (malloc (data_size));
Run Code Online (Sandbox Code Playgroud)
(经换算)
char *only_valid_data = new static_cast<char*> [data_size];
Run Code Online (Sandbox Code Playgroud) c++ ×8
static-cast ×8
dynamic-cast ×2
casting ×1
downcast ×1
inheritance ×1
malloc ×1
optimization ×1
pointers ×1
polymorphism ×1