derived-class

Why does my C++ subclass need an explicit constructor?

白昼怎懂夜的黑 提交于 2019-12-05 15:24:12
问题 I have a base class that declares and defines a constructor, but for some reason my publicly derived class is not seeing that constructor, and I therefore have to explicitly declare a forwarding constructor in the derived class: class WireCount0 { protected: int m; public: WireCount0(const int& rhs) { m = rhs; } }; class WireCount1 : public WireCount0 {}; class WireCount2 : public WireCount0 { public: WireCount2(const int& rhs) : WireCount0(rhs) {} }; int dummy(int argc, char* argv[]) {

MATLAB : import package for base class

谁说我不能喝 提交于 2019-12-05 14:21:56
i have a base class A and a derived class B which are stored in the following folder structures. +myPackage (package Path) @A ( folder of class A ) A.m ( filename of class ) @B B.m Now i want to use class B which has the following head classdef B < A unfortunately this does not work because they are in different folders and i cannot import like this: import myPackage.* classdef B < A Is it possible to solve this without loosing the folder organisation? I think if you write classdef B < mypackage.A it should work. 来源: https://stackoverflow.com/questions/8475312/matlab-import-package-for-base

create instance of unknown derived class in C++

断了今生、忘了曾经 提交于 2019-12-05 11:04:52
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); } Cloning struct Base { virtual Base* clone() { return new Base(*this); } }; struct Derived : Base { virtual Base* clone() { return new Derived(*this); } }; void someFunction(Base* b) { Base* newInstance = b-

Static Instance Base/Derived class

自古美人都是妖i 提交于 2019-12-05 06:07:06
I would like to write a static instance property in a base class and derive this, but I am facing some problems. Here is the code for the base class - I currently have: public abstract class ResourceInstance<T> { private static T _instance; public static T Instance { get { if (_instance != null) return _instance; var method = MethodBase.GetCurrentMethod(); var declaringType = method.DeclaringType; if (declaringType != null) { var name = declaringType.Name; _instance = (T)Application.Current.TryFindResource(name); } return _instance; } } } As you can see its primary use is for WPF Resources

Can I pass arguments to a base constructor from a derived class's default constructor?

跟風遠走 提交于 2019-12-05 04:50:05
Suppose I have an abstract base class Deck: public abstract class Deck { public List<Card> cards; public Deck(string[] values, string[] suits) {...} ... } and a derived class EuchreDeck: public class EuchreDeck : Deck { string[] values = new string[] { "9", "10", "J", "Q", "K", "A" }; string[] suits = new string[] { "clubs", "spades", "hearts", "diamonds" }; public EuchreDeck() : base(values, suits) // Error. {} ... } I want the ability to instantiate EuchreDeck and have the two string arrays passed to the base class, i.e. var gameDeck = new EuchreDeck(); . Currently I'm getting the error: "An

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

佐手、 提交于 2019-12-05 04:16:51
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) : boost::variant<char,bool>(v) {} }; int main () { MyVariant a; MyVariant b = a; //compilation error //

Derived and base class, can I set the base explicitly?

流过昼夜 提交于 2019-12-04 22:53:26
public class SuperCar: Car { public bool SuperWheels { get {return true; } } } public class Car { public bool HasSteeringWheel { get {return true;} } } How can I set the base class for the derived Supercar? For example, I want to simply set SuperCars base class like this: public void SetCar( Car car ) { SuperCar scar = new SuperCar(); car.Base = car; } Basically, if I have Car objects, I do not want to manually iterate through every property of the car in order to setup the SuperCar oject, which I think is the only way you can do it but if you can do it the other way it would be sooo much

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

荒凉一梦 提交于 2019-12-04 16:43:59
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 (const BaseError& other) : std::exception(other), errorMsg(other.errorMsg) { ec = other.errorCode;

Calling a function from a derived template class

梦想与她 提交于 2019-12-04 15:26:38
My base class: //Element.h class Element { public: Element(); virtual ~Element(){}; // not sure if I need this virtual Element& plus(const Element&); virtual Element& minus(const Element&); }; Derived template class: //Vector.h #include "Element.h" template <class T> class Vector: public Element { T x, y, z; public: //constructors Vector(); Vector(const T& x, const T& y = 0, const T& z =0); Vector(const Vector& u); ... //operations Element& plus(const Element&) const; Element& minus(const Element&) const; ... }; ... //summation template <class T> Element& Vector<T>::plus(const Element& v)

Constructor and Destructor Inheritance

旧时模样 提交于 2019-12-04 06:01:29
I believe Constructors and Destructors in base class cannot be inherited by derived classes of the base class. Is my understanding correct. Your understanding is correct. For example, if you have class Base { Base(int i) {} }; class Derived: public Base {}; Derived d(3); This will not compile because the Base constructor is not inherited. Note that default and copy constructor are created by the compiler if possible, and call the corresponding constructor of base classes, therefore for those constructors it looks as if those were inherited. Niels I think this is what you are looking for? You