我如何使用一个类对象进入另一类 [英] how can i use one class object into other class

查看:77
本文介绍了我如何使用一个类对象进入另一类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像以前一样,我从U获得数据库类的帮助

现在我要执行以下任务...

1.我有一类名为"pthread"的类正在创建线程.线程仅显示其编号

2.我还有一个名为DBconnecter的类,它仅连接到数据库.它用于在数据库中插入数据

现在是我的问题开始

...我想在pthread类中使用DBconnecter类的对象
当我的pthread类执行任务时,必须执行此工作

....我的pthread类正在创建线程.
由pthread类创建的线程.
他们必须将一些硬编码数据输入数据库.


我该怎么办?

[edit]从错误的解决方案中复制:[/edit]

这是我的DBconnector类

As previously I get help in Database class from U

now I want to perform following task...

1. I have One class with name "pthread" which is creating thread.the threads are simply display its number

2. I have another class with name DBconnecter it simply get connected to the database.It is used to insert data in database

NOW HERE IS MY PROBLEM START

...I want to use DBconnecter class''s object in pthread class
when my pthread class perform task it must to do this work

....my pthread class is creating thread.
the thread which creating by pthread class.
they must to enter some hard coded data into database.


how can I do that?

[edit]copied from misplaced solution:[/edit]

this is my DBconnector class

/*
* DBconnector.cpp
*
*   Created on: Mar 1, 2012
*         Author: root
*/
 
#include "DBconnector.h"
#include <mysql.h>
#include <stdio.h>
#include <iostream>
 
MYSQL mysql,*connection;
MYSQL_RES *result;
MYSQL_ROW row;
 
char * ip = (char*)"192.168.49.131";
char * usr = (char*)"root";
char * pass = (char*)"admin";
char * db = (char*)"vcs";
 
unsigned fields;
using namespace std;
 
void DBconnector ()
{
     mysql_init(&mysql);
 
     connection = mysql_real_connect(&mysql, ip, usr, pass, db, 0, NULL, 0);
 
     if (connection==NULL)
     {
          std::cout << mysql_error(&mysql)<< std::endl;
     }
 
     else
     {
          if(mysql_query(&mysql, "INSERT INTO `vcs`.`users`   (`id`, `fname`, `sname`) VALUES (5, 'Rana', 'sameer')"));
 
          else
          {
               result = mysql_store_result(&mysql);
               fields = mysql_num_fields(result);
 
               while((row = mysql_fetch_row (result)))
               {
                    unsigned long *length;
                    length = mysql_fetch_lengths(result);
 
                    for(int i=0; i<fields; i++)
                    {
                         cout <<   length[i], row[i] , row[i]   ;
                         cout << "NULL";
                    }
                    cout << endl;
               }
          }
     }
}
 

namespace rumeel
{
 
     DBconnector::DBconnector()
     {
          // TODO Auto-generated constructor stub
 
     }
 
     DBconnector::~DBconnector()
     {
          // TODO Auto-generated destructor stub
     }
 
} /* namespace rumeel */





这是我的pthread类







and this is my pthread class



/*
* pThread.cpp
*
*   Created on: Mar 1, 2012
*         Author: root
*/
 
#include "pThread.h"
#include <iostream>
#include <pthread.h>
#include <stdio.h>
 
using namespace std;
 
     void * threadfunction (void *arg)
     {
          cout << "Thread Id = " <<   int(arg)<<endl;
 
          return (void *) 0;
     }
 
     int main()
     {
          DBconnector -> DBconnector();
          int j;
 

          int value;
          cout << "Enter The Number Of Threads :";
          cin >> value;
 
     for(int i=1 ; i <= value ;i++)
          {
               pthread_t thread;
 
               pthread_create( &thread, NULL, threadfunction, (void*) i);
          }
 
          cin>>j;
          return 0;
 
     }
 

 

 

namespace rumeel
{
 
     pThread::pThread()
     {
          // TODO Auto-generated constructor stub
 
     }
 
     pThread::~pThread()
     {
          // TODO Auto-generated destructor stub
     }
 
} /* namespace rumeel */



对不起,英文不好.



sorry for poor English

推荐答案

您可以简单地将所需的对象(指向该对象的指针)传递给线程函数.
当心,无论何时使用线程,都必须使用同步,即必须保护数据免受并发访问(例如,参见 [ ^ ].
You may simply pass the object (the pointer to the object) you need to the thread function.
Beware, whenever you use threads you must use synchronization, that is you must protect your data from concurrent access (see, for instance, "POSIX thread (pthread) libraries"[^].


这篇关于我如何使用一个类对象进入另一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆