CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 56
  1. #1
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    C++ Aha! Moments

    Scott Meyers' shares his Most Important Aha! Moments here : My Most Important C++ Aha! Moments...Ever

    It would be nice to hear what your most important "Aha!" moments are..

    Please try to pick atleast 5.. it would be really nice to see posts from everyone!!

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: C++ Aha! Moments

    The first three immediately changed how I wrote software. The latter three are things I've learned recently.

    - Discovering Patterns (circa 1995)
    - Discovering STL (circa 1996)
    - Discovering boost (circa 2002).
    - Finding a use for TypeLists (this year): I built a collection based on typelists for a very specific use. The power to maintain type information in a heterogeneous container can be very powerful.
    - Finally understanding what a "template template" parameter is (this year), and how to use it.
    - Finally understanding the Visitor pattern (this year). I no longer have to look it up every time I come across it!

    There are many more.

    Jeff

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: C++ Aha! Moments

    Thanks Jeff, for sharing.

    Here are mine:

    1. Operator overloading (2005) - I was new to C++ then. Very fearful of programming as a whole.. I used think that it was something that I could never understand or write myself. Well, having an operator work for your classes? Amazing.. should be something the best brains could only think of writing. How would the syntax work? Must be some really low-level coding involved there. But one day I picked up a book and started reading about them.. and realized they were no different than writing functions. All the job was alreay done for me that I used to worry about were in vain and I was provided with everything that was needed, I just had to provide an implementation, like any other function. This is something that I always tell people to understand when I find some talking or asking about them.

    2. Returning *this from operator= (2005) - I used to write these differently from what my book told me. That is returning void. I mean, I tested it, it worked perfectly fine. Then why do I write that extra return statement? In fact, I did not even think about it or notice it that way. And one day at a job interview, the interviewer asked me this quuestion. It struck me.. really why do we need it? I asked for sometime to think, went on the board with a marker and tried figuring out the reason writing all kinds of stuff.. of which some made sense and some did not. Finally, with the question from the interviewer (they hired me ) about how would I do multiple initialization in one statement:
    Code:
    a=b=c=0;
    ... it struck me. I was so delighted to discover this fact. I must say it sprouted in me a lot of interest about learning C++ well.

    3. Hashtable implementations with O(1) retrievals (2004) - I used to wonder how can a hashtable give me a near constant time retrievals? Then I read an article by Scott Mitchell about C# hash tables on msdn. And it was so easy to understand. You get an object and generate a unique integer for it and then using the hash-function you get to an index which is no different than an index in an array. If it is O(1), it has to be arrays knowing the index to retrieve data for. The way of creating a mapping from any object (usually string) to an integer index was truely something that I appreciated.

    4. Understanding the difference between pass by value and pass by reference (2005) - Considering I learnt C first (however basic and then VB6.0), I was under the impression that when you use a pointer, that is pass by reference and otherwise its pass by value. Then with C++, I finally understood, that in fact pass by pointer is a pass by value mechanism too. And there was a specific reference way of passing objects/variables around. This is arguable when you try to get into the way references could be implemented but if it is truely considered an implementation detail, that is what the difference is. Otherwise, pass by reference is just an illusion.

    5. The most recent one... learnt from Andrei's Modern C++ Design : Expressions used in sizeof() aren't evaluated (or better said sizeof arguments are not evaluated) (2006) - I was going through his book and the section "Detecting Convertibility and Inheritance at compile time". It was just amazing how we could exploit this fact. I did not even know this about sizeof, never came into my mind. Sample code for reference:
    Code:
    template <class T, class U>
    class Conversion
    {
    	typedef char small;
    	class big { char dummy[2];};
    	static small test(U);
    	static big test(...);
    	static T constructT();
          public:	
    	enum {exists=sizeof(test(constructT()))==sizeof(small) };
    };
    I must accept the fact that still there's a lot to learn... learn to apply, much much more than what I have until now. But it's too interesting and early to lose hope
    Last edited by exterminator; October 13th, 2006 at 12:40 PM. Reason: added time (YYYY) :D

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: C++ Aha! Moments

    - Discovering Codeguru (2002)
    I know this sounds cheesy, but I had been programming on my own (and with some friends, but with limited exchanges) only helped by books for more than 10 years before I discovered coding forums like CG. It changed the way I learnt C++, looked at it and programmed in it.
    - Discovering the STL (2002)
    I had a good algorithms background and was always coding my own lists, trees etc. to support the algorithms because the languages I used before (Pascal, C) didn't have them. So when I learnt of the existence of the STL, I immediately fell in love with it.
    - How smart C++ compilers are (2002)
    This came pretty much at the same time as the STL, since I was interested in the assembly generated by iterator-container-algorithm heavy code and whether it was actually slowed down compared to a hand-crafted C-style solution. (Answer: most of the time, it isn't).
    - RAAI in C++ (2002) (Resource acquisition and/as/is Initialization)
    Suddenly dealing with pointers (considering my Pascal/C past and the heavy use of pointers there) didn't seem so bad anymore.
    - Getting a glimps of an understanding on templates (2003 - ongoing)
    There are so many funky things you can do with the clever use of templates and coding your own can be really interesting. I think the first aha moment I had in that was when I programmed my own n-dimensional matrix class that uses some clever tricks to have element access without multiplications.
    Last edited by Yves M; October 12th, 2006 at 02:19 PM.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  5. #5
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Talking Re: C++ Aha! Moments

    So many to choose from ...

    I did quite a bit of C in my spare time before I learned C++, so when I started to look into C++ I could see solutions to existing problems appearing before my eyes. Many of these are general OO, but I discovered them via the C++ language.

    The idea of associating functions with data structures (i.e. classes) immediatly made sense to me. It was a fundamental "yes of course!" moment.

    Templates were more of an "at last" moment. I had been creating big nasty macros before I got my hands on a compiler which could do templates.

    namespaces, how did I ever live without namespaces?

    Exceptions, oh how I love exceptions. And operator overloading, why are some people so against operator overloading?

    Virtual functions, especially the pure virtual functions which COM uses, are great but they did my head in until I looked at it from a C point of view (arrays of function pointers).

    dynamic_cast was related to my understanding of virtual functions, but the fact that you can cross-cast from one class to another (unrelated) class still does my head in, even though I know how it works!

    Code:
    struct A {virtual ~A();};
    struct B {virtual ~B();};
    
    B* f(A* a)
    {
    return dynamic_cast<B*>(a);
    } 
    
    // ...
    
    struct C : A, B {};
    
    void main()
    {
    A* a = new C;
    B* b = f(a);
    }


    Template specialisations are wonderful. Took me a while to understand what the big deal was, but when I needed them they were there for me.

    My fav phrase is "Pure virtual destructor with inline implementation".

    Function objects were another 'you can do what?' moment.
    Last edited by Zaccheus; October 12th, 2006 at 03:01 PM.
    My hobby projects:
    www.rclsoftware.org.uk

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: C++ Aha! Moments

    1. Understanding how containers/iterators/algorithms actually worked
    2. Reading Effective C++ for the first time
    3. Really grokking public inheritance

    No 1 was the real eye-opener for me. When I fully appreciated the sheer beauty of that, it really enhanced my appreciation of good design and spurred me to never accept a second-best solution.

    No 2 cured me of early misunderstandings about how to properly use C++.

    No 3 has saved me from so much bad code that it has to have a place here. Actually, this one is so important to me that I base my assessment of interview candidates primarily on their answer to the question "What is (public) inheritance?". The ones whose answer includes any mention of "reusing base class code" (sadly, a majority) get marked down. The ones who talk about (any of) generalisation/specialisation, abstraction, polymorphism show that they have a reasonably good understanding. Extra marks for noting that inheritance tends to be overused.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: C++ Aha! Moments

    I thank you all for your contributions here, so far. More are welcome!
    Quote Originally Posted by Yves M
    - Discovering Codeguru (2002)
    I know this sounds cheesy, but I had been programming on my own (and with some friends, but with limited exchanges) only helped by books for more than 10 years before I discovered coding forums like CG. It changed the way I learnt C++, looked at it and programmed in it.
    It has made (and still makes) a great difference to me as well. I don't have much words to explain the feeling of obligation to CG.
    Quote Originally Posted by Graham
    No 3 has saved me from so much bad code that it has to have a place here. Actually, this one is so important to me that I base my assessment of interview candidates primarily on their answer to the question "What is (public) inheritance?". The ones whose answer includes any mention of "reusing base class code" (sadly, a majority) get marked down. The ones who talk about (any of) generalisation/specialisation, abstraction, polymorphism show that they have a reasonably good understanding. Extra marks for noting that inheritance tends to be overused.
    Effective C++ was turnaround for me as well! About inheritances - I would give credit to your earlier signature that you held here as well.

    I see boost/STL mentioned too. I would have had kept those reserved for a future thread on the most important C++ libraries.. Anyways, they sure can create Aha! moments too!

  8. #8
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: C++ Aha! Moments

    I am probably too young/too new to programming to have my own Aha! moments, but I think that I know what I need to know and that it will be Aha! when I finally learn it. Maybe it sounds strange, but most of Ahas mentioned above by you are nothing special to me, I have just learned them from books/CG/articles and I did not have to discover them on my own.
    But the first and the greatest AHA! was discovering CG. I was preparing to my first job interview and I knew that there will be a C++ test. I liked C++ at my uni but I thought that lectures are not enough, and I have to google for some articles, online courses etc. So I came to CG. And saw that I know nothing. Lectures did just suck. And lied. I would never know what I know without your help. This site is great. There is lots of smaller ahas falling into this point, like discovering that there is anything like C++ standard, why <iostream.h> is wrong, and what is the difference between C and C++.
    Another one is reading "Symfonia C++" and "Pasja C++" books. These are Polish titles by Jerzy Grebosz, and these are greatest beginner-level C++ books I have ever seen. AFAIK they have never been translated to English or any other language, so you probably won't never know it. Pity. These books are written 'for learners', and not 'by a teacher'. No technical mumbo-jumbo. No difficult words. No nothing hard to understand. Just like a novel about C++.
    I think that these are all Aha!s that happened, but there is a few going to come some day:
    1. Getting more and more acquainted with templates. While basic concepts are quite easy for me, more complicated stuff, like template template param, some aspects of partial specialization, are not.
    2. A bit specialized point of above one: understanding how to use and create own functors and functor adapters (like SGI's unary_compose, binary_compose), member (and global) function adapters, etc.
    3. Understanding aspect programming (if it ever will be introduced to C++)

    Thats about me. Really, very interesting topic.

    Cheers,
    Hob
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: C++ Aha! Moments

    Quote Originally Posted by Hobson
    I am probably too young/too new to programming to have my own Aha! moments, but I think that I know what I need to know and that it will be Aha! when I finally learn it. Maybe it sounds strange, but most of Ahas mentioned above by you are nothing special to me, I have just learned them from books/CG/articles and I did not have to discover them on my own.
    Don't think that way.. the sources to information can be different and many but it is you who finally understands the concepts otherwise all that is just "black characters/words". Aha! is all about that, IMO. The moment when you really feel you got to understand something that was there but you were always far from it or understood it wrong or not in entirety. It could also be a moment when you exterminated the fear of it (like my operator overloading point ). Take another try, I am sure you will be able to recollect a few interesting ones..

  10. #10
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: C++ Aha! Moments

    I just remembered an important one:

    Learning to put constants on the left hand side of == expressions, just in case it somehow ends up with only one = character, was a real "I'm truely out of college now" moment in my first week in my first software job.

    Code:
    if(NULL == p)
    {
    //...
    }
    My hobby projects:
    www.rclsoftware.org.uk

  11. #11
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: C++ Aha! Moments

    Quote Originally Posted by Hobson
    Maybe it sounds strange, but most of Ahas mentioned above by you are nothing special to me, I have just learned them from books/CG/articles and I did not have to discover them on my own.
    The Aha! comes when you are working on a problem and make the connection between the what you learned in the book and the problem. You don't really understand something until you apply it. This is particularly true of Patterns.

    Jeff

  12. #12
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: C++ Aha! Moments

    Quote Originally Posted by jfaust
    This is particularly true of Patterns.
    I agree, it took some time until I finally figured out why the Adapter pattern was a good thing and not just enoying extra work, as it I thought it was in the beginning. Ok, It was Java that gave me this Aha!, but IMO it applies to C++ aswell.

    - petter

  13. #13
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: C++ Aha! Moments

    Quote Originally Posted by wildfrog
    I agree, it took some time until I finally figured out why the Adapter pattern was a good thing and not just enoying extra work, as it I thought it was in the beginning. Ok, It was Java that gave me this Aha!, but IMO it applies to C++ aswell.

    - petter
    And that's the great thing about Patterns!

    For me, it was the Visitor pattern. I was struggling how to avoid runtime type checking (dynamic_cast<>), and something was whispering at the back of my mind... "visitor... visitor...".

    Jeff

  14. #14
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: C++ Aha! Moments

    Few are common with C but I think that's normal. So

    Aha,...
    • ... '{' and '}' are not ugly! Moreover, seems to be even nicer than 'BEGIN' and 'END'.
    • ... pointers are not evil, if you are a good guy!
    • ... Windows API is something cool! Death to console applications!
    • ... references, hmm... quite good idea, Bjarne!
    • ... virtual functions... I can't live without them!
    • ... MFC is not so scary!
    • ... 'mutable'... at last I need you!
    • ... throw/try/catch.. what wonderful! I love them although I don't like baseball.
    • ... Java has no operators overloading, and no pointers! Poor javamen![].
    Last edited by ovidiucucu; October 13th, 2006 at 08:54 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  15. #15
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: C++ Aha! Moments

    Quote Originally Posted by jfaust
    The Aha! comes when you are working on a problem and make the connection between the what you learned in the book and the problem.
    Or if you have been struggling with a problem and then read something helpful in a book!
    My hobby projects:
    www.rclsoftware.org.uk

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured