Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to create a class "Parallel" which is a subclass of thread class,therefore my class is defined in "Parallel.h",but my main method defined in separate file "main.cpp" in same project(in visual studio).When I create an instance of Parallel class and execute join() function in main method as below code segment:
//Parallel.h

C#
#include <thread>

using namespace std;
namespace Para{
    class Parallel:thread
    {
    public:

        static void run(){

        }
        Parallel(void)
        {
        }

        virtual ~Parallel(void)
        {
        }

        inline static void start(Parallel* p){
                    // (*p).join();
        }

        virtual void Parallel::start(thread& t){

        }
        static void parallelize(Parallel& p1,Parallel& p2){

        }
        inline virtual Parallel* operator=(thread* t){
            return  static_cast<Parallel*>(t);
        }
    }

;
}
//main.cpp
C#
#include <iostream>
#include "Parallel.h"
#include "Resource.h"

using namespace std;
using namespace Para;

void main(){

    Parallel p;
    p.join();
    thread t(print);
     t.join();
     //cout<<p.get_id()<<"\n";
     system("Pause");

}


print function defined in "Resource.h"

Problem is how to define a proper subclass of a thread class having a overloaded constructor taking function name as a parameter,also "
p.join()
" gave following compile errors:

Error 2 error C2247: 'std::thread::join' not accessible because 'Para::Parallel' uses 'private' to inherit from 'std::thread' C:\Users\Gamer\Desktop\PROJECQ\VC++@OMAQ\CQ47\CQ47\main.cpp 11

3 IntelliSense: function "std::thread::join" (declared at line 209 of "H:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\thread") is inaccessible c:\Users\Gamer\Desktop\PROJECQ\VC++@OMAQ\CQ47\CQ47\main.cpp 11


How to construct a subclass of thread class properly
Posted
Updated 6-Oct-13 4:39am
v4

1 solution

You need to carry the public scope attributes forward to your inherited class, thus:
C++
class Parallel: public thread
{
 
Share this answer
 
Comments
Buddhi Chaturanga 6-Oct-13 22:41pm    
thank you.But lot of people are saying that constructing a subclass of std::thread class isn't a wise idea.I want exact reason or definition for the cause,could you help me on that?
Richard MacCutchan 7-Oct-13 2:47am    
Who are these people, and why aren't you asking your question of them?
As to your second question, you should read up on inheritance in your C++ reference.
kaushik4study 8-Oct-13 14:25pm    
:)
Buddhi Chaturanga 8-Oct-13 22:52pm    
@Richard MacCutchan -> Stackoverflow people.
Richard MacCutchan 9-Oct-13 3:30am    
Then post your question on StackOverflow.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900