使用Postgresql.
我尝试使用TRIGGER程序对INSERT进行一些一致性检查.
问题是 ......
是否"在插入每行之前"是否可以确保每行插入"已检查"和"插入"一个接一个?我是否需要在桌面上额外锁定才能从并发插入中生存?
检查新的row1 - >插入row1 - >检查新的row2 - >插入row2
--
--
-- unexpired product name is unique.
CREATE TABLE product (
"name" VARCHAR(100) NOT NULL,
"expired" BOOLEAN NOT NULL
);
CREATE OR REPLACE FUNCTION check_consistency() RETURNS TRIGGER AS $$
BEGIN
IF EXISTS (SELECT * FROM product WHERE name=NEW.name AND expired='false') THEN
RAISE EXCEPTION 'duplicated!!!';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trigger_check_consistency
BEFORE INSERT ON product
FOR EACH ROW EXECUTE PROCEDURE check_consistency();
--
INSERT …Run Code Online (Sandbox Code Playgroud) 我有一个模板函数,带有模板化参数类型的另一个函数.
使用它时,我必须明确指定参数类型(1)否则不会编译(2).
template<typename T>
void process(const std::function<void(const T &)>& f)
{
// ...
}
process<Foo>( [&](const Foo& arg){/*...*/} ); // (1) Ok!
// process( [&](const Foo& arg){/*...*/} ); // (2) Won't Work!
Run Code Online (Sandbox Code Playgroud)
让(2)工作有什么诀窍吗?
我有两种方法来为指针创建实例.但其中一个会失败.
class A {
public:
int num;
};
void testPointer1(A* a){
a = new A();
a->num = 10;
}
A* testPointer2(){
A* a = new A();
a->num = 10;
return a;
}
void testPointer() {
A* a1 = NULL;
testPointer1(a1); // this one fails
//cout << a1->num << endl; // segmentation fault
A* a2 = NULL;
a2 = testPointer2();
cout << a2->num << endl;
}
Run Code Online (Sandbox Code Playgroud)
为什么testPointer1错了?
启动一个类对象我有一个奇怪的问题.这个问题很奇怪,不易再现.但是我会尝试给出一个指示示例.我有继承类.
class BarClass {
public:
BarClass() {
...
}
BarClass(int i, int j) {
...
}
void doSomething() { ... }
};
class FooClass : public BarClass {
public:
FooClass() {
}
FooClass(int i, int j) : BarClass(i,j) {
...
}
};
Run Code Online (Sandbox Code Playgroud)
有时如果我以下面的方式启动对象,我会通过初始化得到分段错误错误.
FooClass foo1;
foo1.doSomething();
FooClass foo2(10, 20);
foo2.doSomething();
Run Code Online (Sandbox Code Playgroud)
如果我使用显式指针new,那就没关系..
FooClass *foo1= new FooClass();
foo1->doSomething();
FooClass foo2(10, 20);
foo2.doSomething();
Run Code Online (Sandbox Code Playgroud)
以下代码将在第2行给出编译器错误.
FooClass foo1();
foo1.doSomething();
FooClass foo2(10, 20);
foo2.doSomething();
Run Code Online (Sandbox Code Playgroud)
我应该如何正确地启动一个对象,特别是当它有默认构造函数和带参数的对象时.
c++ ×3
class ×1
constructor ×1
object ×1
pointers ×1
postgresql ×1
std-function ×1
templates ×1
triggers ×1