tags:

views:

247

answers:

3

I am using gdi+ and c++. I have a question about SolidBrush. How To clone a SolidBrush?

SolidBrush* oldBrush xxx;
Brush* newBrush = oldBrush->Clone();

I found newBrush is a Brush Object. Which mean if I use dynamic_cast<SolidBrush>(newBursh), I will always get NULL.

I read the .h file of gdi+ SolidBrush seems used Brush's virtual Clone method, it do not override it!

Why?

Thanks for solutions, but I still have a question? why SolidBrush do not implement Clone Method?

A: 
Aamir
+1  A: 

Brush can do everything SolidBrush can. SolidBrush is just a convenient way to create a Brush with a solid color.

Here's an example derived from this example at MSDN:

SolidBrush solidBrush(Color(255,255,0,0));
Brush *clone = solidBrush.Clone();

You can then just go ahead and use clone like any other brush and it'll behave exactly like solidBrush.

Naaff
A: 

why SolidBrush do not implement Clone Method?

SolidBrush inherits the clone method from Brush. Implementing it again in SolidBrush would be redundant.

Tim Sylvester
Actually I think clone method should invoke its own construct, so it must be overrided. Otherwise how can it create a object of its own type?
The `SolidBrush` class creates exactly the same type of GDI+ object as the `Brush` class. These classes are simply C++ wrappers over C APIs, and `Brush::Clone` just calls `::GdipCloneBrush`, and wraps the result in a new `Brush` object (NOT a `SolidBrush` object). The code is right there in `GdiPlusBrush.h`, you should look at it.
Tim Sylvester