我有一个基类和几代后代(派生类):
class Base{
public:
virtual void myFunc() = 0;
}
class Derived : public Base{
//This class needs to implement myFunc()
}
class Derived2 : public Derived{
//How to force this class to implement its version of myFunc(), and force it to call Derived::myFunc a
我有以下继承结构:
template<unsigned int t>
class Base {
protected:
int a;
};
template<unsigned int t>
class Derived1 : public Base {
};
template<unsigned int t>
class Derived2 : public Base {
};
我想要的是让一个Derived1构造函数接受一个Derived2对象作为参数,然后访问基类中的受保护成员'a‘。
我在基类中添加了下面这行代码:
template&l
最后我有了奇怪的要求。我有来自一个基类的多个派生类,如下所示。
class base
{
}
class derived1 : base
{
}
class derived2 : base
{
}
.
.
.
.
class derivedN : base
{
}
void someFunction(base bObj)
{
//get me derived class object with which bObj was created.
}
现在,在代码的某个地方,我得到了基类对象(这是函数的参数)。此函数能否提取创建此基类对象的精确派生类对象?
这可能没有道理,但我有强烈的感觉,一定
这在一定程度上与有关。
我有两个类,它们都是模板化的,例如:
class Base
{
public:
template< class T > void operator=(T other)
{
//irrelevant
}
Derived toDerived()
{
Derived d;
//do stuff;
return d;
}
};
class Derived: public Base
{
public:
template< class T &
假设您有以下三个类(我省略了实现):
abstract class BaseClass
class Derived1 : BaseClass
class Derived2 : BaseClass
现在,在BaseClass中,我想定义一个方法,它应该返回一个新的、稍微修改过的同一个实例的版本。但问题是:我不能构造BaseClass,因为它是抽象的,而且我不知道您使用的是哪个派生类,因为我们使用的是BaseClass。
我能想到的唯一解决办法就是让它成为一个抽象的方法,并使每个派生类都实现它自己的版本。但这将是大量重复的代码,只需更改一个类名即可。有什么更好的方法吗?
我不明白为什么这段代码可以工作。派生类继承自使用派生类类型的基类。我曾期望在类型创建中会出现一些无限循环。
有人能解释一下为什么这段代码能工作吗?谢谢。
using System;
using System.Collections.Generic;
using static System.Console;
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
WriteLine(new Deri
class Base {
public:
template<typename T>
static Base* construct() {
return new Derived<T>();
}
};
template<typename T>
class Derived : public Base {
public:
Derived() {}
};
该代码会生成一个编译错误(VS Studio 2017):
syntax error: unexpected token 'identifier',
我试图通过基类的另一个函数发送一个指向基类函数的派生指针,但由于某种原因,它会抱怨:错误:在第8行中无效使用不完整类型的“struct派生”。
#include <iostream>
using namespace std;
class Derived;
class Base
{
public:
void getsomething(Derived *derived){derived->saysomething();} //This is line 8
void recieveit(Derived *derived){getsomething(&*de
在中,我们编写
template <class Derived>
class Base {
};
class Derived : public Base<Derived> {
};
怎样才能使代码具有鲁棒性,从而使代码再次复制粘贴遗漏,从而使以下代码段引发编译时错误:
class AnotherDerived : public Base<Derived> {
};
我正在使用VisualC++ 2013。
我正在尝试获取类链中最底层类(G)的类型,如下所示。
1.public class G: F
2.public class F: E<STate>
3.public abstract class E<TState> : D<TState, StatusQueryParams> where TState : E, new()
4.public abstract class D<TState, TQuery> :C<TState, TQuery>
5.public abstract class C<TState, TQuery>
我可以获得基类的名称如下:
>>> class Base(object):
... pass
...
>>> class Derived(Base):
... def print_base(self):
... for base in self.__class__.__bases__:
... print base.__name__
...
>>> foo = Derived()
>>> foo.print_base()
Base
在本例中,如何进行反向操作,并从De
我有如下的类结构:
class Common {
//members and data here.
};
class Derived1 : public Common
{
};
class Derived2: public Common, public Derived1
{
};
根据我对这个层次结构的理解,Derived1和Derived2都将共享Common中的任何成员。有没有办法在Derived2中不让Derived1成为私有的,这样Derived2仍然可以继承Common,但有单独的函数覆盖。基本上,我需要在Derived1和Derived2中覆盖一个虚函数,但仍然要让Deriv
我有以下对象
#include <stdio.h>
class foo_t{ };
class bar_t: public foo_t{ };
class zoo_t: public bar_t{ };
class base_t{
public:
void dostuff(foo_t * foo){ printf("Defaulting to base\n"); };
};
class derived_t: public base_t{
public:
void dostuff(bar_t * bar){ printf("Overloadin
在继承类的shared_ptr之间自动类型转换方面,我遇到了一些问题。
我的类结构如下:一个基类Base和两个派生类Derived1和Derived2。
// Base class
class Base {
protected:
...
...
public:
Base() = default;
virtual ~Base() = default;
virtual void run() = 0;
...
...
};
// Derived class
class Derived1: Base {
protected:
...
...
public:
D
假设我们有这两个类:
public class Derived : Base
{
public Derived(string s)
: base(s)
{ }
}
public class Base
{
protected Base(string s)
{
}
}
如何从Base的构造函数中确定Derived是调用者?这是我想出来的:
public class Derived : Base
{
public Derived(string s)
: base(typeof(Derived), s)
{ }
我有两个模板类Derived1和Derived2,它们都是从Base派生的。
我希望foo可以通过模板实例化返回Derived1或Derived2,并且它们的类型可以通过参数use_int来区分。
class Base {
public:
virtual ~Base() = default;
};
template <typename T>
class Derived1 : public Base {};
template <typename T>
class Derived2 : public Base {};
template <typename
我有一个基类,它是一个模板,下面有两个派生类,
template <class parm>
class Base{
};
class Derived1: public Base<Derived1>
{};
class Derived2: public Base<Derived2>
{};
现在,我有另一个类保存对Derived1对象或Derived2对象的引用,这取决于传递给构造函数的是哪个,但是我不知道语法,它在C++中可以实现吗?
struct Observer{
// I want to hold a reference to Derived
我试图理解遗传和多态性。当我运行这个:
#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual Base* fun() = 0;
int getX() { return x; }
};
// This class inherits from Base and implements fun()
class Derived: public Base
{
public:
int y;
Derived* fu
以下Python3.5代码:
class Base(object):
def __init__(self):
print("My type is", type(self))
class Derived(Base):
def __init__(self):
super().__init__()
print("My type is", type(self))
d = Derived()
指纹:
My type is <class '__main__.Derived'>
M
我遇到了一些奇怪的继承和蒙蒂的东西。
我有以下几点:
`
@Document
public class Base {
public String fieldA;
}
public class Derived extends Base {
public String fieldB;
}
public interface DerivedRepository extends MongoRepository<Base, String> {
List<Derived> findByFieldA(String fieldA);
}
`
当插入的时候
在集合
我有几个例外,如下所示
class ExceptionType1Base : public std::runtime_error {
};
class ExceptionType1Derived1 : public ExceptionType1Base {
};
class ExceptionType1Derived2 : public ExceptionType1Base {
};
和另一种异常类型
class ExceptionType2Base : public std::runtime_error {
public:
int type;
};
class Ex
我有一些代码,比如:
class Base {
virtual bool acceptsData(char*) = 0;
};
class Derived1 : public Base {
virtual bool acceptsData(char*) { /* do something */ }
};
class Derived2 : public Base {
virtual bool acceptsData(char*) { /* do something else */}
}
Base* createStuff(char* data)
{
Base
我有这个问题。有一个由我们开发和使用的框架。我们有一些这个框架的用户需要实现的接口。我希望确保这些接口中的两个永远不能在同一个类上实现。下面是一个例子 class A{
void a() = 0;
};
class B{
void b() = 0;
};
class C: public A, public B { // This should give error
};
class D: public A{ // OK
}; 到目前为止,我想也许我可以使用static_assert和std::is_convertible,但我不知道怎么做。 编辑:我不知道谁来编写派生类,所以如果可
请参阅以下示例代码:
#include <iostream>
using namespace std;
class base
{
public:
base()
{
cout << "ctor in base class\n";
}
};
class derived1 : public base
{
public:
derived1()
{
cout <<"ctor in derived1 class\n";
给定这些Java类定义:
class Base {
int value;
public <T extends Base> T self() {
return (T) this;
}
}
class Derived extends Base {}
这段Java代码可以很好地编译:
new Derived().self();
但是这段Scala代码无法运行:
(new Derived).self(); // runtime error
运行时间错误:
java.lang.ClassCastException: Derived cannot
请考虑下面的代码示例:
class Base
{
public:
Base() {}
};
class Derived1 : public Base
{
public:
Derived1() : Base() {}
};
class Derived2 : public Base
{
public:
Derived2() : Base() {}
};
有什么东西可以让Derived1拥有-- Derived2和Derived2 -- Derived1?
最好的解决方案是使用具有这两个对象的第三个类。但万一需要高性能呢?
一个例子可能是双向客户端服务器应用
class base
{
public:
virtual void display() = 0;
};
class derived : virtual public base
{
public:
void display()
{
cout << "Display of derived : " << std::endl;
}
};
class derived1 : virtual public base
{
public:
void display()
{
co
我目前正试图在我的项目中实现大量继承的类。因此,我使用成员初始化器-列表和“管道”,一个对变量的引用,一直到基类。我真的不确定,为什么我会得到一个编译器错误。
我已经尝试将引用"int &id“更改为指针"int* id”。下面的例子只是一个很小的例子,它指出了我的问题:
class Base
{
public:
int& m_id;
Base(int &id)
: m_id(id)
{
}
};
class Derived1: virtual public Base
{
public:
Der
我想写一个Scala测试来检查使用工厂方法创建的对象的确切类。
class Base { }
class Derived extends Base { }
class TestSpec {
test("test instance class") {
val result = new Derived()
// I want to check that result is exactly Derived type
result should be a[Derived]
result should not be a[Base]
}
}