GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Placement Papers  >  L&T infotech

 Print  |  
Question:  eg.for a program that can be done in C but not in C++?



September 09, 2006 02:15:47 #3
 Saurabh Shankar   Member Since: Visitor    Total Comments: N/A 

RE: eg.for a program that can be done in C but not in ...
 

A classic example for a program that can be written in C and not in C++ is creating a mutlithreaded program with POSIX Thread. The POSIX thread functions have been defined in such a way that it cannot be acessed as a member function of an object. If it is required to create a thread of a memeber function of an object in C++, the only way to resolve it is create a thread of a C function which in turn calls the member of function of the desired object.

E.g.

class xyz

{

 private:

   // all variables defined here

public:

  void newthreadfunction(void Param);

};

xyz someobject; // creating a global object of class xyz

// the C function which will be created as a new thread which will access the memeber function of the object

void *NewThread(void *Parameter)

{

   someobject.newthreadfunction((void)Parameter); //call of the function

}

void main()

{

   pthread_t Thread;

  int ThreadCreated;

  ThreadCreated = pthread_create(&Thread, NewThread,(void*)3);

}

     

 

Back To Question