将虚拟 class 引用作为参数传递给构造函数
Passing a virtual class reference as an argument to constructor
我在 .h
文件中定义了一个摘要 class Algorithm1
。我希望这个抽象 class、Algorithm1
保存对其他一些抽象 class 的引用,并能够在运行时决定确切的引用。所以我的 Algorithm1
持有这些引用,我希望能够通过构造函数传递确切的实现。 Algorithm1
的一个确切实现是 class Algorithm1A
所以这是它的样子。
Algorithm1.h
#include "Interval.h"
#include "OrderedAlphabet.hpp"
#include "Rank.h"
//! Algorithm1 interface
class Algorithm1 {
protected:
Rank &rank;
OrderedAlphabet &alphabet;
std::map<symbol_type, occurence_type> &C;
public:
Algorithm1(Rank &r,
OrderedAlphabet &a,
std::map<symbol_type, occurence_type> &c):
rank(r), alphabet(a), C(c) {
}
};
#endif /* Algorithm1_h */
算法1A
#include "Algorithm1.h"
class Algorithm1A : Algorithm1 {
std::string uniqueCharsInInterval(int i, int j) {
rank.rank() // dome something with ABSTRACT rank
}
std::vector<Interval> getIntervals(int i, int j) {
alphabet.getSize() // do something with alphabet - not abstract
C.find('x') // do something with C - not abstract
}
};
除此之外,我的Rank
class也是一个抽象class,但是是由classWTRank
、NTRank
实现的等等。所以我真正想要的是能够传递 WTRank
对象,但作为对 Algorithm1
.
构造函数的 Rank
引用
Rank.h
#ifndef Rank_h
#define Rank_h
#include "DataTypes.h"
//! Rank interface
class Rank {
public:
virtual unsigned long long rank(int index, symbol_type symbol)=0;
};
#endif /* Rank_h */
而我的WTRank
class分为.h
和.cpp
两个文件。
WTRank.h
#ifndef WTRank_h
#define WTRank_h
#include "Rank.h"
#include <sdsl/wavelet_trees.hpp>
using namespace sdsl;
//! Rank based on Wavelet Tree
class WTRank: public Rank {
private:
wt_huff<> wt;
public:
WTRank(wt_huff<> w_tree) {
this->wt = w_tree;
}
~WTRank();
unsigned long long rank(int index, symbol_type symbol);
};
#endif /* WTRank_h */
WTRank.cpp
#include "WTRank.h"
unsigned long long WTRank::rank(int index, symbol_type symbol) {
return wt.rank(index, symbol);
}
现在,如果我想创建一个实现 Algorithm1
抽象 class 的 Algorithm1A
对象,并且我想为其构造函数提供所需的参数,我正在做这样的事情 - 并且得到 "No matching constructor error."
wt_huff<> wt;
construct_im(wt, str, 1);
OrderedAlphabet alphabet(10);
std::map<symbol_type, occurence_type> C = calculateC(wt, alphabet);
WTRank wtrank = *new WTRank(wt);
Algorithm1A alg = new Algorithm1A(wtrank, &alphabet, &C); // ERROR!!
如何才能让它发挥作用?
构造函数不被继承。您需要在 Algorithm1A
上定义一个构造函数,它接受适当的参数。
Algorithm1A::Algorithm1A(Rank &r,
OrderedAlphabet &a,
std::map<symbol_type, occurence_type> &c):
Algorithm1(r, a, c)
{}
在 C++ 中默认情况下不继承构造函数。如果你有 C++11,你可以显式地将你的基础 class 构造函数转发给派生的 class:
class Algorithm1A : Algorithm1 {
public:
using Algorithm1::Algorithm1;
...
};
我在 .h
文件中定义了一个摘要 class Algorithm1
。我希望这个抽象 class、Algorithm1
保存对其他一些抽象 class 的引用,并能够在运行时决定确切的引用。所以我的 Algorithm1
持有这些引用,我希望能够通过构造函数传递确切的实现。 Algorithm1
的一个确切实现是 class Algorithm1A
所以这是它的样子。
Algorithm1.h
#include "Interval.h"
#include "OrderedAlphabet.hpp"
#include "Rank.h"
//! Algorithm1 interface
class Algorithm1 {
protected:
Rank &rank;
OrderedAlphabet &alphabet;
std::map<symbol_type, occurence_type> &C;
public:
Algorithm1(Rank &r,
OrderedAlphabet &a,
std::map<symbol_type, occurence_type> &c):
rank(r), alphabet(a), C(c) {
}
};
#endif /* Algorithm1_h */
算法1A
#include "Algorithm1.h"
class Algorithm1A : Algorithm1 {
std::string uniqueCharsInInterval(int i, int j) {
rank.rank() // dome something with ABSTRACT rank
}
std::vector<Interval> getIntervals(int i, int j) {
alphabet.getSize() // do something with alphabet - not abstract
C.find('x') // do something with C - not abstract
}
};
除此之外,我的Rank
class也是一个抽象class,但是是由classWTRank
、NTRank
实现的等等。所以我真正想要的是能够传递 WTRank
对象,但作为对 Algorithm1
.
Rank
引用
Rank.h
#ifndef Rank_h
#define Rank_h
#include "DataTypes.h"
//! Rank interface
class Rank {
public:
virtual unsigned long long rank(int index, symbol_type symbol)=0;
};
#endif /* Rank_h */
而我的WTRank
class分为.h
和.cpp
两个文件。
WTRank.h
#ifndef WTRank_h
#define WTRank_h
#include "Rank.h"
#include <sdsl/wavelet_trees.hpp>
using namespace sdsl;
//! Rank based on Wavelet Tree
class WTRank: public Rank {
private:
wt_huff<> wt;
public:
WTRank(wt_huff<> w_tree) {
this->wt = w_tree;
}
~WTRank();
unsigned long long rank(int index, symbol_type symbol);
};
#endif /* WTRank_h */
WTRank.cpp
#include "WTRank.h"
unsigned long long WTRank::rank(int index, symbol_type symbol) {
return wt.rank(index, symbol);
}
现在,如果我想创建一个实现 Algorithm1
抽象 class 的 Algorithm1A
对象,并且我想为其构造函数提供所需的参数,我正在做这样的事情 - 并且得到 "No matching constructor error."
wt_huff<> wt;
construct_im(wt, str, 1);
OrderedAlphabet alphabet(10);
std::map<symbol_type, occurence_type> C = calculateC(wt, alphabet);
WTRank wtrank = *new WTRank(wt);
Algorithm1A alg = new Algorithm1A(wtrank, &alphabet, &C); // ERROR!!
如何才能让它发挥作用?
构造函数不被继承。您需要在 Algorithm1A
上定义一个构造函数,它接受适当的参数。
Algorithm1A::Algorithm1A(Rank &r,
OrderedAlphabet &a,
std::map<symbol_type, occurence_type> &c):
Algorithm1(r, a, c)
{}
在 C++ 中默认情况下不继承构造函数。如果你有 C++11,你可以显式地将你的基础 class 构造函数转发给派生的 class:
class Algorithm1A : Algorithm1 {
public:
using Algorithm1::Algorithm1;
...
};