C ++交叉实践用例 [英] C++ cross-casting practical use case

查看:91
本文介绍了C ++交叉实践用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





只是为了通过练习了解交叉铸造的使用,

可以任何人给我一个实际的使用示例十字铸造?





Thx。

Hi,

Just to understand the use of cross casting by practice,
Could any person give me a practical example of using a cross casting ?


Thx.

推荐答案

我想我可以给出一个交叉铸造的例子。

我将使用接口来举例,但在C ++中它将是另一个类。所以...



在.Net中,很多类实现IDisposable是很常见的。

现在,你有一个共同的列表type(比方说:Control),但是普通类型不是一次性类型。



使用IDisposable的正常强制转换只会使程序在调用Dispose时崩溃。

但是,当清除列表时,你也想要处理所有可能是IDisposable的对象。



在.Net中我们将使用as运算符。如果结果不同于 null ,那么我们可以处理它。使用C ++会是一样的。





想要其他样品吗?

好​​吧......想象一下你有一个Serializable对象的基类(可以将自己保存到文件中的对象)。

Serialize只接收它将保存的文件(流)。

但是对于某些对象,您可能想要调用一种特殊的保存方式。因此,您检查对象是否支持该特殊保存(即,它从另一个不相关的类继承到基类)。如果它支持,那么你使用它。
I think I can give an example of cross-casting.
I will use interfaces to give the example, but in C++ it will be another class. So...

In .Net it is very common that many classes implement IDisposable.
Now, you have a list of a common type (let's say: Control) but that common type is not a disposable type.

Using a normal cast to an IDisposable will only make the program crash when calling Dispose.
But then, when clearing the list you want to also Dispose all those objects that may be IDisposable.

In .Net we will use the "as" operator. If the result is different than null then we can dispose it. It will be the same using C++.


Want other samples?
Ok... imagine that you have a base class for Serializable objects (objects that can save themselves to a file).
Serialize only receives the file (stream) to which it will save.
But for some objects, you may want to call a special kind of save. So, you check if the object supports that special save (ie, it inherits from another, unrelated class to the base one). If it supports, then you use it.


通常通过接口知道一个对象,并且你想访问同一个对象的另一个接口:



It is typical when you know an object through an interface and you want to access another interface of that same object:

class ia
{
public:
    virtual void a1() =0;
    virtual void a2() =0;
    virtual ~ia() {}
};

class ib
{
public:
    virtual void b1() =0;
    virtual void b2() =0;
    virtual ~ib() {}
};

class agg1:
    public ia,
    public ib
{
public:
    virtual void a1() { std::cout << "a1 in agg1" << std::endl; }
    virtual void a2() { std::cout << "a2 in agg1" << std::endl; }
    virtual void b1() { std::cout << "b1 in agg1" << std::endl; }
    virtual void b2() { std::cout << "b2 in agg1" << std::endl; }
};

class agg2:
    public ia,
    public ib
{
public:
    virtual void a1() { std::cout << "a1 in agg2" << std::endl; }
    virtual void a2() { std::cout << "a2 in agg2" << std::endl; }
    virtual void b1() { std::cout << "b1 in agg2" << std::endl; }
    virtual void b2() { std::cout << "b2 in agg2" << std::endl; }
};

int main()
{
    ia* pa1(new agg1);
    ia* pa2(new agg2);

    pa1->a1();
    pa1->a2();
    pa2->a1();
    pa2->a2();

    ib* pb1(dynamic_cast<ib*>(pa1)); // CROSS CAST from ia to ib thanks to agg1
    ib* pb2(dynamic_cast<ib*>(pa2)); // CROSS CAST from ia to ib thanks to agg2

    if(pb1)
    {
        pb1->b1();
        pb1->b2();
    }
    if(pb2)
    {
        pb2->b1();
        pb2->b2();
    }

    delete pa1;
    delete pa2;
}






谢谢大家的回答。 />
对于迟到的衣服抱歉:)

我一直在寻找实际的例子,但这取决于项目的设计,所以我将给出一个通用的例子。

想象一下,我们有四个级别:ANIMAL,FLYER,DOG,CROWN。



Hi,

Thank you all for your answears.
Sorry for the late answear :)
I've been looking for practical example but it will depends on the design of a project, so i'll give a generic example.
Imagine we have four class : ANIMAL, FLYER, DOG, CROWN.

class Animal {
public:
	virtual ~Animal() {};
	void talk (string sound) {
	cout << " Grrrr " << (char *)&sound <<  endl;
}
};


class Flyer {
public:
	void fly (string destination) {
	cout << " Flying to " << (char *)&destination <<  endl;
}
}; 

class Dog:public Animal{
};


class Crow:public Animal, public Flyer{
};





为了保存Dog和Crow类型的对象,我们需要对基类动物进行向上翻转。

当我们需要调用每个Crow的fly()方法时我们需要知道动物是否继承从Flyer课程开始,这是通过交叉演员完成的:





To save the object of type Dog and Crow we need to do an upcast to the base class animal.
when we need to call a fly() method of each Crow we need to know if the animal inherits from Flyer class or not, this is done by a cross-cast :

Flyer* myAnimal = dynamic_cast<Flyer*>(pAnimal);

 if(!myAnimal)
       cout<<" my animal can't fly :( ..."<<endl;

else

       cout<<" my animal can flye :) ..."<<endl;









我希望它会有所帮助:)。





I hope that it would be helpfull :).


这篇关于C ++交叉实践用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆