I am trying to store objects of inherited types into a std::deque array.  I succeed to store the elements, but when I try to use them, only the parent properties are preserved.  enum Family { parent_, child_};  class Parent {  public:      double a;      Family type;      Parent(double a_, Family type_=Family::parent_) {         a = a_;         type = type_;     }      void print() {         std::cout &lt;&lt; "I am the parent. a:" &lt;&lt; a &lt;&lt; std::endl;     }  };   class Children: public Parent {  public:      double b;     double c;      Children(double a_, double b_, double c_) : Parent(a_, Family::child_) {                 b = b_;         c = c_;     }      void print() {         std::cout &lt;&lt; "I am the children. a:" &lt;&lt; a &lt;&lt; ", b:" &lt;&lt; b &lt;&lt; ", c:" &lt;&lt; c &lt;&lt; std::endl;     } };   void class_inheritance_test() {      std::deque&lt;Parent&gt; vec;      vec.push_back(Parent(1.0));     vec.push_back(Parent(2.0));     vec.push_back(Children(10.0, 20.0, 30.0));      for (Parent &amp;e : vec)         if (e.type == Family::child_){             Children* ch = static_cast&lt;Children*&gt;(&amp;e);             ch-&gt;print();         } else {             e.print();         }        }   The output of this piece of code is:  I am the parent. a:1 I am the parent. a:2 I am the children. a:10, b:0, c:-1.30018e-231   Clearly, when I cast back the Children from its storage as a Parent, the properties defined in the Children class are lost.  I see that the problem is either in the way I store the objects or in the way I cast them back to the class they are supposed to be.  I would appreciate some guidance on how to store objects of multiple types in a vector and be able to use them back.  The other examples I have seen do not include properties, and for my problem I must have properties.  EDIT ------------------------------  For those in the future, the solution is to use shared_ptr:  void class_inheritance_test() {      std::deque&lt;std::shared_ptr&lt;Parent&gt;&gt; vec;      vec.push_back(std::make_shared&lt;Parent&gt;(1.0));     vec.push_back(std::make_shared&lt;Parent&gt;(2.0));     vec.push_back(std::make_shared&lt;Children&gt;(10.0, 20.0, 30.0));      for (std::shared_ptr&lt;Parent&gt; e : vec)         if (e-&gt;type == Family::child_){             std::shared_ptr&lt;Children&gt; ch = std::static_pointer_cast&lt;Children&gt;(e);             ch-&gt;print();         } else {             e-&gt;print();         }        }   The output is exactly what is expected:  I am the parent. a:1 I am the parent. a:2 I am the children. a:10, b:20, c:30
664
Someone mentioned it in the IRC as the slicing problem.
55385
This is one of the possible ways I come out:  struct RetrieveKey {     template &lt;typename T&gt;     typename T::first_type operator()(T keyValuePair) const     {         return keyValuePair.first;     } };  map&lt;int, int&gt; m; vector&lt;int&gt; keys;  // Retrieve all keys transform(m.begin(), m.end(), back_inserter(keys), RetrieveKey());  // Dump all keys copy(keys.begin(), keys.end(), ostream_iterator&lt;int&gt;(cout, "\n"));   Of course, we can also retrieve all values from the map by defining another functor RetrieveValues.  Is there any other way to achieve this easily? (I'm always wondering why std::map does not include a member function for us to do so.)
45776
I'm trying to do something like the following:  enum E;  void Foo(E e);  enum E {A, B, C};   which the compiler rejects.  I've had a quick look on Google and the consensus seems to be "you can't do it", but I can't understand why.  Can anyone explain?  Clarification 2: I'm doing this as I have private methods in a class that take said enum, and I do not want the enum's values exposed - so, for example, I do not want anyone to know that E is defined as  enum E {     FUNCTIONALITY_NORMAL, FUNCTIONALITY_RESTRICTED, FUNCTIONALITY_FOR_PROJECT_X }   as project X is not something I want my users to know about.  So, I wanted to forward declare the enum so I could put the private methods in the header file, declare the enum internally in the cpp, and distribute the built library file and header to people.  As for the compiler - it's GCC.
17104
Consider these classes.  class Base {    ... };  class Derived : public Base {    ... };   this function  void BaseFoo( std::vector&lt;Base*&gt;vec ) {     ... }   And finally my vector  std::vector&lt;Derived*&gt;derived;   I want to pass derived to function BaseFoo, but the compiler doesn't let me. How do I solve this, without copying the whole vector to a std::vector&lt;Base*&gt;?
111702
Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?.  I have tried it and it creates a run-time error.
18078
Note: This was posted when I was starting out C#. With 2014 knowledge, I can truly say that auto-properties are among the best things that ever happened to the C# language.  I am used to create my properties in C# using a private and a public field:  private string title; public string Title {     get { return title;  }     set { title = value;  } }   Now, with  3.0, we got auto-properties:  public string Title { get; set; }   I know this is more a philosophical/subjective questions, but is there any reason to use these auto-properties except from saving five lines of code for each field? My personal gripe is that those properties are hiding stuff from me, and I am not a big fan of black magic.  In fact, the hidden private field does not even show up in the debugger, which is OK given the fact that the get/set functions do nothing. But when I want to actually implement some getter/setter logic, I have to use the private/public pair anyway.  I see the benefit that I save a lot of code (one vs six lines) without losing the ability to change the getter/setter logic later, but then again I can already do that by simply declaring a public field "Public string Title" without the need of the { get; set; } block, thus even saving more code.  So, what am I missing here? Why would anyone actually want to use auto-properties?
77872
Say I have the following C++:  char *p = new char[cb]; SOME_STRUCT *pSS = (SOME_STRUCT *) p; delete pSS;   Is this safe according to the C++ standard? Do I need to cast back to a char* and then use delete[]? I know it'll work in most C++ compilers, because it's plain-ordinary-data, with no destructors. Is it guaranteed to be safe?
117860
Given a struct like this:  public struct SomeStruct {     public SomeStruct(String stringProperty, Int32 intProperty)     {         this.StringProperty = stringProperty;         this.IntProperty = intProperty;     }      public String StringProperty { get; set; }     public Int32 IntProperty { get; set; } }   Of course, a compiler error is generated that reads The 'this' object cannot be used before all of its fields are assigned to.  Is there a way to assign values to the backing fields or the properties themselves, or do I have to implement properties the old-fashioned way with my own explicit backing fields?
15076
I have code like this:  class RetInterface {...}  class Ret1: public RetInterface {...}  class AInterface {   public:      virtual boost::shared_ptr&lt;RetInterface&gt; get_r() const = 0;      ... };  class A1: public AInterface {   public:      boost::shared_ptr&lt;Ret1&gt; get_r() const {...}      ... };   This code does not compile.  In visual studio it raises     C2555: overriding virtual function return type differs and is not   covariant   If I do not use boost::shared_ptr but return raw pointers, the code compiles (I understand this is due to  in C++). I can see the problem is because boost::shared_ptr of Ret1 is not derived from boost::shared_ptr of RetInterface. But I want to return boost::shared_ptr of Ret1 for use in other classes, else I must cast the returned value after the return.      Am I doing something wrong?  If not, why is the language like this - it should be extensible to handle conversion between smart pointers in this scenario? Is there a desirable workaround?
14283
I've been reading up on STL containers in my book on C++, specifically the section on the STL and its containers. Now I do understand each and every one of them have their own specific properties, and I'm close to memorizing all of them... But what I do not yet grasp is in which scenario each of them is used.  What is the explanation? Example code is much prefered.
6545
I am using Lightning Data Services to have the current record already prefetched  &lt;aura:component controller="MyCtrl" implements="force:hasRecordId"&gt;     &lt;aura:attribute name="parent" type="SObject" /&gt;      &lt;aura:handler name="init" value="{!this}" action="{!c.init}" /&gt;      &lt;force:recordData aura:id="recordLoader"                       recordId="{!v.recordId}"                       fields="Name,..."                       targetRecord="{!v.parent}"                       layoutType="FULL" /&gt;     {!v.market.fields.Name.value} &lt;-- WORKS! &lt;/aura:component&gt;   I can directly display its fields in the component without any problems. But when I pass the record   ({     init: function(cmp, evt, helper) {         var action = cmp.get("c.queryData");         action.setParams({ parent: cmp.get("v.parent") });         action.setCallback(this, function(response) {             ...         });          $A.enqueueAction(action);        }, })   into my Apex init method fields are not accessible anymore  public class MyCtrl {      @AuraEnabled     public static String queryData(Parent__c parent) {          // parent is null     } }
62334
I have an array of values that is passed to my function from a different part of the program that I need to store for later processing.  Since I don't know how many times my function will be called before it is time to process the data, I need a dynamic storage structure, so I chose a std::vector.  I don't want to have to do the standard loop to push_back all the values individually, it would be nice if I could just copy it all using something similar to memcpy.
36472
In C#, what is the best way to access a property of the derived class when the generic list contains just the base class.  public class ClassA : BaseClass {    public object PropertyA { get; set; } }  public class ClassB: BaseClass {     public object PropertyB { get; set; } }  public class BaseClass { }  public void Main {     List&lt;BaseClass&gt; MyList = new List&lt;BaseClass&gt;();     ClassA a = new ClassA();     ClassB b = new ClassB();      MyList.Add(a);     MyList.Add(b);      for(int i = 0; i &lt; MyList.Count; i++)     {         //I would like to access PropertyA abd PropertyB from the derived classes             } }
1488
Can anyone explain why following code won't compile? At least on g++ 4.2.4.  And more interesting, why it will compile when I cast MEMBER to int?  #include &lt;vector&gt;  class Foo {   public:       static const int MEMBER = 1;   };  int main(){       vector&lt;int&gt; v;       v.push_back( Foo::MEMBER );       // undefined reference to `Foo::MEMBER'     v.push_back( (int) Foo::MEMBER ); // OK       return 0; }
4408
I just noticed that you can not use standard math operators on an enum such as ++ or +=  So what is the best way to iterate through all of the values in a C++ enum?
114888
I'm in the process of creating a class that stores metadata about a particular data source.  The metadata is structured in a tree, very similar to how XML is structured.  The metadata values can be integer, decimal, or string values.  I'm curious if there is a good way in C++ to store variant data for a situation like this.  I'd like for the variant to use standard libraries, so I'm avoiding the COM, Ole, and SQL VARIANT types that are available.  My current solution looks something like this:  enum MetaValueType {     MetaChar,     MetaString,     MetaShort,     MetaInt,     MetaFloat,     MetaDouble };  union MetaUnion {     char cValue;     short sValue;     int iValue;     float fValue;     double dValue; };  class MetaValue { ... private:     MetaValueType ValueType;     std::string StringValue;     MetaUnion VariantValue; };   The MetaValue class has various Get functions for obtaining the currently stored variant value, but it ends up making every query for a value a big block of if/else if statements to figure out which value I'm looking for.  I've also explored storing the value as only a string, and performing conversions to get different variant types out, but as far as I've seen this leads to a bunch of internal string parsing and error handling which isn't pretty, opens up a big old can of precision and data loss issues with floating point values, and still doesn't eliminate the query if/else if issue stated above.  Has anybody implemented or seen something that's cleaner to use for a C++ variant data type using standard libraries?
24839
class Outer {      class Inner {     public:         Inner() {}         void func() ;     };  private:     static const char* const MYCONST;     int var; };  void Outer::Inner::func() {     var = 1; }  const char* const Outer::MYCONST = "myconst";   This errors out when I compile with class Outer::Inner' has no member named `var'