derived

'base' values may only be used to make direct calls to the base implementations of overridden members

孤街醉人 提交于 2019-12-10 20:43:11
问题 Why can't I call the base implementation of f here: type Base = abstract f : int -> int -> int default this.f (x : int) (y : int) : int = x + y type Derived = inherit Base override this.f (x : int) (y : int) : int = base.f -x -y The call to base.f elicits this compiler error: error FS0419: 'base' values may only be used to make direct calls to the base implementations of overridden members If I change f to take a single argument then it compiles. Presumably this is something to do with

derived class as default argument g++

半腔热情 提交于 2019-12-10 13:24:03
问题 Please take a look at this code: template<class T> class A { class base { }; class derived : public A<T>::base { }; public: int f(typename A<T>::base& arg = typename A<T>::derived()) { return 0; } }; int main() { A<int> a; a.f(); return 0; } Compiling generates the following error message in g++: test.cpp: In function 'int main()': test.cpp:25: error: default argument for parameter of type 'A<int>::base&' has type 'A<int>::derived' The basic idea (using derived class as default value for base

access base class variable in derived class

房东的猫 提交于 2019-12-10 07:51:50
问题 class Program { static void Main(string[] args) { baseClass obj = new baseClass(); obj.intF = 5; obj.intS = 4; child obj1 = new child(); Console.WriteLine(Convert.ToString(obj.addNo())); Console.WriteLine(Convert.ToString(obj1.add())); Console.ReadLine(); } } public class baseClass { public int intF = 0, intS = 0; public int addNo() { int intReturn = 0; intReturn = intF + intS; return intReturn; } } class child : baseClass { public int add() { int intReturn = 0; intReturn = base.intF * base

Base class on the initialisation list of a derived class' copy constructor (C++)

大憨熊 提交于 2019-12-08 04:34:33
问题 Let the example be: class Base { Base (const Base & copyFrom) { globalRegister (* this); } } class Derived { Derived (const Derived & copyFrom) : Base (copyFrom) {} } I've read suggestions to include the Base's copy constructor on the initialisation list of Derived in order to copy over the Base's properties (as in the example). However, I have the Base's copy constructor passing itself (* this) to other object (to be registered with that object). Would that be a case where I actually must

C++ 虚析构函数 纯虚析构函数 虚构造函数

南楼画角 提交于 2019-12-07 07:17:28
众所周知,在实现多态的过程中,一般将基类的析构函数设为virtual,以便在delete的时候能够多态的链式调用。那么析构函数是否可以设为纯虚呢? class CBase { public : CBase() { printf("CBase()\n"); } virtual ~CBase() = 0; }; 答案是可以,那么这样实现的目的是什么呢?当然是避免实例化。 但因为派生类不可能来实现基类的析构函数,所以 基类析构函数虽然可以标为纯虚,但是仍必须实现析构函数 ,否则派生类无法继承,也无法编译通过。 下面详细讨论: 一. 虚析构函数 我们知道,为了能够正确的调用对象的析构函数,一般要求具有层次结构的顶级类定义其析构函数为虚函数。因为在delete一个抽象类指针时候,必须要通过虚函数找到真正的析构函数。 如: [cpp] view plain copy print ? class Base { public : Base(){} virtual ~Base(){} }; class Derived: public Base { public : Derived(){}; ~Derived(){}; } void foo() { Base *pb; pb = new Derived; delete pb; } 这是正确的用法,会发生动态绑定,它会先调用Derived的析构函数

C++虚析构函数及纯虚析构函数

血红的双手。 提交于 2019-12-07 07:02:02
C++中析构函数可以为纯虚吗? 众所周知,在实现多态的过程中,一般将基类的析构函数设为virtual,以便在delete的时候能够多态的链式调用。那么析构函数是否可以设为纯虚呢? class CBase { public: CBase() { printf("CBase()\n"); } virtual ~CBase() = 0; }; 答案是可以,那么这样实现的目的是什么呢?当然是避免实例化。 但因为派生类不可能来实现基类的析构函数,所以基类析构函数虽然可以标为纯虚,但是仍必须实现析构函数,否则派生类无法继承,也无法编译通过。 下面详细讨论: 一. 虚析构函数 我们知道,为了能够正确的调用对象的析构函数,一般要求具有层次结构的顶级类定义其析构函数为虚函数。因为在delete一个抽象类指针时候,必须要通过虚函数找到真正的析构函数。 如: class Base { public: Base(){} virtual ~Base(){} }; class Derived: public Base { public: Derived(){}; ~Derived(){}; } void foo() { Base *pb; pb = new Derived; delete pb; } 这是正确的用法,会发生动态绑定,它会先调用Derived的析构函数,然后是Base的析构函数。

C++中的虚析构函数、纯虚析构函数详解

主宰稳场 提交于 2019-12-07 06:57:41
C++中析构函数可以为纯虚函数吗? 众所周知,在实现多态的过程中,一般将基类的析构函数设为 virtual ,以便在 delete 的时候能够 多态的链式调用 。那么析构函数是否可以设为纯虚呢? class CBase { public : CBase () { printf( "CBase()\n" ); } virtual ~CBase() = 0 ; // 析构函数是纯虚函数 }; 答案是可以,那么这样实现的目的是什么呢?当然是 避免实例化 。 但因为 派生类不可能来实现基类的析构函数 ,所以 基类析构函数虽然可以标为纯虚,但是仍必须实现析构函数,否则派生类无法继承,也无法编译通过 。 下面详细讨论: 虚析构函数 我们知道,为了能够正确的调用对象的析构函数,一般要求具有层次结构的 顶级类 定义其析构函数为虚函数。因为在 delete 一个抽象类指针时候,必须要通过虚函数找到真正的析构函数。 如: class Base { public : Base (){} virtual ~Base(){} }; class Derived: public Base { public : Derived (){}; ~Derived(){}; } void foo() { Base *pb; pb = new Derived; delete pb; } 这是正确的用法,会发生 动态绑定

C++中虚析构函数和纯虚函数的作用

别来无恙 提交于 2019-12-07 06:56:53
一. 虚析构函数 为了能够正确的调用对象的析构函数,一般要求具有层次结构的顶级类定义其析构函数为虚函数。因为在delete一个抽象类指针时候,必须要通过虚函数找到真正的析构函数。 class Base { public : Base (){} virtual ~Base(){} }; class Derived: public Base { public : Derived (){}; ~Derived(){}; } void foo() { Base *pb; pb = new Derived; delete pb; } 这是正确的用法,会发生动态绑定,它会先调用Derived的析构函数,然后是Base的析构函数。 如果析构函数不加virtual,delete pb只会执行Base的析构函数,而不是真正的Derived析构函数。 因为不是virtual函数,所以调用的函数依赖于指向静态类型,即Base。 二. 纯虚析构函数 现在的问题是,我们想把Base做出抽象类,不能直接构造对象,需要在其中定义一个纯虚函数。如果其中没有其他合适的函数,可以把析构函数定义为纯虚的,即将前面的CObject定义改成: class Base { public : Base (){} virtual ~Base()= 0 }; 可是,这段代码不能通过编译,通常是link错误,不能找到~Base(

Base class vs Utility class

*爱你&永不变心* 提交于 2019-12-07 01:31:05
问题 Which of the two should be preferred? There are some methods which are called by class A, B and C. Should those methods be encapsulated in a class D (base of A, B and C) ? OR Should those methods be encapsulated in a class U and other classes creats it's object to use the methods as required. On what basis decision should be taken? Thanks. 回答1: You should make a static utility class. Only use inheritance if it's actually meaningful—if A , B , and C actually are a D . 回答2: I'd base the

Prevent calling base class implemented interface method in the derived class C#

大兔子大兔子 提交于 2019-12-06 10:32:53
Is it possible to implement an interface in a base class and allow calling/overriding the implemented method in the first derived class level but prevent calling it from any further derived classes? public interface IInterfaceSample { bool Test(); } public class Base: IInterfaceSample { public virtual bool Test() { return True; } } public class Sub1: Base { //I need to be able to override the Test method here public override bool Test() { return True; } } //Under a separate project: public class Sub2: Sub1 { //I need to prevent overriding the interface implementation in this class } Now what i