derived-class

Design alternative for access to derived class member from base class pointer

拜拜、爱过 提交于 2019-12-25 07:47:45
问题 I'm writing a DAL/ORM library. This library will be accessed mainly from GUIs but also from some "business level" applications. I'm still in the design phase of this library and came to a point where I'm not sure how to solve the following issue nicely. In my current design I have a class, let's call it List for the moment, that has a container of another class, Properties . Properties come in two flavors (A and B), with mostly the same functionality, but some of their functionality is

Add a Property to Entity Class in ViewModel

被刻印的时光 ゝ 提交于 2019-12-25 05:30:42
问题 I have a profile with a EntityCollection of ProfileUser. In this Class I have a Profile_ID ald a Profile relation and a User_ID but no USer relation, because User is in another Database. In a Datagrid I want to access via User.Username I tried this, but ofc it doesnt work... public EntityCollection<ProfileUser> ProfileUsers { get { if (profile != null) return profile.ProfileUser; else return null; } set { profile.ProfileUser = value; } } and here my custom Class public class

How to access protected base class function, from derived class through base class ptr

故事扮演 提交于 2019-12-24 12:14:43
问题 I have abstract class A, from which I inherit a number of classes. In the derived classes I am trying to access protected function in A trough A pointer. But I get a compiler error. class A { protected: virtual void f()=0; }; class D : public A { public: D(A* aa) :mAPtr(aa){} void g(); protected: virtual void f(); private: A* mAPtr; // ptr shows to some derived class instance }; void D::f(){ } void D::g() { mAPtr->f(); } The compiler error says : cannot access protected member A::f declared

Is it undefined behavior to cast from base class to derived?

别来无恙 提交于 2019-12-24 09:43:12
问题 I've encountered myself in a problem where casting to the derived class would solve the problem. I've found an answer on S.O that says it can lead to UB, testing it, it both compiled and worked correctly. Is it undefined behavior? If it is what would be a correct approach to this problem? class A { public: A(){}; ~A(){} }; class B : public A { public: B(){}; ~B(){} void Show() { std::cout << "Show" << std::endl; } }; int _tmain(int argc, _TCHAR* argv[]) { A a; B* b = static_cast<B*>(&a); b-

Undefined reference to member function of template base class

烂漫一生 提交于 2019-12-24 00:52:59
问题 Consider following code: myclass.h : template <class T> struct S { void f(); }; struct MyClass : public S<int> { void g(); }; myclass.cpp : #include "myclass.h" #include <iostream> template <class T> void S<T>::f() { std::cout << "f()\n"; /* some code here that could not be in header */ } void MyClass::g() { std::cout << "g()\n"; } main.cpp : #include "myclass.h" int main() { MyClass m; m.g(); m.f(); // problem here } I've got linker error: undefined reference to `S::f()' Can I solve this

Abstract base class for derived classes with functions that have a return type of the derived class

馋奶兔 提交于 2019-12-23 19:22:39
问题 I would like to force a certain API for all classes derived from the base class. Normally, you do that using an abstract base class that has purely virtual functions. However, how do you handle functions that return the derived type? How do I go about forcing that type of function? struct base { virtual base func() = 0; }; struct deriv1 : base { deriv1 func(); }; struct deriv2 : base { deriv2 func(); }; This example will give an error like "invalid abstract return type for member function". I

Why should the derived class constructor always access base class constructor?

喜你入骨 提交于 2019-12-23 15:53:39
问题 I saw this question in one of my question papers: Why should the derived class constructor always access base class constructor? I'm wondering whether the question is valid? 回答1: So that you may have a valid object of type "Base" before you start messing with the inherited functionality in your derived object! 回答2: It's not valid. There's no 'should' about it: it must , and the compiler enforces it, by calling the base class's default constructor if it exists, and giving you a compile error

How to declare an “implicit conversion” in a variadic template?

本小妞迷上赌 提交于 2019-12-23 05:11:30
问题 My aim is to send a data to several streams. It is possible by using boost::tee. But I want to write a wrapper with variadic template for using several streams. The problem is that I need an implicit convertation from descendant struct to ancestor struct. Or something like that. #include <boost/iostreams/tee.hpp> #include <boost/iostreams/stream.hpp> #include <fstream> #include <iostream> using namespace std; namespace bio = boost::iostreams; using bio::tee_device; using bio::stream; template

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

北慕城南 提交于 2019-12-22 07:55:59
问题 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

Static Instance Base/Derived class

五迷三道 提交于 2019-12-22 05:49:18
问题 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