473,698 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scope of constructor initialisation list

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(CMyThre adFunctor *pThreadFunctor )
{
...
};
};
// *************** *************** *************** ********
class CMyThreadFuncto r
{
....
};
// *************** *************** *************** ********
class CMyThread : public CThread
{

CMyThreadFuncto r *m_pThreadFunct or;

public:

CMyThread()
: CThread(m_pThre adFunctor = new CMyThreadFuncto r(this)) <<<<
SCOPE-1
{
//CThread(m_pThre adFunctor = new CMyThreadFuncto rIMO(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
{

CMyThreadFuncto r *m_pThreadFunct or;
CThread

public:

CMyThread()
: CThread(m_pThre adFunctor = new CMyThreadFuncto r(this)) <<<<
SCOPE-1
{
//CThread(m_pThre adFunctor = new CMyThreadFuncto rIMO(this));
<<<<SCOPE-2

}
};

May 18 '07 #1
3 1772
....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(CMyThre adFunctor *pThreadFunctor )
{
...
};};

// *************** *************** *************** ********
class CMyThreadFuncto r
{
...};

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

CMyThreadFuncto r *m_pThreadFunct or;

public:

CMyThread()
: CThread(m_pThre adFunctor = new
CMyThreadFuncto r(this)) <<<< SCOPE-1
{
//CThread(m_pThre adFunctor = new
CMyThreadFuncto rIMO(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
{

CMyThreadFuncto r *m_pThreadFunct or;
CThread mainThread;
public:

CMyThread()
//: CThread(m_pThre adFunctor = new
CMyThreadFuncto r(this)) <<<< SCOPE-1
{
mainThread=(m_p ThreadFunctor = new
CMyThreadFuncto rIMO(this)); <<<<SCOPE-2

}

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

Thanks

May 18 '07 #2
On 18 May, 12:05, Pantokrator <ado...@shotoku .co.ukwrote:
...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(CMyThre adFunctor *pThreadFunctor )
{
...
};};

// *************** *************** *************** ********
class CMyThreadFuncto r
{
...};

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

CMyThreadFuncto r *m_pThreadFunct or;

public:

CMyThread()
: CThread(m_pThre adFunctor = new
CMyThreadFuncto r(this)) <<<< SCOPE-1
This initializes base CThread
{
//CThread(m_pThre adFunctor = new
CMyThreadFuncto rIMO(this)); <<<<SCOPE-2
This creates temporary non named object of type CThread
and calls it's destructor at ';'.

D.

May 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4429
by: mihai | last post by:
People say that is a bad technique to throw exception from constructors; and that the solution would be to create a function _create_ to initialize an object. What about copy constructors? How can we avoid throwing exceptions? If we already have an abject witch was initialized wit _create_ we will be forced to call create in copy constructor and to throw exceptions from it. Have a nice day,
8
1906
by: arun | last post by:
Hello Group, I have a class class Simple2 { publica: Simple2(); list<int> *l1; list<int> l2; };
26
2143
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null); this.property1 = arg; this.property2 = aConstantDefinedGlobally; this.method1 = function (anArg) {
4
14795
by: ali | last post by:
Hi, I am new to C++ and trying to understand how to work on Inheritance and Operator overloading. I understand that the derived class can pass the base class constructor in its constructor definition as following code: Car::Car(int id, int colorID, int type):Vehicle(id, colorID) { this->type = type;
6
2112
by: Mark | last post by:
I have a problem with a template class defined: // start matrix.h file template <class Tclass Matrix { public: Matrix() { // default constructor } Matrix(const Subscript rows, const Subscript cols)
20
432
by: royashish | last post by:
Hi all , A question to the C++ fraternity , Why the Copy constructor ? Was suggested in Effective C++ , In Case the Object contains a Char* , a assignment operator = makes the two object point to the same address . In this case any Of the two Object is destroyed a memory leak occurs as one of the object stays on the Heap . My experiments on the same have proved otherwise .
4
2912
by: Sunil Varma | last post by:
Hi, Here is a piece of code where the constructor throws an exception. class A { int n; public: A() try{
5
8198
by: Pallav singh | last post by:
How can we justify that initializer list is better in performance than assignment list in constructor of C++ ??
6
3018
by: Taras_96 | last post by:
Hi everyone, The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 states that: "Consider the following constructor that initializes member object x_ using an initialization list: Fred::Fred() : x_(whatever) { }. The most common benefit of doing this is improved performance. For example, if the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9026
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.