Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 10-Oct-17 0:28am
Comments
Richard MacCutchan 7-Nov-11 12:48pm    
I know what casting is, but what do you mean by cross casting?
elgaabeb 8-Nov-11 4:11am    
Definition by Danny Kalev : A cross cast converts a multiply-inherited object to one of its secondary base classes.
Maximilien 7-Nov-11 13:18pm    
this : http://www.objectmentor.com/resources/articles/crosscst.pdf (PDF file) ?
Sergey Alexandrovich Kryukov 7-Nov-11 15:48pm    
Could be a complete answer is posted as such.
As to the practical use... not sure, to me it's doubtful...
--SA
elgaabeb 8-Nov-11 4:16am    
I've seen this article, it seems to be in a Reseach context and it's not so clear.
Thx for your comment :)

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.
 
Share this answer
 
Comments
elgaabeb 14-Nov-11 6:01am    
I'm OK with you.
It is typical when you know an object through an interface and you want to access another interface of that same object:

C++
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;
}
 
Share this answer
 
v2
Comments
Dalek Dave 14-Nov-11 4:47am    
Good Answer.
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{
};


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 :

XML
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 :).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900