The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

static_cast or not

class a { ... };
class b { ... };
class c : public a, public b { ... };


<code>

c *pC = new c();
a *pA;
a = c;               

</code>

Is "a = c;" OK in this case since 'c' inherits from 'a' or should I still be static_cast'ing 'c' into 'a'?
caster
Sunday, February 26, 2006
 
 
not
Andy Brice Send private email
Sunday, February 26, 2006
 
 
not as in "a = c is ok"?
caster
Sunday, February 26, 2006
 
 
Don't cast if you don't need to.
sloop
Sunday, February 26, 2006
 
 
Rat's nest. C++. One and the same.
Art Wilkins
Sunday, February 26, 2006
 
 
No casty.
Matt Send private email
Sunday, February 26, 2006
 
 
> Don't cast if you don't need to.

And if you find lots of casts are required, IMHO it is a clear indication that the design is not quite right.
Jussi Jumppanen
Monday, February 27, 2006
 
 
That won't even compile, so how could it be "ok?"
Bill
Monday, February 27, 2006
 
 
Sorry, wasn't paying sufficient attention. Casting to a derived class when you have multiple inheritance is a very bad idea. There is no good way to do it.
Andy Brice Send private email
Monday, February 27, 2006
 
 
Actually, after taking a closer look at what I was trying to do, it looks just like the observer pattern, so I don't know why you guys think it is a bad idea.

Thanks anyway..
caster
Monday, February 27, 2006
 
 
Bill,

It does compile.
caster
Monday, February 27, 2006
 
 
I think what I meant to ask is this:

The code in my OP does compile and work in a very simple case. Before I go ahead and use it all over the place, I am wondering... Since the compiler doesn't flag any errors, is it safe to assume there is no faul play? or am I fooling the compiler somehow?
caster
Monday, February 27, 2006
 
 
One more thing.. you can assume class a is pure virtual, but  class b is not.
caster
Monday, February 27, 2006
 
 
The last issue of the C++ Users Journal has an article that explores that in detail. I read the paper version but you need to register to see it online (may be free, I don't know).

Multiple Inheritance Considered Useful
http://www.cuj.com/documents/s=8188/cuj0602reeves/0602reeves.html

On a sad note, this is the last paper based CUJ issue :-(
JSD Send private email
Monday, February 27, 2006
 
 
Wouldn't you really want to use a dynamic_cast?
Briguy
Monday, February 27, 2006
 
 
caster, your original code definitely won't compile. in "a = c", you're trying to assign one class to another, which just can't be done. Assuming you meant, "pA = pC", that's fine - you can static cast if you like, but there's no need to. Most people don't.

Upcasting isn't great if you're using multiple inheritance, but as long as you don't need to know the starting-point of the object in memory, it's OK.
R. C. James Harlow Send private email
Monday, February 27, 2006
 
 
Yes, I meant pA = pC. Sorry!
caster
Monday, February 27, 2006
 
 
JSD, why no more CUJ?

Jack Reeves is awesome. I tope this won't be his last article. Multiple inheritance IS useful.
Scott
Monday, February 27, 2006
 
 
CUJ is now part of Dr Dobbs (a nice way of saying it disappeared). There is only one new column in DD that is about c++ though.
JSD Send private email
Friday, March 03, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz