我有一个角色扮演游戏的类结构,看起来像这样......
public abstract class Item
{
public abstract string Name { get; set; }
}
public abstract class Armor : Item
{
public override string Name { get; set; }
}
public class Helmet : Armor
{
public override string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
基本上,我试图强制每个派生类型包含"名称"属性.这是最好的方法吗?我知道我可以从Item.Name中删除"abstract",然后删除Armor和Helmet中的重写"Name"属性.如果我这样做,代码看起来有点干净,但我可能忘记在这些派生类中设置base.Name.
有人可以帮我告诉我最好的方法吗?
编辑:对不起,让我再澄清一下我的问题.我想确定两件事.1)Name属性存在于所有派生类中2)Name属性不为null或为空
我基本上想强制任何派生自Item(并且不是抽象)的类具有Name的值.
请考虑以下事项
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) 我有一个基类和两个派生类,我需要将一个指向派生类对象的指针复制到另一个类中,就像示例一样.
class Base
{
public:
Base(const Base& other);
}
class Derived1 :public Base
{
public:
Derived1(const Derived& other): Base(other){...};
}
class Derived2: public Base
{
public:
Derived2(const Derived& other): Base(other){...};
}
main()
{
Derived 1 d1;
Derived2 d2(d1)
}
Run Code Online (Sandbox Code Playgroud)
我尝试从Derived 1 ti base(允许向上转换)传递,然后到*dynamic_cast*Base到Derived2并调用复制构造函数,但它不起作用.我只需要在两个派生对象之间复制两个对象的Base部分.
假设 A、B、C 派生自 AbstractBaseClass,并且它们都是同一个 java 项目的一部分,是以下形式的包结构...
package com.whatever.###
AbstractBaseClass.java
package com.whatever.###.###
A.java
B.java
C.java
Run Code Online (Sandbox Code Playgroud)
...通常优于以下形式的封装结构...
package com.whatever.###.###
AbstractBaseClass.java
A.java
B.java
C.java
Run Code Online (Sandbox Code Playgroud)
?
...或者很少有人会关心?
我正在尝试为我正在编写的程序实现良好的设计模式。我有一个这样的班级结构。
abstract class SomeBase
{
public SomeObject obj { get; protected set; }
protected SomeBase(SomeObject x)
{
obj = x;
}
//Other methods and fields...
}
public class SomeDerived : SomeBase
{
public SomeDerived() : base(new SomeObject(this))
{
}
}
Run Code Online (Sandbox Code Playgroud)
现在我确定您知道,您不能在基本构造函数中传递 this,因为此时对象尚未初始化。无论如何,我真的希望有一个解决方法。允许SomeDerived()处理基类字段的设置对我来说不是最佳实践。我想将这个新对象向上传递。
派生类的构造函数返回基类的实例.
以下代码解释了我的问题:
// 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(new TestA() instanceof TestA); // returns true, all is good
Run Code Online (Sandbox Code Playgroud)
这怎么可能?
javascript inheritance constructor derived-class ecmascript-6
[class.access/1] 中的 C++ 标准规定(强调我的):
一个类的成员可以是
- 私人的; 也就是说,它的名字只能被声明它的类的成员和朋友使用。
- 受保护;也就是说,它的名称只能由声明它的类的成员和朋友、从该类派生的类及其朋友使用(参见 [class.protected])。
- 民众; 也就是说,它的名字可以在任何地方使用而不受访问限制。
那么为什么编译器会在下面的 C++ 程序中引发这个错误呢?
#include <iostream>
class B {
protected:
static int const i = 1;
};
class D: public B {
public:
void f();
friend void g();
};
void D::f() {
B b;
std::cout << b.i; // OK
}
void g() {
B b;
std::cout << b.i; // error: 'i' is a protected member of 'B'
}
int main() {
D d;
d.f();
g();
return 0;
} …Run Code Online (Sandbox Code Playgroud) 以下是两个案例.
案例1)Base-> BaseIndirect-> DerivedIndirect
案例2)Base-> Derived
在案例2)中,我能够使用3种符号调用Base类的模板函数.在案例1)中,我只能使用其中一个符号来调用Base类的模板函数.并且,我无法使用任何符号调用BaseIndirect的模板函数:(.如何解决此问题?谢谢.
struct Base {
template<bool R> inline void fbase(int k) {};
};
template<class ZZ> struct BaseIndirect : Base {
template<bool R> inline void fbaseIndirect(int k) {};
};
template<class ZZ>
struct DerivedIndirect : BaseIndirect<ZZ> {
DerivedIndirect() {
this->fbase<true>(5); // gives error, line 13
fbase<true>(5); // gives error, line 14
Base::fbase<true>(5); // WORKS, line 15
this->fbaseIndirect<true>(5); // gives error, line 16
fbaseIndirect<true>(5); // gives error, line 17
BaseIndirect<ZZ>::fbaseIndirect<true>(5); // gives error, line 18
}
};
template<class …Run Code Online (Sandbox Code Playgroud) 我已经尝试了几个小时来编写一个派生自的类boost::variant.但我不明白是什么问题(我不明白编译错误意味着什么).
实现干净boost::variant派生类的规则是什么?
#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
// MyVariant c = MyVariant();
// MyVariant d (true);
// MyVariant e ('E');
}
Run Code Online (Sandbox Code Playgroud)
为什么我要使用继承?(编辑给@zaufi更多细节)
const char*为stringint为longenum …正如在C++ 编程语言中解释的那样:
virtual void push(char c) = 0;
virtual void pop() = 0;
Run Code Online (Sandbox Code Playgroud)
这个词virtual的意思是“以后可能会在这一个派生类中重新定义”
的=0语法说,一些类来源于栈必须定义的功能。
那么为什么=0需要符号呢?是不是说派生类一定要定义这个函数,也就是说没有的时候=0,一些派生类就没有强制定义这个方法?
我对此感到困惑,需要一些帮助。
derived-class ×10
c++ ×6
base-class ×2
c# ×2
constructor ×2
inheritance ×2
boost ×1
destructor ×1
ecmascript-6 ×1
friend ×1
gcc ×1
java ×1
javascript ×1
package ×1
protected ×1
pure-virtual ×1
templates ×1
virtual ×1