小编Kol*_*nya的帖子

使用模板对象操作

免责声明我不允许使用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)

c++ templates pointers function-pointers pointer-to-member

1
推荐指数
1
解决办法
109
查看次数

Action Script 3.0中的异步

我想知道,AS3中如何实现异步?让我们Timer上课.计时器以异步方式运行并调度某些事件.似乎它为自己创造了一个新的线程.当计时器事件发生时调用的函数如何是线程安全的?如何在AS3中实现线程安全?

multithreading actionscript asynchronous thread-safety actionscript-3

1
推荐指数
1
解决办法
2788
查看次数

Qt连接信号和插槽

在我的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)

这不起作用.你能帮我吗?我的错是什么?

signals qt4 disabled-control disabled-input slots

1
推荐指数
1
解决办法
482
查看次数

重载C++赋值运算符

我想扩展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

1
推荐指数
3
解决办法
920
查看次数

std :: thread构造和执行

如果在线程对象构造完成后保证线程实际正在运行,我在文档中找不到任何信息.换句话说,是否保证在线程构造函数完成后线程函数已经被执行?一些参考文献将不胜感激.

c++ multithreading constructor c++11

1
推荐指数
1
解决办法
413
查看次数

C++内存泄漏修复

我的一个伙伴告诉我,我的代码中有内存泄漏

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)

问题是:

  1. 这是正确的内存泄漏修复吗?

  2. 会的析构函数Base的派生,或不析构函数之前被调用?

  3. 请问fooesvertor被自动删除,同时删除Base或我必须手动删除类的所有成员?

c++ memory memory-leaks derived-class

0
推荐指数
1
解决办法
249
查看次数

C++创建类的实例?

可能重复:
带空括号的默认构造函数
实例化带或不带括号的类?

程序:

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呢?

c++ constructor class instance default-parameters

0
推荐指数
1
解决办法
231
查看次数

从多线程程序安全地删除对象

免责声明: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++ multithreading mutex thread-safety delete-operator

0
推荐指数
1
解决办法
4884
查看次数