标签: static-cast

根据情况在if语句中声明不同的数据类型:如何关闭编译器?

嘿所以我正在创建一个序列化函数,它接受一个基类指针'Joint',提取它'type'的联合后代,然后想要根据指针实际指向的任何类型的'关节' 来实例化正确类型的'定义'. .

  • 然而,我仍然得到关于基类关节不包含后代类确实具有的函数的错误,即使我static_cast指向正确类型的指针.我如何使编译器意识到指针正在被铸造一个具有该功能的新类型?

  • 我也得到关于'typedef'的错误,它可以根据关节*的'type'而不同,编译说它是未定义的.我如何告诉编译器无论如何,其中一个if语句都是真的?(Jointdef在if语句中声明)

这是来源:

template<class Archive>
b2Joint* const preSave(Archive & ar, b2Joint* Joint) 
{
    int Type = Joint->GetType();
    ar & Type; // pulled out first so we know what kind of JointDef to instantiate

    if(Type == b2JointType::e_distanceJoint){
        b2DistanceJointDef JointDef;
        static_cast<b2DistanceJoint *>(Joint);
            JointDef.localAnchorA=      Joint->GetAnchorA();
            JointDef.localAnchorB=      Joint->GetAnchorB();
            JointDef.length=            Joint->GetLength();
            JointDef.frequencyHz=       Joint->GetFrequency();
    }
    if(Type == b2JointType::e_weldJoint){
        b2WeldJointDef JointDef;
        static_cast<b2WeldJoint *>(Joint);
            JointDef.localAnchorA=      Joint->GetAnchorA();
            JointDef.localAnchorB=      Joint->GetAnchorB();
            JointDef.referenceAngle=    Joint->GetReferenceAngle();     // function added
    }
    if(Type == …
Run Code Online (Sandbox Code Playgroud)

c++ pointers if-statement base-class static-cast

0
推荐指数
1
解决办法
1491
查看次数

c ++危险的铸造代码

我很确定这是危险的代码.但是,我想检查是否有人知道究竟会出现什么问题.

假设我有这个类结构:

class A {
protected:
  int a;
public:
  A() { a = 0; }        
  int getA() { return a; }
  void setA(int v) { a = v; }
};

class B: public A {
protected:
  int b;
public:
  B() { b = 0; }
};
Run Code Online (Sandbox Code Playgroud)

然后假设我想要一种自动扩展类的方法,如下所示:

class Base {
public:
   virtual ~Base() {}
};

template <typename T>
class Test: public T, public Base {};
Run Code Online (Sandbox Code Playgroud)

我能做出的一个非常重要的保证是既Base不会也不Test会有任何其他成员变量或方法.它们本质上是空类.

(可能)危险的代码如下:

int main() { …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism inheritance dynamic-cast static-cast

0
推荐指数
1
解决办法
244
查看次数

从'QCell*'类型的static_cast无效到'QWidget*'类型

第4行出现此错误:

void QPiece::setPosition( QPoint value )
{
    _position = value;
    QWidget* parentWidget = static_cast<QWidget *>( _board->Cells[value.x() ][ value.y() ]);
if (parentWidget->layout()) {
    parentWidget->layout()->addWidget( this ); }
else { 
     QHBoxLayout *layout = new QHBoxLayout( parentWidget );
     layout->setMargin(0); 
     layout->addWidget(this); 
     parentWidget->setLayout(layout);
}
    this->setParent( _board->Cells[ value.x() ][ value.y() ] );
}
Run Code Online (Sandbox Code Playgroud)

这是函数Cells()的定义:

class QBoard : public QWidget
{
     Q_OBJECT
     public:
          QCell *Cells[8][8];
          QBoard(QWidget *parent = 0);
          void drawCells();

     private:
          void positionCells();
};
Run Code Online (Sandbox Code Playgroud)

我想我做错了什么,但是什么?提前致谢. 这是QCell的类型,我认为QWidget是QLabel的父级

class QCell:public QLabel

{
     Q_OBJECT

public:
     QCell( QPoint position, QWidget …
Run Code Online (Sandbox Code Playgroud)

qt static-cast

0
推荐指数
1
解决办法
2972
查看次数

从指针转换为引用

我有一个通用的Singleton类,我将用于许多单例类.从指针转换为引用时,泛型类会产生编译错误.

错误3错误C2440:'static_cast':无法从'IApp*'转换为'IApp&'

下面是泛型类,instance()函数中出现编译器错误.

template<typename DERIVED>
class Singleton
{
public:

    static DERIVED& instance()
    {
        if (!_instance) {
            _instance = new DERIVED;
            std::atexit(_singleton_deleter); // Destruction of instance registered at runtime exit (No leak).
        }

        return static_cast<DERIVED&>(_instance);
    }

protected:

    Singleton() {}
    virtual ~Singleton() {}

private:

    static DERIVED* _instance;

    static void _singleton_deleter() { delete _instance; } //Function that manages the destruction of the instance at the end of the execution.

};
Run Code Online (Sandbox Code Playgroud)

有可能这样投吗?我不想instance()返回指针,我更喜欢引用.或者是一个weak_ptr?任何想法如何投这个?

c++ templates static-cast

0
推荐指数
1
解决办法
96
查看次数

为什么const_cast(或static_cast)不添加const?

我正在看这个答案,想要使用.但是,当使用static_cast和时,我得到一个分段错误,const_cast但如果我使用临时变量,一切都很好.很明显,因为非bar()自我调整版本的调用是自我反复的.但我虽然static_cast会导致a const foo*然后选择const版本bar().为什么会这样?

#include <iostream>
using namespace std;

class foo
{
    public:
    void bar() const
    {
        cout << "const" << endl;
    }

    void bar()
    {
        cout << "non-const" << endl;

//      static_cast<const decltype(this)>(this)->bar();

//      const_cast<const decltype(this)>(this)->bar();

        const auto& tmp = *this;
        tmp.bar();
    }
};

int main() {
    foo A;
    A.bar();
    const foo B;
    B.bar();
    static_cast<const foo*>(&A)->bar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ const-cast static-cast c++11

0
推荐指数
1
解决办法
386
查看次数

void*保留继承信息吗?

是否有可能void*保留类型信息?
我试图使它忘记了真正的类型(B*)通过投B* bvoid* v,但它似乎知道它的起源.
这对我来说是好运,但我不知道为什么.

这是一个简化的代码(完整演示): -

#include <iostream>
#include <memory>
using namespace std;

class B{public: int ib=1;};
class C:public B{public: int ic=2;};

int main() {
    B* b=new C();
    void* v=b;                     //<- now you forget it!
    C* c=static_cast<C*>(v);    
    std::cout<<c->ib<<" "<<c->ic<<std::endl; //print 1 and 2 correct (it should not)
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望c指向B*(一个错误的地址)的地址,但似乎知道这vB*正确的.

它为什么有效?难道void*真的记得类型?
它能"记住"多远?

背景

我试图在这里创建一个void* …

c++ void-pointers static-cast c++14

0
推荐指数
1
解决办法
107
查看次数

使用静态转换,因为动态转换失败.不好的做法?

在我正在工作的项目中,我注意到动态向下转换失败了.快速代码检查证实,特定对象实际上从不属于那种类型.但是,我看到其他开发人员通过应用静态强制转换强制执行此非常相同的强制转换.

在我看来,这是一种危险的方法,非常脆弱,如下例所示.这通常不是一个糟糕的编程习惯吗?

class Base
{
public:
    Base(){ m_p = 0; }
protected:
    int m_p;
};

class Derived: public Base
{
public:
    Derived(){ m_arr = new int[1]; }
    void Add_A() { m_p += 2; }
    void Add_B() { m_arr[0] += 3; }
private:
    int* m_arr;
};


Base* parent = new Base();

// obviously fails -> c_d is null
Derived* c_d = dynamic_cast<Derived*>(parent); 

Derived* c_s = static_cast<Derived*>(parent);
c_s->Add_A(); // works
c_s->Add_B(); // crashes, since m_arr does not exist.
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-cast static-cast

0
推荐指数
1
解决办法
202
查看次数

使用static_cast <void>()的目的是什么?

static_cast<void>()是编写void转换的'C++方式'

在en.cppreference.com网站上提到丢弃表达式的值.在下面链接解释部分的四个点

http://en.cppreference.com/w/cpp/language/static_cast

我们应该在哪里和为什么使用static_cast<void>()?举个例子..

c++ casting void static-cast

0
推荐指数
1
解决办法
1010
查看次数

static_cast int要引用int吗?

是否允许:

int x = 1;
int r1 = static_cast<int&>(x);
int r2 = static_cast<int&&>(x)
Run Code Online (Sandbox Code Playgroud)

如果是,那么这些演员表的含义是什么?

这个代码引起了问题:

int x = 1;
int y = 2;
std::swap(x, y); // uses int temp = std::move(x);
Run Code Online (Sandbox Code Playgroud)

c++ static-cast rvalue-reference c++11

0
推荐指数
1
解决办法
85
查看次数

为什么 static_cast&lt;int&gt; 无法将变量转换为 int

我有以下代码,我试图将变量转换为整数,但从倒数第二次打印可以看出,该类型仍被视为“d”-有人知道如何将其更改为 i,因此有它表现得好像它最初被初始化为auto a = 2?

#include <iostream>
#include <stack>
#include <string>
#include <cmath>

using namespace std;

int main()
{
    auto a = 2.3;
    cout << typeid(a).name() << endl;
    a = static_cast<int>(a);
    cout << typeid(a).name() << endl;
    cout << a << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ type-conversion static-cast c++17

0
推荐指数
1
解决办法
141
查看次数