我有以下代码:
#include <iostream>
class BaseClass {
protected:
static int x;
};
int BaseClass::x;
class DerivedA: public BaseClass {
public:
DerivedA() {
x = 3;
}
};
class DerivedB: public BaseClass {
public:
DerivedB() {
std::cout << DerivedA::x;
}
};
int main(int argc, char* argv[]) {
DerivedB b;
}
Run Code Online (Sandbox Code Playgroud)
用g ++(g++ classtest.cpp)编译我收到以下错误:
classtest.cpp:在构造函数'DerivedB :: DerivedB()'中:
classtest.cpp:9:5:错误:'int BaseClass :: x'受保护
int BaseClass :: x;
^ classtest.cpp:25:32:错误:在此上下文中
std :: cout << DerivedA :: x;
当我用clang ++(clang++ classtest.cpp)编译时,没有错误.
为什么g ++会返回编译错误? …
我的一个项目中有很多自定义数据类型,它们共享一个公共基类.
我的数据(来自数据库)有一个数据类型,它由基类的枚举区分.我的体系结构允许特定的数据类型专门用于派生类,或者它可以由基类处理.
当我构造一个我特定的数据类型时,我通常直接调用构造函数:
Special_Type_X a = Special_Type_X("34.34:fdfh-78");
a.getFoo();
Run Code Online (Sandbox Code Playgroud)
有一些模板魔术也可以像这样构建它:
Type_Helper<Base_Type::special_type_x>::Type a = Base_Type::construct<Base_Type::special_type_x>("34.34:fdfh-78");
a.getFoo();
Run Code Online (Sandbox Code Playgroud)
对于枚举类型的某些值,可能没有专门化
Type_Helper<Base_Type::non_specialized_type_1>::Type == Base_Type
Run Code Online (Sandbox Code Playgroud)
当我从数据库中获取数据时,在编译时不知道数据类型,因此有第三种方法来构造数据类型(来自QVariant):
Base_Type a = Base_Type::construct(Base_type::whatever,"12.23@34io{3,3}");
Run Code Online (Sandbox Code Playgroud)
但是我当然希望调用正确的构造函数,所以该方法的实现过去看起来像:
switch(t) {
case Base_Type::special_type_x:
return Base_Type::construct<Base_Type::special_type_x>(var);
case Base_Type::non_specialized_type_1:
return Base_Type::construct<Base_Type::non_specialized_type_1>(var);
case Base_Type::whatever:
return Base_Type::construct<Base_Type::whatever>(var);
//.....
}
Run Code Online (Sandbox Code Playgroud)
这段代码是重复的,因为基类也可以处理新类型(添加到枚举),我提出了以下解决方案:
// Helper Template Method
template <Base_Type::type_enum bt_itr>
Base_Type construct_switch(const Base_Type::type_enum& bt, const QVariant& v)
{
if(bt_itr==bt)
return Base_Type::construct<bt_itr>(v);
return construct_switch<(Base_Type::type_enum)(bt_itr+1)>(bt,v);
}
// Specialization for the last available (dummy type): num_types
template <>
Base_Type construct_switch<Base_Type::num_types>(const Base_Type::type_enum& bt, const QVariant&)
{ …Run Code Online (Sandbox Code Playgroud) 我正在寻找可以集成到我的Qt5应用程序中的脚本语言.该应用程序有一个公共API,可用于通过插件扩展应用程序.现在我想在应用程序中添加脚本语言,以提供对整个公共API的访问.脚本语言必须满足以下要求:
我评估了以下脚本语言:
您建议哪些脚本语言和工具满足我的所有要求?
我有一个自定义angular2管道,它UserInfo使用服务将uid(字符串)转换为对象.
@Pipe({name: 'userInfo'})
export class UserInfoPipe implements PipeTransform {
constructor(public _userService: UserService) {
}
transform(uid:string) : any {
/*let users = this._userService.users.filter((u)=> {
return u.uid==uid;
});
if(users.length==1) return users[0];
return null;*/
return {"Name":"hans","Age":13};
}
}
Run Code Online (Sandbox Code Playgroud)
在我的组件中,我有一个绑定:
<Label row="4" text="Author: {{event?.author | userInfo | .Name}}" class="small-spacing"></Label>
Run Code Online (Sandbox Code Playgroud)
event?.author返回UID,在通过管道后userInfo我有一个对象.但是如何访问该对象的属性?有没有语法?.Name不起作用.
我目前正在研究Postgres 9.2中的复杂排序问题.您可以在此处找到本问题(简化)中使用的源代码:http://sqlfiddle.com/#!12/9857e/11
我有一个巨大的(>> 20Mio行)表,其中包含不同类型的各种列.
CREATE TABLE data_table
(
id bigserial PRIMARY KEY,
column_a character(1),
column_b integer
-- ~100 more columns
);
Run Code Online (Sandbox Code Playgroud)
假设我想在2列(ASC)上对此表进行排序.但是我不想用简单的Order By来做那个,因为后来我可能需要在排序的输出中插入行,用户可能只希望一次看到100行(排序的输出).
为了实现这些目标,我执行以下操作:
CREATE TABLE meta_table
(
id bigserial PRIMARY KEY,
id_data bigint NOT NULL -- refers to the data_table
);
--Function to get the Column A of the current row
CREATE OR REPLACE FUNCTION get_column_a(bigint)
RETURNS character AS
'SELECT column_a FROM data_table WHERE …Run Code Online (Sandbox Code Playgroud) 我正在使用 SWIG 为我的 qt 应用程序生成 Python 绑定。我有几个地方使用QList,我想从 SWIG 库中集成像 std::vector 这样的QList(参见http://www.swig.org/Doc1.3/Library.html#Library_nn15)。
这意味着:
为了实现这一点,我使用以下代码:https :
//github.com/osmandapp/OsmAnd-core/blob/master/swig/java/QList.i
后来在我使用 QLists 的课程中,我添加了如下代码:
%import "qlist.i"
%template(listfilter) QList<Interface_Filter*>;
class A {
public:
//.....
QList<Interface_Filter*> get_filters();
};
Run Code Online (Sandbox Code Playgroud)
到目前为止,这有效,但它没有给我与 std::vector 的那种集成。我无法找出 std_vector.i、std_container.i、... 的哪些部分使对象可迭代。
我需要如何扩展 QList 接口文件才能使我的 QList 可迭代?
我有以下代码:
#include <experimental/string_view>
struct b_symbol {
template <typename T>
explicit b_symbol(T&& symbol)
: symbol(std::forward<T>(symbol)) {
}
std::experimental::string_view symbol;
};
struct b_utf8 {
template <typename T>
explicit b_utf8(T&& value)
: value(std::forward<T>(value)) {
}
std::experimental::string_view value;
};
struct value {
explicit value(b_utf8) {}
explicit value(b_symbol) {}
};
int main() {
value v({b_utf8("test")});
}
Run Code Online (Sandbox Code Playgroud)
如果我用clang(3.8.0)编译它:
clang++ oload.cpp -std=c++1y
一切运行正常.
如果我用gcc(6.1.1 20160602)编译它
g++ oload.cpp -std=c++1y
我得到:
Run Code Online (Sandbox Code Playgroud)oload.cpp: In function ‘int main()’: oload.cpp:30:29: error: call of overloaded ‘value(<brace-enclosed initializer list>)’ …