thx dudes & dudettes!
thx dudes & dudettes!
Oh BTW, I feel retarded about the whole *instance* thing.
I think you were saying this is bad:
right?Code:class myClass { aNotherClass x; /* OR */ aNotherClass * x = new aNotherClass(); };
Because I knew that, the terminology just confused me a bit.
Which brings me to another question:
When you use *inheritance* does that create an *instance* of the base class? I'm pretty sure it does because if you have the constructors of your parent & child classes print a simple message, you see both.
I know some people avoid inheritance at all costs, is this the reason why they do that?
No, matsp is not saying that that is bad. Member variables, and members in general, have their uses, i.e. "what's necessary to solve the problem and nothing more". matsp was criticising the use of member variables where local variables are more appropriate.I think you were saying this is bad:
No, it just creates a class hierarchy. I guess that you are actually asking: are instances of a derived class also instances of its base class? The answer would then be yes, as it is the basis for runtime polymorphism.When you use *inheritance* does that create an *instance* of the base class?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Poor, misguided souls.I know some people avoid inheritance at all costs
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
The thing I'm saying is bad is:
The iterator should not be PART OF THE CLASS.Code:class x { vector<int> v; vector<int>::iterator i; };
It is fine to have one class as a member inside another one, likewise, if it's meaningfull to solve the problem, have pointer(s) to class instances.
What you shouldn't do is stuff all sorts of things inside the class that doesn't belong in the class -such as iterators that are just used inside one function, or integer variables used to loop through some fixed lenght array, temporary results of calculations, or some such.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
ok, so that allows me to do fun stuff like:Originally Posted by laserlight
Code:Base *ptr; Child *ptr2 = (Child*)ptr;
C-style casts shouldn't be used in such cases. A base pointer is not neccessarily a valid child pointer. You'll need dynamic_cast which returns 0, if the pointer cannot be cast.
Children can always be up-cast towards base, but base pointer cannot be down-cast to a child pointer unless the base pointer is actually pointing to a particular child (or any class down the hierarchy after the child).
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
should dynamic_cast be used in BOTH cases then? i know in most C++ books, they'll tell you to use daynamic_cast. I was just being sloppy.
No. As anon pointed out, "children can always be up-cast towards base".should dynamic_cast be used in BOTH cases then?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
No cast is necessary for assigning a child pointer/reference to a parent pointer/reference:A dynamic_cast can be used when going the other way if you want to verify that the conversion is valid:Code:Derived* derived_ptr = new Derived; Base* base_ptr = derived_ptr; Derived derived_obj; Base& base_ref = derived_obj;If you know that the conversion will succeed through some other logic, then use a static_cast.Code:Base* base_ptr = new Derived; Derived* derived_ptr = dynamic_cast<Derived*>(base_ptr); if (derived_ptr == 0) // cast failed Derived derived_obj; Base& base_ref = derived_obj; try { Derived& derived_ref = dynamic_cast<Derived&>(base_ref); } catch (std::bad_cast&) { // cast failed }Code:Base* base_ptr = new Derived; Derived* derived_ptr = static_cast<Derived*>(base_ptr); // cast should never fail Derived derived_obj; Base& base_ref = derived_obj; Derived& derived_ref = static_cast<Derived&>(base_ref); // cast should never fail