Scope of constructor initialisation list

P

Pantokrator

Hi all,
I've got a question about the scope of the constructor initialisation
list. First of all, let me explain my classes:



// *****************************************************
class CThread
{
CThread(){};
CThread(CMyThreadFunctor *pThreadFunctor)
{
...
};
};
// *****************************************************
class CMyThreadFunctor
{
....
};
// *****************************************************
class CMyThread : public CThread
{

CMyThreadFunctor *m_pThreadFunctor;

public:

CMyThread()
: CThread(m_pThreadFunctor = new CMyThreadFunctor(this)) <<<<
SCOPE-1
{
//CThread(m_pThreadFunctor = new CMyThreadFunctorIMO(this));
<<<<SCOPE-2

}
};

When I init the CThread object using the constructor init list
(SCOPE-1), my CThread object runs fine without a problem. If I init
CThread within the constructor (SCOPE-2), my CThread calls the
destructor straight after initialisation and I suspect that it is
related to the scope of the object. I even tried the following but
without any luck:
class CMyThread : public CThread
{

CMyThreadFunctor *m_pThreadFunctor;
CThread

public:

CMyThread()
: CThread(m_pThreadFunctor = new CMyThreadFunctor(this)) <<<<
SCOPE-1
{
//CThread(m_pThreadFunctor = new CMyThreadFunctorIMO(this));
<<<<SCOPE-2

}
};
 
P

Pantokrator

....sorry presed the wrong button by mistake..

Here's the entire message again:


Hi all,
I've got a question about the scope of the constructor initialisation
list. First of all, let me explain my classes:

// *****************************************************
class CThread
{
CThread(){};
CThread(CMyThreadFunctor *pThreadFunctor)
{
...
};};

// *****************************************************
class CMyThreadFunctor
{
...};

// *****************************************************
class CMyThread : public CThread
{

CMyThreadFunctor *m_pThreadFunctor;

public:

CMyThread()
: CThread(m_pThreadFunctor = new
CMyThreadFunctor(this)) <<<< SCOPE-1
{
//CThread(m_pThreadFunctor = new
CMyThreadFunctorIMO(this)); <<<<SCOPE-2

}

};

When I init the CThread object using the constructor init list
(SCOPE-1), my CThread object runs fine without a problem. If I init
CThread within the constructor (SCOPE-2), my CThread calls the
destructor straight after initialisation and I suspect that it is
related to the scope of the object. I even tried the following but
without any luck:

class CMyThread : public CThread
{

CMyThreadFunctor *m_pThreadFunctor;
CThread mainThread;
public:

CMyThread()
//: CThread(m_pThreadFunctor = new
CMyThreadFunctor(this)) <<<< SCOPE-1
{
mainThread=(m_pThreadFunctor = new
CMyThreadFunctorIMO(this)); <<<<SCOPE-2

}

};


Could you please tell me what am i doing wrong here and what is the
sccope of the two lines?

Thanks
 
D

dasjotre

...sorry presed the wrong button by mistake..

Here's the entire message again:

Hi all,
I've got a question about the scope of the constructor initialisation
list. First of all, let me explain my classes:

// *****************************************************
class CThread
{
CThread(){};
CThread(CMyThreadFunctor *pThreadFunctor)
{
...
};};

// *****************************************************
class CMyThreadFunctor
{
...};

// *****************************************************
class CMyThread : public CThread
{

CMyThreadFunctor *m_pThreadFunctor;

public:

CMyThread()
: CThread(m_pThreadFunctor = new
CMyThreadFunctor(this)) <<<< SCOPE-1

This initializes base CThread
{
//CThread(m_pThreadFunctor = new
CMyThreadFunctorIMO(this)); <<<<SCOPE-2

This creates temporary non named object of type CThread
and calls it's destructor at ';'.

D.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top