derived-class

Accessing Values in a Class Similar to boost::any

断了今生、忘了曾经 提交于 2019-12-07 16:12:23
问题 I'm making a simple boost::any -like class for educational purposes, but I can't figure out how to access the stored value. I can set the value perfectly, but when I try to access any member in the "holder" class the compiler just complains that the member wasn't found in the class it was derived from. I can't declare the members as virtual because of the templates. Here's the relevant code: class Element { struct ValueStorageBase { }; template <typename Datatype> struct ValueStorage: public

c++/cli static constructor of derived class is not called

匆匆过客 提交于 2019-12-07 12:53:33
问题 As described in another SO post of me I saw a strange behaviour of my application after moving from VS 2008 (.net 3.5) to VS 2013 (and using .net 4.0, not 4.5). I found that the static constructor (cctor) of a class was not called any more. Therefore I broke the application down into a small test program: DLLs testAssembly_2-0 and testAssembly_4-0 (similar content; testAssembly_4-0 has names with 40 instead of 20 ) namespace testAssembly_20 { public ref class Class20 { public: Class20 () {

serializing Eigen's Matrix using boost.serialization

↘锁芯ラ 提交于 2019-12-07 05:40:38
问题 I'm trying to serialize Eigen's matrix. So that I can serialize a more complex object. I'm using Matrix as a base class and include the serialization in the derived class. I'm confused on how to address Matrix.data(), which returns a c-style array (if i'm correct). This is my attempt: #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> template < class TEigenMatrix> class VariableType : public TEigenMatrix { private: friend class boost::serialization::access;

create instance of unknown derived class in C++

吃可爱长大的小学妹 提交于 2019-12-07 05:39:23
问题 let's say I have a pointer to some base class and I want to create a new instance of this object's derived class. How can I do this? class Base { // virtual }; class Derived : Base { // ... }; void someFunction(Base *b) { Base *newInstance = new Derived(); // but here I don't know how I can get the Derived class type from *b } void test() { Derived *d = new Derived(); someFunction(d); } 回答1: Cloning struct Base { virtual Base* clone() { return new Base(*this); } }; struct Derived : Base {

How to implement a boost::variant derived-class?

喜夏-厌秋 提交于 2019-12-07 00:28:30
问题 I have tried for hours to code a class deriving from boost::variant . But I do not understand what is the problem (I do not understand what the compilation error means). What are the rules to implement a clean boost::variant derived-class? #include <boost/variant.hpp> class MyVariant : public boost::variant<char,bool> { public: MyVariant () : boost::variant<char,bool>( ) {} template <typename T> MyVariant( T& v) : boost::variant<char,bool>(v) {} template <typename T> MyVariant(const T& v) :

C++ calling template functions of Base class

风格不统一 提交于 2019-12-07 00:13:40
问题 Below are two cases. Case 1) Base->BaseIndirect->DerivedIndirect Case 2) Base->Derived In Case 2), I am able to call a template function of Base class using 3 notations. In Case 1), I am able to call template function of Base class using only 1 of those notations. And, I am NOT able to call template function of BaseIndirect using any notation :(. How do I fix this? Thanks. struct Base { template<bool R> inline void fbase(int k) {}; }; template<class ZZ> struct BaseIndirect : Base { template

ES6 constructor returns instance of base class?

青春壹個敷衍的年華 提交于 2019-12-06 23:13:06
问题 The constructor of a derived class returns an instance of the base class. The following code explains my problem: // Vector is defined by an external module (Unreal.js) class TestB extends Vector { constructor() { super(); } Log() { console.log("" + this); } } console.log(new TestB() instanceof TestB) // returns false !!! why ??? console.log(new TestB() instanceof Vector) // returns true... class TestA extends Array { constructor() { super(); } Log() { console.log("" + this); } } console.log

How to create derived classes from a base class using template programming in C++?

丶灬走出姿态 提交于 2019-12-06 11:04:53
问题 I need to create a number of classes (more than 50) from a base class, where the only difference is in the names of the derived classes. For example, my base class is defined as: class BaseError : public std::exception { private: int osErrorCode; const std::string errorMsg; public: int ec; BaseError () : std::exception(), errorMsg() {} BaseError (int errorCode, int osErrCode, const std::string& msg) : std::exception(), errorMsg(msg) { ec = errorCode; osErrorCode = osErrCode; } BaseError

c++/cli static constructor of derived class is not called

限于喜欢 提交于 2019-12-05 22:24:22
As described in another SO post of me I saw a strange behaviour of my application after moving from VS 2008 (.net 3.5) to VS 2013 (and using .net 4.0, not 4.5). I found that the static constructor (cctor) of a class was not called any more. Therefore I broke the application down into a small test program: DLLs testAssembly_2-0 and testAssembly_4-0 (similar content; testAssembly_4-0 has names with 40 instead of 20 ) namespace testAssembly_20 { public ref class Class20 { public: Class20 () { Console::WriteLine (__FUNCTION__"()"); } static Class20 () { Console::WriteLine (__FUNCTION__"()" + " ms

Casting List of derived class to List of base class still returning objects of derived class

落花浮王杯 提交于 2019-12-05 17:44:40
I have the following code: public class BaseClass { public string A { get; set; } } public class DerivedClass : BaseClass { public string B { get; set; } } List<DerivedClass> _derivedClass = new List<DerivedClass>() { new DerivedClass() { A = "test1", B = "test2" } }; List<BaseClass> _baseClass = _derivedClass.Cast<BaseClass>.ToList(); The code compiles without any error, however, I expected _baseClass to only contain Property A (object of type BaseClass ), but it's return both A and B properties (objects of type DerivedClass ). What am I missing and what's the correct way to convert a List of