标签: dynamic-cast

多态性问题:如何检查派生类的类型?

这是我的第一个问题:)

我知道我不应该检查对象类型,而是使用dynamic_cast,但这不会解决我的问题.

我有一个名为Extension和接口的类,名为IExtendable和IInitializable,IUpdatable,ILoadable,IDrawable(后四个基本相同).如果Extension实现IExtendable接口,它可以使用不同的Extension对象扩展自身.

问题是我想允许实现IExtendable的Extension只扩展,扩展实现与原始扩展相同的接口.

你可能不会那么糟糕,所以我试着用代码来解释它:

class IExtendable
{
public:
 IExtendable(void);

 void AddExtension(Extension*);
 void RemoveExtensionByID(unsigned int);

 vector<Extension*>* GetExtensionPtr(){return &extensions;};
private:
 vector<Extension*> extensions;
};

class IUpdatable
{
public:
 IUpdatable(void);
 ~IUpdatable(void);

 virtual void Update();
};

class Extension
{
public:
 Extension(void);
 virtual ~Extension(void);

 void Enable(){enabled=true;};
 void Disable(){enabled=false;};

 unsigned int GetIndex(){return ID;};

private:
 bool enabled;
 unsigned int ID;

 static unsigned int _indexID;
};
Run Code Online (Sandbox Code Playgroud)

现在想象我创建扩展的情况如下:

class MyExtension : public Extension, public IExtendable, public IUpdatable, public IDrawable
{
public:
    MyExtension(void);
    virtual ~MyExtension(void);

    virtual void AddExtension(Extension*);
    virtual void …
Run Code Online (Sandbox Code Playgroud)

polymorphism virtual types dynamic-cast interface

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

我可以教dynamic_cast&lt;&gt;()新技巧吗?

C++ 中有没有一种方法可以构造您的类,以便给定一个指向您的类的指针,您可以指示dynamic_cast<>() 如何转换为您要包装其实现的另一个类?运算符强制转换可以解决问题吗?想象一下,我有一个抽象接口基类,并从中派生出一个creteA和一个concreteB,但concreteB将该接口包装到一个concreteA类型的对象。如果我收到从creteA转换为concreteA的请求,我希望它能够工作:

class Abstract {
public:
  virtual void interface() = 0;

};

class concreteA : public Abstract {
public:
  virtual void interface();

};

class concreteB : public Abstract {
public:
  concreteB(concreteA &underlying)
    : _underlying(&underlying) {
  }
  virtual void interface();

  operator concreteA*() {
    return _underlying;
  }

private:
  concreteA *_underlying;

};

void
myTest() {
  concreteA myClassA;
  concreteB myClassB(myClassA);
  Abstract *abstract = &myClassB;
  concreteA *underlying = dynamic_cast<concreteA *>(abstract);
}
Run Code Online (Sandbox Code Playgroud)

c++ abstract-class dynamic-cast concrete

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

C++ downcast(使用dynamic_cast)返回NULL?

环境:Linux C++/Qt 4x

我不明白为什么以下downcast返回NULL?我粘贴了下面的基础和派生类.

提前感谢任何提示或建议.

-ed

void MainWindow::onRtledaEventHandler(fes::EventArgs eventArgs)
{
   // This cast returns a NULL ?
   fes::AtsCommandEventArgs* atsCommandEventArgs = dynamic_cast<fes::AtsCommandEventArgs*>(&eventArgs);
}


/// The base class for containing event arguments sent to clients using boost::signals2
class EventArgs
{
public:

   EventArgs() {}
   EventArgs(RtledaEventType eventType) :
         m_eventType(eventType) {}
   EventArgs(const EventArgs& eventArgs) :
         m_eventType(eventArgs.m_eventType) {}
   virtual ~EventArgs() {}

   /// The type of event this is
   RtledaEventType eventType() const { return m_eventType; }

protected:
   RtledaEventType m_eventType;
};

// Derived class I am trying to …
Run Code Online (Sandbox Code Playgroud)

c++ null dynamic-cast

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

简单检查dynamic_cast c ++

我做了一个dynamic_cast并想检查演员是否成功.

我现在正在做一个基本的空指针检查.它是这样的:

A *temp_ptr = dynamic_cast<A *>(obj_ptr);
if( (temp_ptr) && (temp_ptr->some_function()))
{
      // do something if the function returns true
}
else
{
      // cast failed or function returns false
      // continue with normal execution
}
Run Code Online (Sandbox Code Playgroud)

这样很好还是我需要使用断言?我所关心的只是那个特殊的功能.我还有其他检查吗?

它会通过代码审查吗?

c++ pointers dynamic-cast typecasting-operator

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

前向声明和转换不完整类型 C++

我有关于前向声明的问题。

namespace downloader {
class IHttpThreadCallback ;
class MemoryHttpRequest ;
Run Code Online (Sandbox Code Playgroud)

}

当我投

auto responseHttpRequest = dynamic_cast<downloader::MemoryHttpRequest*>(m_callback);
Run Code Online (Sandbox Code Playgroud)

它显示警告不完整类型。我应该如何尝试,请给我建议。

除此之外,我尝试包含类,但它不起作用,我认为这不是一个好主意。非常感谢

c++ dynamic-cast

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

使用动态强制转换为派生类内的变量返回不同的值

试图找到一种方法让我的动态转换工作,但我不断收到运行时错误.它在打印时跳转到else语句块值(当它应该是if块值时,但是当我在派生类中调用它时甚至不使用它.所以它显示错误的值而且根本不使用它计算.这是为什么?感谢您的帮助.

class Package
{
protected:
    string name_and_address = "?";

    double cost = 0.0;
    double discount = 0.0;
    double discount_rate = 0.0;

    bool overnight_delivery = false;
    bool insured = false;

    string package_contents = "?";

    double shipcost = 0.0;

public:
    Package() {};
    ~Package() {};

protected:
    virtual double calculate_cost() = 0;

    class Video_Games {}; //defined the classes
    class Genius_Phone {};
    class Sausage {};
    class Albums {};

    // here I'm trying to change "shipcost" inside each of the derived classes
    // to their respective …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-cast

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

dynamic_cast的继承和用法

假设我有3个类如下(因为这是一个例子,它不会编译!):

class Base
{
public:
   Base(){}
   virtual ~Base(){}
   virtual void DoSomething() = 0;
   virtual void DoSomethingElse() = 0;
};

class Derived1
{
public:
   Derived1(){}
   virtual ~Derived1(){}
   virtual void DoSomething(){ ... }
   virtual void DoSomethingElse(){ ... }
   virtual void SpecialD1DoSomething{ ... }
};

class Derived2
{
public:
   Derived2(){}
   virtual ~Derived2(){}
   virtual void DoSomething(){ ... }
   virtual void DoSomethingElse(){ ... }
   virtual void SpecialD2DoSomething{ ... }
};
Run Code Online (Sandbox Code Playgroud)

我想创建Derived1或Derived2的实例,具体取决于在运行时之前不可用的某些设置.

由于我无法在运行时确定派生类型,那么您认为以下是不好的做法吗?...

class X
{
public:
   ....

   void GetConfigurationValue()
   {
      ....
      // Get configuration setting, …
Run Code Online (Sandbox Code Playgroud)

c++ oop inheritance dynamic-cast

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

检查RTTI

我有以下类和方法:

//Base class
class Node {
    public:
        virtual ~Node() {};
        Node() {};
    private:
        // Private things for my implementation.
};

class Element : public Node {
    public:
        // Returns the name of the element.
        const xml::String &name() const {
            return eleName;
        }

        static bool is_Element(const Node *base) {
            Element *p = NULL;
            p = dynamic_cast<Element*>(base);
            return (p!=NULL);
        }

        static const Element *to_Element(const Node *base) {
            return dynamic_cast<Element*>(base);   
        }

    private:
        s_namespace eleNamespace;
        xml::String &eleName;
        // Private things for my implementation.
}; …
Run Code Online (Sandbox Code Playgroud)

c++ oop dynamic-cast class

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

在operator <<中动态转换为派生类型

我有一个基类Tag和一个TagSet继承自的子类Tag.

class Tag
{
  public:
    Tag(std::string);
    std::string tag;
};
std::ostream & operator <<(std::ostream &os, const Tag &t);

class TagSet : public Tag
{
public:
    TagSet();
};
std::ostream & operator <<(std::ostream &os, const TagSet &ts);
Run Code Online (Sandbox Code Playgroud)

和他们的实施

Tag::Tag(std::string t)
: tag(t)
{}

std::ostream & operator <<( std::ostream &os, const Tag &t )
{
  os << "This is a tag";
  return os;
}

TagSet::TagSet()
: Tag("SET")
{}

std::ostream & operator <<(std::ostream &os, const TagSet &ts)
{
  os …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-cast operator-overloading

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

如何在if语句中使用dynamic_cast

所以我有一个简单的任务要做.从一个基类派生出3个类.它们非常简单,将在下面提供.我需要做的是创建一个名为的新类PolymorphicAnimal,它将能够像animalAnimal基类派生的任何其他类一样运行.确切地说,他们需要做的就是在SoundOff调用方法后显示正确的文本.我猜我需要在dynamic_cast这里使用.我的问题是,什么是使用正确的语法dynamic_cast作为一个if声明,并尽一切派生类需要有至少一个虚拟方法呢?

#include "stdafx.h"
#include <iostream>
#include <string>

class Animal {
public:
virtual std::string SoundOff() = 0;
};

class Dog : public Animal {
std::string SoundOff() override { return "Woof"; }
};

class Cat : public Animal {
std::string SoundOff() override { return "Meow"; }
};

class Cow : public Animal {
std::string SoundOff() override { return "Muu"; }
};

class PolymorphicAnimal : public Animal {
std::string …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-cast class base-class

0
推荐指数
2
解决办法
1090
查看次数