我有一个基类DockedToolWindow:Form,以及从DockedToolWindow派生的许多类.我有一个容器类,用于保存和分配事件到DockedToolWindow对象,但是我想调用子类中的事件.
我实际上有一个关于如何实现这个MSDN网站告诉我要做的事情的问题.以下这节给我的问题是:
// The event. Note that by using the generic EventHandler<T> event type
// we do not need to declare a separate delegate type.
public event EventHandler<ShapeEventArgs> ShapeChanged;
public abstract void Draw();
//The event-invoking method that derived classes can override.
protected virtual void OnShapeChanged(ShapeEventArgs e)
{
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event …Run Code Online (Sandbox Code Playgroud) 在C struct中,我保证:
struct Foo { ... };
struct Bar {
Foo foo;
...
}
Bar bar;
assert(&bar == &(bar.foo));
Run Code Online (Sandbox Code Playgroud)
现在,在C++中,如果我有:
class Foo { ... };
class Bar: public Foo, public Other crap ... {
...
}
Bar bar;
assert(&bar == (Foo*) (&bar)); // is this guaranteed?
Run Code Online (Sandbox Code Playgroud)
如果是这样,你能给我一个参考(如"The C++ Programming Language,page xyz")吗?
谢谢!
请考虑以下事项
class base{
base();
~base();
}:
class derived : public base{
};
Run Code Online (Sandbox Code Playgroud)
当派生对象被破坏并且派生类没有定义析构函数时,是否会自动调用基类析构函数?
否则,如果我在派生类中也有析构函数,我是否还需要显式调用基类析构函数?
class base{
base();
~base();
}:
class derived : public base{
derived();
~derived
base::~base(); //do I need this?
}
};
Run Code Online (Sandbox Code Playgroud) 我创建了一个javascript类TkpSlider,受益于这个w3schools页面.(JSFiddle)
var TkpSlider = function (args) {
args= args|| {};
};
var mainSwiper = new TkpSlider();
Run Code Online (Sandbox Code Playgroud)
我已经扩展了这个以添加一些从该页面启发的滑动功能,以便我可以在用户滑动时使用滑块.(JSFiddle)
var TkpSwiper = function (args) {
TkpSlider.call(this, args);
};
TkpSwiper.prototype = Object.create(TkpSlider.prototype);
var mainSwiper = new TkpSwiper();
Run Code Online (Sandbox Code Playgroud)
我将代码分开,所以我不会在以后从基本代码和任何其他函数中感到困惑.但是对于OOP,我必须通过为它创建一个新名称来扩展TkpSwiper,但我想找到另一种使用相同名称TkpSlider的方法.
我看到了这篇文章,并试图在这个JSFiddle(下面的代码段)中使用prototype来扩展TkpSlider.问题是我无法从子类访问基类的公共方法.我测试过的博客中的示例javascript,我必须遗漏我的代码.任何人都可以有所了解吗?谢谢.
var TkpSlider = function (args) {
args= args|| {};
//private variable
var btnPrev = args.btnPrev || "tkp-slide-btn-prev";//class for previous button (default: "tkp-slide-btn-prev")
var btnNext …Run Code Online (Sandbox Code Playgroud)使用g ++ 4.2.1编译此代码:
struct S { };
template<typename T> struct ST { };
template<typename BaseType>
class ref_count : private BaseType { };
template<typename RefCountType>
class rep_base : public RefCountType { };
class wrap_rep : public rep_base<ref_count<S> > {
typedef rep_base<ref_count<S> > base_type; // line 11
};
Run Code Online (Sandbox Code Playgroud)
我明白了:
bug.cpp:1: error: ‘struct S’ is inaccessible
bug.cpp:11: error: within this context
Run Code Online (Sandbox Code Playgroud)
但是,如果我更改wrap_rep要使用的类ST:
class wrap_rep : public rep_base<ref_count< ST<int> > > {
typedef rep_base<ref_count< ST<int> > > base_type;
};
Run Code Online (Sandbox Code Playgroud)
它编译得很好.或者,如果我将原始代码更改为: …
$ 4.11/2州 -
类型为" cv
B类型成员的指针"的rvalue ,其中是类类型,可以转换为类型为"指向cv类型 成员的指针"的rvalue ,其中是派生类(第10节).如果不可访问(第11条),模糊(10.2)或虚拟(10.1)基类 ,则需要进行此转换的程序是不正确的.TBDTDBBD
我的问题是为什么我们有限制B不是虚拟基类D?
我有一个DbContext,设置了不同的DbSet<T>s,所有类型派生自同一个基类:
public class Foo : Entity { }
public class Bar : Entity { }
MyDbContext : DbContext
{
public DbSet<Foo> Foos { get; set; }
public DbSet<Bar> Bars { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
是否可以Entity在一个查询中获取具有基类的所有实体,例如:
DbContext.Set<Entity>(); // doesn't work
Run Code Online (Sandbox Code Playgroud)
我试图向DbSet<Entity>DbContext 引入一个显式,但这会为数据库中的所有实体生成一个大表.
附加问题:如果这种方式有效,那么查询接口呢?
编辑:
我按照Ladislav Mrnka 链接的说明进行操作,并按照以下方式完成了我的映射:
MyDbContext : DbContext
{
public DbSet<Entity> Entities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Foo
modelBuilder.Entity<Foo>().Map(x =>
{
x.MapInheritedProperties();
x.ToTable("Foo");
})
.HasMany(x => …Run Code Online (Sandbox Code Playgroud) mapping inheritance base-class ef-code-first entity-framework-4.1
我正在读约瑟夫·阿尔巴巴里和本·阿尔巴巴里的书"简而言之的C#4.0".从那里我发现访问修饰符的主题限制.第91页,主题"访问修饰符的限制".
从书中引用.
编译器会阻止任何不一致的访问修饰符的使用.例如,子类本身可以比基类更难访问,但不能更多
所以这说明基类应该与子类相同或更易于访问.因此,如果基类是内部的,那么子类应该是私有的或内部的.如果基类是私有的,而子类是公共的,那么将生成编译时错误.在Visual Studio中尝试这个时,我发现了一些奇怪的行为.
尝试1:Base是私有的,子类是私有的(Works,正确的行为)如果两者都是内部的,那么它也有效.
private class A { }
private class B : A { } // Works
Run Code Online (Sandbox Code Playgroud)
尝试2:Base是私有的,子类是public或internal(这是失败的,正确的行为)
private class A { }
public class B : A { } // Error
Run Code Online (Sandbox Code Playgroud)
尝试3:Base是内部的,sub是公共的(这是有效的,但它应该失败.因为Base比子类更难访问
internal class A { }
public class B : A { } // Works, but why
Run Code Online (Sandbox Code Playgroud)
现在我的问题是为什么试试3没有失败?子类是公共的,比内部的基类更容易访问.即使这本书说这应该失败.但Visual Studio成功编译了这个.这应该工作与否?
编辑:
我在VS中创建了一个新的控制台项目.在Program.cs中,我添加了我的代码.这是Program.cs文件的完整代码.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication
{
class Program
{
internal class A { }
public class B …Run Code Online (Sandbox Code Playgroud) 让我们看看以下代码:
class A{
protected:
int _val;
public:
A(){printf("calling A empty constructor\n");}
A(int val):_val(val){printf("calling A constructor (%d)\n", val);}
};
class B: virtual public A{
public:
B(){printf("calling B empty constructor\n");}
B(int val):A(val){printf("calling B constructor (%d)\n", val);}
};
class C: public B{
public:
C(){printf("calling C empty constructor\n");}
C(int val):B(val){printf("calling C constructor (%d)\n", val);}
};
int main(void) {
C test(2);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
calling A empty constructor
calling B constructor (2)
calling C constructor (2)
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么没有任何参数调用A类构造函数?如果我希望B类从A虚拟继承,我还能如何"修复"这种行为?(如果继承不是虚拟的 - 样本工作正常)
我在理论上认为这个问题的答案是肯定的.
但是,在实践中,我的编译器(VS2010)似乎并没有在以下情况下抱怨:我有一个抽象基类提供了一些通用接口(但没有数据成员)以及从中派生的各种子类和子类.
class Base
{
public:
Base() {}
virtual ~Base() {}
virtual void interfaceFunction1() = 0;
virtual void interfaceFunction2() = 0;
private:
Base(const Base&); // all derived classes should be uncopyable
Base& operator=(const Base&);
// no data members
};
Run Code Online (Sandbox Code Playgroud)
我的编译器发现甚至在子子类或子子类中实现完整拷贝构造函数也没有问题.
如何确保从Base派生的每个类都是不可复制的?
编辑:如果我理解得很好,这正是Scott Meyers在Effective C++(第3版,2005)第6项中解释的他对类的想法Uncopyable(仅扩展到完整的接口类).是什么让他的想法发挥作用?(我知道他私下继承,但这不应该造成问题)
base-class ×10
c++ ×6
inheritance ×4
c# ×2
private ×2
c++11 ×1
class ×1
destructor ×1
events ×1
extending ×1
javascript ×1
mapping ×1
name-lookup ×1
oop ×1
prototype ×1
virtual ×1