免责声明我不允许使用BOOST或任何其他库,只能使用标准版.
在我的类中,Foo我有一个模板函数foo,它有两个参数:指向对象的指针和指向此对象成员函数的指针.foo正如您所见,该函数适用于任何类的指针.我不知道,会传给哪一堂课.此函数创建模板结构的实例.
template <typename TypeName>
struct Bar
{
void(TypeName::*action)();
TypeName* objPtr;
};
template <typename TypeName> void Foo::foo ( void(TypeName::*action)() , TypeName* objPtr )
{
Bar <TypeName> bar;
bar.action = action;
bar.objPtr = objPtr;
};
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何存储Bar,创建的对象foo,以便稍后我可以迭代它们并调用指针到对象成员函数,如下所示:
(BarStorage[i].objPtr->*BarStorage[i].action)();
Run Code Online (Sandbox Code Playgroud) 我想知道,AS3中如何实现异步?让我们Timer上课.计时器以异步方式运行并调度某些事件.似乎它为自己创造了一个新的线程.当计时器事件发生时调用的函数如何是线程安全的?如何在AS3中实现线程安全?
multithreading actionscript asynchronous thread-safety actionscript-3
在我的Qt项目中,我有一个QPushButton和一个QLineEdit实例.我想在按下QPushButton时禁用QLineEdit.
我写了这段代码:
this->btn = new QPushButton(this);
this->txt = new QLineEdit(this);
QObject::connect(this->btn,SIGNAL(clicked(bool)),this->txt,SLOT(setDisabled(bool)));
Run Code Online (Sandbox Code Playgroud)
这不起作用.你能帮我吗?我的错是什么?
我想扩展std::string一些功能,所以我从中推导String出来.为了使代码像String str = stdStr;工作一样,我试图重载赋值运算符,但我的代码由于某种原因没有被调用.我该如何解决?
#include <string>
class String
:
public std::string
{
public:
/*
I do know that this constructor will solve the problem, but is it possible to use the operator?
String ( const std::string& stdString )
{
...
}
*/
String& operator= ( const std::string& stdString )
{
...
return *this;
}
};
int main()
{
std::string foo = "foo";
String bar = foo;
return 1;
}
Run Code Online (Sandbox Code Playgroud) c++ overloading operator-overloading variable-assignment assignment-operator
如果在线程对象构造完成后保证线程实际正在运行,我在文档中找不到任何信息.换句话说,是否保证在线程构造函数完成后线程函数已经被执行?一些参考文献将不胜感激.
我的一个伙伴告诉我,我的代码中有内存泄漏
Base
{
public:
vector<Foo*> fooes;
};
Derived : public Base
{
public:
Derived ( )
{
for ( int i = 0 ; i < 10 ; i++ )
{
this.fooes.push_back ( new Foo() );
}
};
};
Run Code Online (Sandbox Code Playgroud)
但他是一个非常忙碌的人,他无法帮助我,所以我问你,内存泄漏在哪里?我该如何解决?据我了解,内存泄漏是我不删除创建的对象new Foo(),所以我只能添加一个析构函数Base,并清除fooes向量,对吧?
Base
{
public:
vector<Foo*> fooes;
~Base ( )
{
this->fooes.clear();
};
};
Run Code Online (Sandbox Code Playgroud)
问题是:
这是正确的内存泄漏修复吗?
会的析构函数Base的派生,或不析构函数之前被调用?
请问fooesvertor被自动删除,同时删除Base或我必须手动删除类的所有成员?
可能重复:
带空括号的默认构造函数
实例化带或不带括号的类?
程序:
class Foo
{
public:
Foo ( int bar = 1 )
{
cout << "bar=" << bar;
}
};
int main() {
cout << "0 - ";
Foo foo_0 ( 0 ) ;
cout << '\n';
cout << "1 - ";
Foo foo_1 ();
cout << '\n';
cout << "2 - ";
Foo foo_4;
cout << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
0 - bar=0
1 -
2 - bar=1
Run Code Online (Sandbox Code Playgroud)
问题:为什么示例#1不起作用,而示例#0和#2呢?
免责声明:Boost和C++ 11都不允许.
我有一个程序,我在其中创建了一个实例,Foo并在多个线程中使用它.然后我想要安全地删除它,以便这些线程不会陷入分段错误.
Foo每次线程函数运行时,我都会添加一个互斥锁成员并将其锁定.为了使不同的线程不相互冲突.
class Foo
{
public:
pthread_mutex_t mutex;
};
void* thread ( void* fooPtr )
{
Foo* fooPointer = (Foo*) fooPtr;
while ( 1 )
{
if ( fooPointer )
{
pthread_mutex_lock ( &fooPointer->mutex );
/* some code, involving fooPointer */
pthread_mutex_unlock ( &fooPointer->mutex );
}
else
{
pthread_exit ( NULL );
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想要foo安全删除,因此线程中不会发生错误.我添加了一个析构函数Foo:
Foo::~Foo()
{
pthread_mutex_lock ( &mutex );
}
Run Code Online (Sandbox Code Playgroud)
现在,在所有线程完成当前循环之前,不会删除该对象.
问题是:删除实例后是否会解锁互斥锁?删除实例后,所有线程都会完成吗?我打赌答案是no.所以我改变了析构函数,但现在看起来线程不安全:
Foo::~Foo()
{
pthread_mutex_lock ( …Run Code Online (Sandbox Code Playgroud) c++ ×6
constructor ×2
actionscript ×1
asynchronous ×1
c++11 ×1
class ×1
instance ×1
memory ×1
memory-leaks ×1
mutex ×1
overloading ×1
pointers ×1
qt4 ×1
signals ×1
slots ×1
templates ×1