我有一个模板课
template <T>
class Example
{
...
};
Run Code Online (Sandbox Code Playgroud)
其中有许多以下类型的方法:
template <class U> <class V> method(....)
Run Code Online (Sandbox Code Playgroud)
在这些内部我使用tr1 :: shared_ptr到U或V或T.
它繁琐的打字tr1::shared_ptr<const U>
或tr1::shared_ptr<const V>
.
显而易见的事情:
template <typename U>
typedef tr1::shared_ptr<U> shptr<U>;
Run Code Online (Sandbox Code Playgroud)
不起作用.
你在这种情况下做了什么?什么能减少冗长的东西?
我想用静态方法定义为类X:
class X
{
static string get_type () {return "X";}
//other virtual methods
}
Run Code Online (Sandbox Code Playgroud)
我想强制继承自X的类重新定义get_type()方法并返回与"X"不同的字符串(如果他们现在只重新定义get_type,我很高兴).
我该怎么做呢?我知道我不能拥有虚拟静态方法.
编辑:问题不是关于type_id,而是关于应该重写的静态方法.例如
class X {
static int getid() {return 1;}
}
Run Code Online (Sandbox Code Playgroud) 虽然我std::tr1::shared_ptr<T>
在我的编译器中可用,但我没有make_shared
.
有人能指点我正确实施make_shared
吗?我看到我需要使用varargs为T的构造函数提供参数.
但是我的编译器中也没有可用的变量模板.
我有一个XML文件,格式如下:
<doc>
<id name="X">
<type name="A">
<min val="100" id="80"/>
<max val="200" id="90"/>
</type>
<type name="B">
<min val="100" id="20"/>
<max val="20" id="90"/>
</type>
</id>
<type...>
</type>
</doc>
Run Code Online (Sandbox Code Playgroud)
我想解析这个文档并构建一个哈希表
{X: {"A": [(100,80), (200,90)], "B": [(100,20), (20,90)]}, Y: .....}
Run Code Online (Sandbox Code Playgroud)
我将如何在Python中执行此操作?
如果我的代码中有用户定义的异常,我就无法通过Boost测试将其视为失败.
例如,
BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MyTest,1)
BOOST_AUTO_TEST_CASE(MyTest)
{
// code which throws user defined exception, not derived from std::exception.
}
Run Code Online (Sandbox Code Playgroud)
我收到一条通用消息:
Caught exception: ....
unknown location(0):....
Run Code Online (Sandbox Code Playgroud)
它不会将此错误识别为失败,因为它不是std :: exception.所以它不尊重expected_failures子句.
如何强制执行该段代码应始终抛出异常?这似乎是一件有用的事情.如果将来的代码更改导致代码通过并且没有抛出异常,我想知道这一点.
我有整个使用原始指针的代码.
它需要调用一个将原始指针转换为shared_ptr的方法.这种方法不在我的控制之下,属于外部api.我不能只是将指针传递给shared_ptr,因为当shared_ptr超出方法范围时(当方法返回时)它将被删除.
除了在我的内部代码中将原始指针设为shared_ptr之外,我还有其他选择吗?
我有以下人为的例子(来自实际代码):
template <class T>
class Base {
public:
Base(int a):x(a) {}
Base(Base<T> * &other) { }
virtual ~Base() {}
private:
int x;
};
template <class T>
class Derived:public Base<T>{
public:
Derived(int x):Base<T>(x) {}
Derived(Derived<T>* &other): Base<T>(other) {}
};
int main() {
Derived<int> *x=new Derived<int>(1);
Derived<int> y(x);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,我得到:
1X.cc: In constructor ‘Derived<T>::Derived(Derived<T>*&) [with T = int]’:
1X.cc:27: instantiated from here
1X.cc:20: error: invalid conversion from ‘Derived<int>*’ to ‘int’
1X.cc:20: error: initializing argument 1 of ‘Base<T>::Base(int) [with T = int]’
Run Code Online (Sandbox Code Playgroud)
1)显然gcc被构造函数混淆了.如果我从构造函数中删除引用,那么代码将编译.所以我的假设是上传指针引用出了问题.谁能告诉我这里发生了什么? …
我上课了
template <typename T>
class C
{
static const int K=1;
static ostream& print(ostream& os, const T& t) { return os << t;}
};
Run Code Online (Sandbox Code Playgroud)
我想将C专门化为int.
//specialization for int
template <>
C<int>{
static const int K=2;
}
Run Code Online (Sandbox Code Playgroud)
我想要保留int的默认打印方法,只需更改常量.对于某些特化,我想保持K = 1并更改print方法,因为没有<<运算符.
我该怎么做呢?
我有以下代码:
if (book.type == A) do_something();
else if (book.type == B) do_something_else();
....
else do so_some_default_thing.
Run Code Online (Sandbox Code Playgroud)
只要有新书类型或删除书籍类型,就需要修改此代码.我知道我可以使用枚举并使用switch语句.是否有一种设计模式可以删除if-then-else?
与使用switch语句相比,这种模式有哪些优点?
有没有理由说std :: tr1 :: unordered_map省略了为std :: map存在的相等(==)运算符?
实现这个的好方法是什么?我正在考虑创建两组unordered_map :: value_type,从两个hash_maps初始化它们,然后检查两个set的相等性.
c++ ×9
boost ×1
dom ×1
equality ×1
if-statement ×1
inheritance ×1
make-shared ×1
pointers ×1
python ×1
reference ×1
shared-ptr ×1
static ×1
templates ×1
tr1 ×1
typedef ×1
unit-testing ×1
upcasting ×1
xml ×1