<p>I am trying to set up dynamic per-item menus (Edit Control Block) in SharePoint 2007.  My goal is to have certain features that are available based on the current user's group membership.</p>

<p>I know that the CustomAction tag that controls the creation of this menu item has a Rights attribute.  The problem that I have with this is that the groups I am using have identical rights in the site (ViewListItems, ManageAlerts, etc).  The groups that we have set up deal more with function, such as Manager, Employee, etc.  We want to be able to assign a custom feature to a group, and have the menu items associated with that feature visible only to members of that group.  Everyone has the same basic site permissions, but will have extra options availble based on their login credentials.</p>

<p>I have seen several articles on modifying the Core.js file to hide items in the context menu, but they are an all-or-nothing approach.  There is an interesting post at <a href="http://blog.thekid.me.uk/archive/2008/04/29/sharepoint-custom-actions-in-a-list-view-webpart.aspx" rel="noreferrer">http://blog.thekid.me.uk/archive/2008/04/29/sharepoint-custom-actions-in-a-list-view-webpart.aspx</a> that shows how to dynamically modify the Actions menu.  It is trivial to modify this example to check the users group and show or hide the menu based on membership.  Unfortunately, this example does not seem to apply to context menu items as evidenced here <a href="http://forums.msdn.microsoft.com/en-US/sharepointdevelopment/thread/c2259839-24c4-4a7e-83e5-3925cdd17c44/" rel="noreferrer">http://forums.msdn.microsoft.com/en-US/sharepointdevelopment/thread/c2259839-24c4-4a7e-83e5-3925cdd17c44/</a>.</p>

<p>Does anyone know of a way to do this without using javascript?  If not, what is the best way to check the user's group from javascript?</p>

[{'answer_id': 30384, 'author': 'Matt Rogish', 'author_id': 2590, 'author_profile': 'https://Stackoverflow.com/users/2590', 'pm_score': 2, 'selected': False, 'text': '<p>The book <em><a href="http://www.semantics.org/cpp_gotchas/" rel="nofollow noreferrer">C++ Gotchas</a></em> may prove useful.</p>\n'}, {'answer_id': 30391, 'author': 'sparkes', 'author_id': 269, 'author_profile': 'https://Stackoverflow.com/users/269', 'pm_score': 3, 'selected': False, 'text': '<p>The web page <em><a href="http://developer.kde.org/~wheeler/cpp-pitfalls.html" rel="nofollow noreferrer">C++ Pitfalls</a></em> by Scott Wheeler covers some of the main C++ pitfalls.</p>\n'}, {'answer_id': 30412, 'author': '17 of 26', 'author_id': 2284, 'author_profile': 'https://Stackoverflow.com/users/2284', 'pm_score': 4, 'selected': False, 'text': '<p>Some must have C++ books that will help you avoid common C++ pitfalls:</p>\n\n<p><a href="https://rads.stackoverflow.com/amzn/click/com/0321334876" rel="noreferrer" rel="nofollow noreferrer">Effective C++</a><br>\n<a href="https://rads.stackoverflow.com/amzn/click/com/020163371X" rel="noreferrer" rel="nofollow noreferrer">More Effective C++</a><br>\n<a href="https://rads.stackoverflow.com/amzn/click/com/0201749629" rel="noreferrer" rel="nofollow noreferrer">Effective STL</a></p>\n\n<p>The Effective STL book explains the vector of bools issue :)</p>\n'}, {'answer_id': 30420, 'author': 'Brian Stewart', 'author_id': 3114, 'author_profile': 'https://Stackoverflow.com/users/3114', 'pm_score': 7, 'selected': True, 'text': '<p>A short list might be:</p>\n\n<ul>\n<li>Avoid memory leaks through use shared pointers to manage memory allocation and cleanup</li>\n<li>Use the <a href="https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization" rel="noreferrer">Resource Acquisition Is Initialization</a> (RAII) idiom to manage resource cleanup - especially in the presence of exceptions</li>\n<li>Avoid calling virtual functions in constructors</li>\n<li>Employ minimalist coding techniques where possible - for example, declaring variables only when needed, scoping variables, and early-out design where possible. </li>\n<li>Truly understand the exception handling in your code - both with regard to exceptions you throw, as well as ones thrown by classes you may be using indirectly. This is especially important in the presence of templates.</li>\n</ul>\n\n<p>RAII, shared pointers and minimalist coding are of course not specific to C++, but they help avoid problems that do frequently crop up when developing in the language. </p>\n\n<p>Some excellent books on this subject are:</p>\n\n<ul>\n<li>Effective C++ - Scott Meyers</li>\n<li>More Effective C++ - Scott Meyers</li>\n<li>C++ Coding Standards - Sutter &amp; Alexandrescu</li>\n<li>C++ FAQs - Cline</li>\n</ul>\n\n<p>Reading these books has helped me more than anything else to avoid the kind of pitfalls you are asking about.</p>\n'}, {'answer_id': 30426, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': '<p>The most important pitfalls for beginning developers is to avoid confusion between C and C++. C++ should never be treated as a mere better C or C with classes because this prunes its power and can make it even dangerous (especially when using memory as in C).</p>\n'}, {'answer_id': 30449, 'author': 'Paul Whitehurst', 'author_id': 3261, 'author_profile': 'https://Stackoverflow.com/users/3261', 'pm_score': 2, 'selected': False, 'text': '<p>Check out <a href="http://boost.org" rel="nofollow noreferrer">boost.org</a>.  It provides a lot of additional functionality, especially their smart pointer implementations.</p>\n'}, {'answer_id': 30470, 'author': 'Rob Wells', 'author_id': 2974, 'author_profile': 'https://Stackoverflow.com/users/2974', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ve already mentioned it a few times, but Scott Meyers\' books <a href="https://rads.stackoverflow.com/amzn/click/com/0321334876" rel="nofollow noreferrer" rel="nofollow noreferrer">Effective C++</a> and <a href="https://rads.stackoverflow.com/amzn/click/com/0201749629" rel="nofollow noreferrer" rel="nofollow noreferrer">Effective STL</a> are really worth their weight in gold for helping with C++.</p>\n\n<p>Come to think of it, Steven Dewhurst\'s <a href="https://rads.stackoverflow.com/amzn/click/com/0321125185" rel="nofollow noreferrer" rel="nofollow noreferrer">C++ Gotchas</a> is also an excellent "from the trenches" resource. His item on rolling your own exceptions and how they should be constructed really helped me in one project.</p>\n'}, {'answer_id': 34064, 'author': 'Tyler', 'author_id': 3561, 'author_profile': 'https://Stackoverflow.com/users/3561', 'pm_score': 3, 'selected': False, 'text': "<p>Two gotchas that I wish I hadn't learned the hard way:</p>\n\n<p>(1) A lot of output (such as printf) is buffered by default.  If you're debugging crashing code, and you're using buffered debug statements, the last output you see may <em>not</em> really be the last print statement encountered in the code.  The solution is to flush the buffer after each debug print (or turn off the buffering altogether).</p>\n\n<p>(2) Be careful with initializations - (a) avoid class instances as globals / statics; and (b) try to initialize all your member variables to some safe value in a ctor, even if it's a trivial value such as NULL for pointers.</p>\n\n<p>Reasoning: the ordering of global object initialization is not guaranteed (globals includes static variables), so you may end up with code that seems to fail nondeterministically since it depends on object X being initialized before object Y.  If you don't explicitly initialize a primitive-type variable, such as a member bool or enum of a class, you'll end up with different values in surprising situations -- again, the behavior can seem very nondeterministic.</p>\n"}, {'answer_id': 37369, 'author': 'Chris de Vries', 'author_id': 3836, 'author_profile': 'https://Stackoverflow.com/users/3836', 'pm_score': 2, 'selected': False, 'text': '<p>PRQA have <a href="http://www.codingstandard.com/" rel="nofollow noreferrer">an excellent and free C++ coding standard</a> based on books from Scott Meyers, Bjarne Stroustrop and Herb Sutter. It brings all this information together in one document.</p>\n'}, {'answer_id': 48676, 'author': 'Motti', 'author_id': 3848, 'author_profile': 'https://Stackoverflow.com/users/3848', 'pm_score': 2, 'selected': False, 'text': '<p>Here are a few pits I had the misfortune to fall into. All these have good reasons which I only understood after being bitten by behaviour that surprised me. </p>\n\n<ul>\n<li><p><code>virtual</code> functions in constructors <a href="https://stackoverflow.com/questions/36832" title="Virtual functions in constructors, why do languages differ?">aren\'t</a>.</p></li>\n<li><p>Don\'t violate the <a href="http://en.wikipedia.org/wiki/One_Definition_Rule" rel="nofollow noreferrer">ODR (One Definition Rule)</a>, that\'s what anonymous namespaces are for (among other things).</p></li>\n<li><p>Order of initialization of members depends on the order in which they are declared.</p>\n\n<pre><code>class bar {\n    vector&lt;int&gt; vec_;\n    unsigned size_; // Note size_ declared *after* vec_\npublic:\n    bar(unsigned size)\n        : size_(size)\n        , vec_(size_) // size_ is uninitialized\n        {}\n};\n</code></pre></li>\n<li><p>Default values and <code>virtual</code> have different semantics.</p>\n\n<pre><code>class base {\npublic:\n    virtual foo(int i = 42) { cout &lt;&lt; "base " &lt;&lt; i; }\n};\n\nclass derived : public base {\npublic:\n    virtual foo(int i = 12) { cout &lt;&lt; "derived "&lt;&lt; i; }\n};\n\nderived d;\nbase&amp; b = d;\nb.foo(); // Outputs `derived 42`\n</code></pre></li>\n</ul>\n'}, {'answer_id': 48708, 'author': 'Registered User', 'author_id': 5087, 'author_profile': 'https://Stackoverflow.com/users/5087', 'pm_score': 2, 'selected': False, 'text': '<p>Be careful when using smart pointers and container classes.</p>\n'}, {'answer_id': 71386, 'author': '0124816', 'author_id': 11521, 'author_profile': 'https://Stackoverflow.com/users/11521', 'pm_score': 4, 'selected': False, 'text': '<p>Brian has a great list: I\'d add "Always mark single argument constructors explicit (except in those rare cases you want automatic casting)."</p>\n'}, {'answer_id': 117741, 'author': 'Avdi', 'author_id': 20487, 'author_profile': 'https://Stackoverflow.com/users/20487', 'pm_score': 3, 'selected': False, 'text': "<p>Not really a specific tip, but a general guideline: check your sources.  C++ is an old language, and it has changed a lot over the years.  Best practices have changed with it, but unfortunately there's still a lot of old information out there.  There have been some very good book recommendations on here - I can second buying every one of Scott Meyers C++ books.  Become familiar with Boost and with the coding styles used in Boost - the people involved with that project are on the cutting edge of C++ design.</p>\n\n<p>Do not reinvent the wheel.  Become familiar with the STL and Boost, and use their facilities whenever possible rolling your own.  In particular, use STL strings and collections unless you have a very, very good reason not to.  Get to know auto_ptr and the Boost smart pointers library very well, understand under which circumstances each type of smart pointer is intended to be used, and then use smart pointers everywhere you might otherwise have used raw pointers.  Your code will be just as efficient and a lot less prone to memory leaks.</p>\n\n<p>Use static_cast, dynamic_cast, const_cast, and reinterpret_cast instead of C-style casts.  Unlike C-style casts they will let you know if you are really asking for a different type of cast than you think you are asking for.  And they stand out viisually, alerting the reader that a cast is taking place.</p>\n"}, {'answer_id': 117788, 'author': 'epatel', 'author_id': 842, 'author_profile': 'https://Stackoverflow.com/users/842', 'pm_score': 2, 'selected': False, 'text': '<p>Avoid <a href="http://www.idinews.com/quasiClass.pdf" rel="nofollow noreferrer">pseudo classes and quasi classes</a>... Overdesign basically.</p>\n'}, {'answer_id': 117801, 'author': 'teratorn', 'author_id': 14739, 'author_profile': 'https://Stackoverflow.com/users/14739', 'pm_score': 2, 'selected': False, 'text': '<ol>\n<li>Not reading the <a href="http://www.parashift.com/c++-faq-lite/" rel="nofollow noreferrer">C++ FAQ Lite</a>. It explains many bad (and good!) practices.</li>\n<li>Not using <a href="http://en.wikipedia.org/wiki/Boost_%28C%2B%2B_libraries%29" rel="nofollow noreferrer">Boost</a>. You\'ll save yourself a lot of frustration by taking advantage of Boost where possible.</li>\n</ol>\n'}, {'answer_id': 117820, 'author': 'ilitirit', 'author_id': 9825, 'author_profile': 'https://Stackoverflow.com/users/9825', 'pm_score': 1, 'selected': False, 'text': '<p>Read the book <em><a href="https://rads.stackoverflow.com/amzn/click/com/0321125185" rel="nofollow noreferrer" rel="nofollow noreferrer">C++ Gotchas: Avoiding Common Problems in Coding and Design</a></em>.</p>\n'}, {'answer_id': 281451, 'author': 'Martin York', 'author_id': 14065, 'author_profile': 'https://Stackoverflow.com/users/14065', 'pm_score': 3, 'selected': False, 'text': '<p>Using C++ like C. Having a create-and-release cycle in the code.</p>\n\n<p>In C++, this is not exception safe and thus the release may not be executed. In C++, we use <a href="https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization" rel="nofollow noreferrer">RAII</a> to solve this problem.</p>\n\n<p>All resources that have a manual create and release should be wrapped in an object so these actions are done in the constructor/destructor.</p>\n\n<pre><code>// C Code\nvoid myFunc()\n{\n    Plop*   plop = createMyPlopResource();\n\n    // Use the plop\n\n    releaseMyPlopResource(plop);\n}\n</code></pre>\n\n<p>In C++, this should be wrapped in an object:</p>\n\n<pre><code>// C++\nclass PlopResource\n{\n    public:\n        PlopResource()\n        {\n            mPlop=createMyPlopResource();\n            // handle exceptions and errors.\n        }\n        ~PlopResource()\n        {\n             releaseMyPlopResource(mPlop);\n        }\n    private:\n        Plop*  mPlop;\n };\n\nvoid myFunc()\n{\n    PlopResource  plop;\n\n    // Use the plop\n    // Exception safe release on exit.\n}\n</code></pre>\n'}, {'answer_id': 281487, 'author': 'Mark Ransom', 'author_id': 5987, 'author_profile': 'https://Stackoverflow.com/users/5987', 'pm_score': 0, 'selected': False, 'text': '<p>Always check a pointer before you dereference it.  In C, you could usually count on a crash at the point where you dereference a bad pointer; in C++, you can create an invalid reference which will crash at a spot far removed from the source of the problem.</p>\n\n<pre><code>class SomeClass\n{\n    ...\n    void DoSomething()\n    {\n        ++counter;    // crash here!\n    }\n    int counter;\n};\n\nvoid Foo(SomeClass &amp; ref)\n{\n    ...\n    ref.DoSomething();    // if DoSomething is virtual, you might crash here\n    ...\n}\n\nvoid Bar(SomeClass * ptr)\n{\n    Foo(*ptr);    // if ptr is NULL, you have created an invalid reference\n                  // which probably WILL NOT crash here\n}\n</code></pre>\n'}, {'answer_id': 281562, 'author': 'blizpasta', 'author_id': 20646, 'author_profile': 'https://Stackoverflow.com/users/20646', 'pm_score': 0, 'selected': False, 'text': '<p>Intention is <code>(x == 10)</code>:</p>\n\n<pre><code>if (x = 10) {\n    //Do something\n}\n</code></pre>\n\n<p>I thought I would never make this mistake myself, but I actually did it recently.</p>\n'}, {'answer_id': 281593, 'author': 'David Thornley', 'author_id': 14148, 'author_profile': 'https://Stackoverflow.com/users/14148', 'pm_score': 1, 'selected': False, 'text': "<p>Keep the name spaces straight (including struct, class, namespace, and using).  That's my number-one frustration when the program just doesn't compile.</p>\n"}, {'answer_id': 281609, 'author': 'David Thornley', 'author_id': 14148, 'author_profile': 'https://Stackoverflow.com/users/14148', 'pm_score': 1, 'selected': False, 'text': '<p>To mess up, use straight pointers a lot.  Instead, use RAII for almost anything, making sure of course that you use the right smart pointers.  If you write "delete" anywhere outside a handle or pointer-type class, you\'re very likely doing it wrong.</p>\n'}, {'answer_id': 282056, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<ul>\n<li><p>Blizpasta. That\'s a huge one I see a lot...</p></li>\n<li><p>Uninitialized variables are a huge mistake that students of mine make. A lot of Java folks forget that just saying "int counter" doesn\'t set counter to 0. Since you have to define variables in the h file (and initialize them in the constructor/setup of an object), it\'s easy to forget.</p></li>\n<li><p>Off-by-one errors on <code>for</code> loops / array access.</p></li>\n<li><p>Not properly cleaning object code when voodoo starts.</p></li>\n</ul>\n'}, {'answer_id': 282070, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 1, 'selected': False, 'text': "<blockquote>\n  <ul>\n  <li><code>static_cast</code> downcast on a virtual base class</li>\n  </ul>\n</blockquote>\n\n<p>Not really... Now about my misconception: I thought that <code>A</code> in the following was a virtual base class when in fact it's not; it's, according to 10.3.1, a <em>polymorphic class</em>. Using <code>static_cast</code> here seems to be fine.</p>\n\n<pre><code>struct B { virtual ~B() {} };\n\nstruct D : B { };\n</code></pre>\n\n<p>In summary, yes, this is a dangerous pitfall.</p>\n"}, {'answer_id': 282921, 'author': 'gsarkis', 'author_id': 36786, 'author_profile': 'https://Stackoverflow.com/users/36786', 'pm_score': -1, 'selected': False, 'text': '<pre><code>#include &lt;boost/shared_ptr.hpp&gt;\nclass A {\npublic:\n  void nuke() {\n     boost::shared_ptr&lt;A&gt; (this);\n  }\n};\n\nint main(int argc, char** argv) {\n  A a;\n  a.nuke();\n  return(0);\n}\n</code></pre>\n'}, {'answer_id': 285100, 'author': 'xtofl', 'author_id': 6610, 'author_profile': 'https://Stackoverflow.com/users/6610', 'pm_score': 2, 'selected': False, 'text': "<p>Forgetting to define a base class destructor virtual. This means that calling <code>delete</code> on a Base* won't end up destructing the derived part.</p>\n"}, {'answer_id': 293047, 'author': 'Johannes Schaub - litb', 'author_id': 34509, 'author_profile': 'https://Stackoverflow.com/users/34509', 'pm_score': 6, 'selected': False, 'text': '<h2>Pitfalls in decreasing order of their importance</h2>\n\n<p>First of all, you should visit the award winning <a href="https://isocpp.org/faq" rel="nofollow noreferrer">C++ FAQ</a>. It has many good answers to pitfalls. If you have further questions, visit <code>##c++</code> on <code>irc.freenode.org</code> in <a href="http://en.wikipedia.org/wiki/Internet_Relay_Chat" rel="nofollow noreferrer">IRC</a>. We are glad to help you, if we can. Note all the following pitfalls are originally written. They are not just copied from random sources.</p>\n\n<hr>\n\n<blockquote>\n  <p><code>delete[]</code> on <code>new</code>, <code>delete</code> on <code>new[]</code></p>\n</blockquote>\n\n<p><strong>Solution</strong>: Doing the above yields to undefined behavior: Everything could happen. Understand your code and what it does, and always <code>delete[]</code> what you <code>new[]</code>, and <code>delete</code> what you <code>new</code>, then that won\'t happen.</p>\n\n<p><strong>Exception</strong>:</p>\n\n<pre><code>typedef T type[N]; T * pT = new type; delete[] pT;\n</code></pre>\n\n<p>You need to <code>delete[]</code> even though you <code>new</code>, since you new\'ed an array. So if you are working with <code>typedef</code>, take special care.</p>\n\n<hr>\n\n<blockquote>\n  <p>Calling a virtual function in a constructor or destructor</p>\n</blockquote>\n\n<p><strong>Solution</strong>: Calling a virtual function won\'t call the overriding functions in the derived classes. Calling a <em>pure virtual function</em> in a constructor or desctructor is undefined behavior.</p>\n\n<hr>\n\n<blockquote>\n  <p>Calling <code>delete</code> or <code>delete[]</code> on an already deleted pointer</p>\n</blockquote>\n\n<p><strong>Solution</strong>: Assign 0 to every pointer you delete. Calling <code>delete</code> or <code>delete[]</code> on a null-pointer does nothing.</p>\n\n<hr>\n\n<blockquote>\n  <p>Taking the sizeof of a pointer, when the number of elements of an \'array\' is to be calculated.</p>\n</blockquote>\n\n<p><strong>Solution</strong>: Pass the number of elements alongside the pointer when you need to pass an array as a pointer into a function. Use the function proposed <a href="https://stackoverflow.com/questions/275994/whats-the-best-way-to-do-a-backwards-loop-in-ccc#276053">here</a> if you take the sizeof of an array that is supposed to be really an array.</p>\n\n<hr>\n\n<blockquote>\n  <p>Using an array as if it were a pointer. Thus, using <code>T **</code> for a two dimentional array.</p>\n</blockquote>\n\n<p><strong>Solution</strong>: See <a href="https://stackoverflow.com/questions/274865/pointer-question-in-c#274943">here</a> for why they are different and how you handle them.</p>\n\n<hr>\n\n<blockquote>\n  <p>Writing to a string literal: <code>char * c = "hello"; *c = \'B\';</code></p>\n</blockquote>\n\n<p><strong>Solution</strong>: Allocate an array that is initialized from the data of the string literal, then you can write to it:</p>\n\n<pre><code>char c[] = "hello"; *c = \'B\';\n</code></pre>\n\n<p>Writing to a string literal is undefined behavior. Anyway, the above conversion from a string literal to <code>char *</code> is deprecated. So compilers will probably warn if you increase the warning level.</p>\n\n<hr>\n\n<blockquote>\n  <p>Creating resources, then forgetting to free them when something throws.</p>\n</blockquote>\n\n<p><strong>Solution</strong>: Use smart pointers like <a href="http://en.cppreference.com/w/cpp/memory/unique_ptr" rel="nofollow noreferrer"><code>std::unique_ptr</code></a> or <a href="http://en.cppreference.com/w/cpp/memory/shared_ptr" rel="nofollow noreferrer"><code>std::shared_ptr</code></a> as pointed out by other answers.</p>\n\n<hr>\n\n<blockquote>\n  <p>Modifying an object twice like in this example: <code>i = ++i;</code></p>\n</blockquote>\n\n<p><strong>Solution</strong>: The above was supposed to assign to <code>i</code> the value of <code>i+1</code>. But what it does is not defined. Instead of incrementing <code>i</code> and assigning the result, it changes <code>i</code> on the right side as well. Changing an object between two sequence points is undefined behavior. Sequence points include <code>||</code>, <code>&amp;&amp;</code>, <code>comma-operator</code>, <code>semicolon</code> and <code>entering a function</code> (non exhaustive list!). Change the code to the following to make it behave correctly: <code>i = i + 1;</code></p>\n\n<hr>\n\n<h2>Misc Issues</h2>\n\n<blockquote>\n  <p>Forgetting to flush streams before calling a blocking function like <code>sleep</code>.</p>\n</blockquote>\n\n<p><strong>Solution</strong>: Flush the stream by streaming either <code>std::endl</code> instead of <code>\\n</code> or by calling <code>stream.flush();</code>.</p>\n\n<hr>\n\n<blockquote>\n  <p>Declaring a function instead of a variable.</p>\n</blockquote>\n\n<p><strong>Solution</strong>: The issue arises because the compiler interprets for example</p>\n\n<pre><code>Type t(other_type(value));\n</code></pre>\n\n<p>as a function declaration of a function <code>t</code> returning <code>Type</code> and having a parameter of type <code>other_type</code> which is called <code>value</code>. You solve it by putting parentheses around the first argument. Now you get a variable <code>t</code> of type <code>Type</code>:</p>\n\n<pre><code>Type t((other_type(value)));\n</code></pre>\n\n<hr>\n\n<blockquote>\n  <p>Calling the function of a free object that is only declared in the current translation unit (<code>.cpp</code> file).</p>\n</blockquote>\n\n<p><strong>Solution</strong>: The standard doesn\'t define the order of creation of free objects (at namespace scope) defined across different translation units. Calling a member function on an object not yet constructed is undefined behavior. You can define the following function in the object\'s translation unit instead and call it from other ones:</p>\n\n<pre><code>House &amp; getTheHouse() { static House h; return h; }\n</code></pre>\n\n<p>That would create the object on demand and leave you with a fully constructed object at the time you call functions on it.</p>\n\n<hr>\n\n<blockquote>\n  <p>Defining a template in a <code>.cpp</code> file, while it\'s used in a different <code>.cpp</code> file.</p>\n</blockquote>\n\n<p><strong>Solution</strong>: Almost always you will get errors like <code>undefined reference to ...</code>. Put all the template definitions in a header, so that when the compiler is using them, it can already produce the code needed.</p>\n\n<hr>\n\n<blockquote>\n  <p><code>static_cast&lt;Derived*&gt;(base);</code> if base is a pointer to a virtual base class of <code>Derived</code>.</p>\n</blockquote>\n\n<p><strong>Solution</strong>: A virtual base class is a base which occurs only once, even if it is inherited more than once by different classes indirectly in an inheritance tree. Doing the above is not allowed by the Standard. Use dynamic_cast to do that, and make sure your base class is polymorphic.</p>\n\n<hr>\n\n<blockquote>\n  <p><code>dynamic_cast&lt;Derived*&gt;(ptr_to_base);</code> if base is non-polymorphic</p>\n</blockquote>\n\n<p><strong>Solution</strong>: The standard doesn\'t allow a downcast of a pointer or reference when the object passed is not polymorphic. It or one of its base classes has to have a virtual function.</p>\n\n<hr>\n\n<blockquote>\n  <p>Making your function accept <code>T const **</code></p>\n</blockquote>\n\n<p><strong>Solution</strong>: You might think that\'s safer than using <code>T **</code>, but actually it will cause headache to people that want to pass <code>T**</code>: The standard doesn\'t allow it. It gives a neat example of why it is disallowed:</p>\n\n<pre><code>int main() {\n    char const c = ’c’;\n    char* pc;\n    char const** pcc = &amp;pc; //1: not allowed\n    *pcc = &amp;c;\n    *pc = ’C’; //2: modifies a const object\n}\n</code></pre>\n\n<p>Always accept <code>T const* const*;</code> instead.</p>\n\n<h2></h2>\n\n<p>Another (closed) pitfalls thread about C++, so people looking for them will find them, is Stack Overflow question <em><a href="https://stackoverflow.com/questions/280531">C++ pitfalls</a></em>.</p>\n'}, {'answer_id': 293080, 'author': 'lurks', 'author_id': 1876, 'author_profile': 'https://Stackoverflow.com/users/1876', 'pm_score': 0, 'selected': False, 'text': '<p>The essay/article <em><a href="http://www.goingware.com/tips/parameters/" rel="nofollow noreferrer">Pointers, references and Values</a></em> is very useful. It talks avoid avoiding pitfalls and good practices. You can browse the whole site too, which contains programming tips, mainly for C++.</p>\n'}, {'answer_id': 754364, 'author': 'Todd Stout', 'author_id': 59768, 'author_profile': 'https://Stackoverflow.com/users/59768', 'pm_score': 0, 'selected': False, 'text': '<p>I spent many years doing C++ development. I wrote a <a href="http://btstout.blogspot.com/2005_09_01_archive.html" rel="nofollow noreferrer">quick summary</a> of problems I had with it years ago. Standards-compliant compilers are not really a problem anymore, but I suspect the other pitfalls outlined are still valid.</p>\n'}, {'answer_id': 3986213, 'author': 'starblue', 'author_id': 49246, 'author_profile': 'https://Stackoverflow.com/users/49246', 'pm_score': 0, 'selected': False, 'text': '<p>Forgetting an <code>&amp;</code> and thereby creating a copy instead of a reference.</p>\n\n<p>This happened to me twice in different ways:</p>\n\n<ul>\n<li><p>One instance was in an argument list, which caused a large object to be put on the stack with the result of a stack overflow and crash of the embedded system.</p></li>\n<li><p>I forgot the <code>&amp;</code> on an instance variable, with the effect that the object was copied. After registering as a listener to the copy I wondered why I never got the callbacks from the original object.</p></li>\n</ul>\n\n<p>Both where rather hard to spot, because the difference is small and hard to see, and otherwise objects and references are used syntactically in the same way.</p>\n'}]
['https://Stackoverflow.com/questions/30397', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2470/']