Thread: help w/template iters

  1. #16
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    thx dudes & dudettes!

  2. #17
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Oh BTW, I feel retarded about the whole *instance* thing.

    I think you were saying this is bad:
    Code:
    class myClass
    {
        aNotherClass x;
    
        /*  OR  */
    
       aNotherClass * x = new aNotherClass();
    };
    right?

    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?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think you were saying this is bad:
    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.

    When you use *inheritance* does that create an *instance* of the base class?
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I know some people avoid inheritance at all costs
    Poor, misguided souls.
    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

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The thing I'm saying is bad is:
    Code:
    class x {
      vector<int> v;
      vector<int>::iterator i;
    };
    The iterator should not be PART OF THE CLASS.

    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.

  6. #21
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    Quote Originally Posted by laserlight
    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.
    ok, so that allows me to do fun stuff like:

    Code:
    Base *ptr;
    Child *ptr2 = (Child*)ptr;

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sh3rpa View Post
    ok, so that allows me to do fun stuff like:

    Code:
    Base *ptr;
    Child *ptr2 = (Child*)ptr;
    Yes, but more:
    Code:
    Base *ptr;
    ptr = new Child;
    --
    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.

  8. #23
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #24
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    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.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    should dynamic_cast be used in BOTH cases then?
    No. As anon pointed out, "children can always be up-cast towards base".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No cast is necessary for assigning a child pointer/reference to a parent pointer/reference:
    Code:
    Derived* derived_ptr = new Derived;
    Base* base_ptr = derived_ptr;
    
    Derived derived_obj;
    Base& base_ref = derived_obj;
    A dynamic_cast can be used when going the other way if you want to verify that the conversion is valid:
    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
    }
    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 = 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

Popular pages Recent additions subscribe to a feed