473,388 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

constructing and initializing a scoped_array

Hi,

I have some old code that I am refactoring to use smart pointers and have
run into a small problem. My original code looks something like this:

class WorkerThread
{
std::map<int, Handler> &HandlerMap;
public:
WorkerThread( std::map<int, Handler> &AHandlerMap )
: HandlerMap( AHandlerMap ) {}
};

WorkerThread *WorkerThreads[MAXTHREADS];

for ( int i=0; i<MAXTHREADS; i++ )
{
WorkerThreads[i] = new WorkerThread( TheHandlerMap );
}
And I think I'd like to change it to use a scoped_array:

boost::scoped_array< WorkerThread > WorkerThreads;

WorkerThreads( new WorkerThread[/*...*/] )

Unfoortunately, the WorkerThread class does not have a default constructor,
and as such, the compiler does not allow me to cosntruct a scoped_array. So
maybe a scoped_array isn't the way to go, but it seemed like most obvious
choice. What would be an appropriate solution?

Thanks,

- Dennis
Jun 1 '06 #1
9 8024
Dennis Jones wrote:
I have some old code that I am refactoring to use smart pointers and have
run into a small problem. My original code looks something like this:

class WorkerThread
{
std::map<int, Handler> &HandlerMap;
public:
WorkerThread( std::map<int, Handler> &AHandlerMap )
: HandlerMap( AHandlerMap ) {}
};

WorkerThread *WorkerThreads[MAXTHREADS];

for ( int i=0; i<MAXTHREADS; i++ )
{
WorkerThreads[i] = new WorkerThread( TheHandlerMap );
}
And I think I'd like to change it to use a scoped_array:

boost::scoped_array< WorkerThread > WorkerThreads;

WorkerThreads( new WorkerThread[/*...*/] )

Unfoortunately, the WorkerThread class does not have a default constructor,
and as such, the compiler does not allow me to cosntruct a scoped_array. So
maybe a scoped_array isn't the way to go, but it seemed like most obvious
choice. What would be an appropriate solution?


Perhaps I missed something, but what about a std::vector?
Jonathan

Jun 1 '06 #2
Dennis Jones wrote:
I have some old code that I am refactoring to use smart pointers and
have run into a small problem. My original code looks something like
this:

class WorkerThread
{
std::map<int, Handler> &HandlerMap;
public:
WorkerThread( std::map<int, Handler> &AHandlerMap )
: HandlerMap( AHandlerMap ) {}
};

WorkerThread *WorkerThreads[MAXTHREADS];

for ( int i=0; i<MAXTHREADS; i++ )
{
WorkerThreads[i] = new WorkerThread( TheHandlerMap );
}
And I think I'd like to change it to use a scoped_array:

boost::scoped_array< WorkerThread > WorkerThreads;

WorkerThreads( new WorkerThread[/*...*/] )

Unfoortunately, the WorkerThread class does not have a default
constructor, and as such, the compiler does not allow me to cosntruct
a scoped_array. So maybe a scoped_array isn't the way to go, but it
seemed like most obvious choice. What would be an appropriate
solution?


I have no idea what 'scoped_array' is (it's not part of the Standard
Library, AFAICT), but can you tell me what's inaproppriate about, say,
'std::vector'?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 1 '06 #3

Victor Bazarov wrote:
Dennis Jones wrote:
I have some old code that I am refactoring to use smart pointers and
have run into a small problem. My original code looks something like
this:

class WorkerThread
{
std::map<int, Handler> &HandlerMap;
public:
WorkerThread( std::map<int, Handler> &AHandlerMap )
: HandlerMap( AHandlerMap ) {}
};

WorkerThread *WorkerThreads[MAXTHREADS];

for ( int i=0; i<MAXTHREADS; i++ )
{
WorkerThreads[i] = new WorkerThread( TheHandlerMap );
}
And I think I'd like to change it to use a scoped_array:

boost::scoped_array< WorkerThread > WorkerThreads;

WorkerThreads( new WorkerThread[/*...*/] )

Unfoortunately, the WorkerThread class does not have a default
constructor, and as such, the compiler does not allow me to cosntruct
a scoped_array. So maybe a scoped_array isn't the way to go, but it
seemed like most obvious choice. What would be an appropriate
solution?


I have no idea what 'scoped_array' is (it's not part of the Standard
Library, AFAICT), but can you tell me what's inaproppriate about, say,
'std::vector'?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

I think that the main problem with vector will be the fact that he is
allocating those objects on the heap - they are pointers and vector is
not so great for those cases, in any case he will run in to the same
problem with vector since vector need the stored object to have default
ctor.
But why not to use boost::ptr_map? it was designed for holding pointers
which is what you code is doing. Then you don't need shared_ptr. Read
the docs for this.

Jun 1 '06 #4
bo*******@yahoo.com wrote:
vector need the stored object to have default ctor.


Why do you say this?
#include <iostream>
#include <vector>

class NoDefaultConstructor {
public:
NoDefaultConstructor(int i) : i_(i) { }

private:
NoDefaultConstructor();
int i_;
};

int main()
{
std::vector<NoDefaultConstructor> v;
v.push_back(NoDefaultConstructor(42));
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 1 '06 #5
Marcus Kwok wrote:
bo*******@yahoo.com wrote:
vector need the stored object to have default ctor.


Why do you say this?
#include <iostream>
#include <vector>

class NoDefaultConstructor {
public:
NoDefaultConstructor(int i) : i_(i) { }

private:
NoDefaultConstructor();
int i_;
};

int main()
{
std::vector<NoDefaultConstructor> v;
v.push_back(NoDefaultConstructor(42));
}


Now add

v.resize(27);

after the push_back call.

Jun 1 '06 #6
red floyd wrote:
Marcus Kwok wrote:
bo*******@yahoo.com wrote:
vector need the stored object to have default ctor.


Why do you say this?
#include <iostream>
#include <vector>

class NoDefaultConstructor {
public:
NoDefaultConstructor(int i) : i_(i) { }

private:
NoDefaultConstructor();
int i_;
};

int main()
{
std::vector<NoDefaultConstructor> v;
v.push_back(NoDefaultConstructor(42));
}


Now add

v.resize(27);

after the push_back call.


Well, duh. What if you don't? What if you limit yourself
to pushing/popping and accessing?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 1 '06 #7
>> bo*******@yahoo.com wrote:
vector need the stored object to have default ctor.
Marcus Kwok wrote:
Why do you say this?

#include <iostream>
#include <vector>

class NoDefaultConstructor {
public:
NoDefaultConstructor(int i) : i_(i) { }

private:
NoDefaultConstructor();
int i_;
};

int main()
{
std::vector<NoDefaultConstructor> v;
v.push_back(NoDefaultConstructor(42));
}

red floyd <no*****@here.dude> wrote: Now add
v.resize(27);
after the push_back call.
Well of course that won't work, just like

NoDefaultConstructor ndc;

won't work.
Given the code from the original post:
WorkerThread *WorkerThreads[MAXTHREADS];

for ( int i=0; i<MAXTHREADS; i++ )
{
WorkerThreads[i] = new WorkerThread( TheHandlerMap );
}


Now using std::vector:

std::vector<WorkerThread*> WorkerThreads;
WorkerThreads.reserve(MAXTHREADS);

for (int i = 0; i < MAXTHREADS; ++i) {
WorkerThreads.push_back(new WorkerThread(TheHandlerMap));
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 1 '06 #8

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:e5**********@news.datemas.de...
I have no idea what 'scoped_array' is (it's not part of the Standard
Library, AFAICT),
Surely you've heard of Boost? Many of its libraries are being considered
for inclusion in the standard.

but can you tell me what's inaproppriate about, say,
'std::vector'?


Actually, nothing...it's a perfectly acceptable solution. e.g.:

std::vector< boost::scoped_ptr<WorkerThread> > WorkerThreads;

WorkerThreads.reserve( cMaxThreads );
for ( int i=0; i<cMaxThreads; ++i )
{
WorkerThreads.push_back( boost::scoped_ptr<WorkerThread>( new
TWorkerThread( TheHandlerMap ) ) );
}

I can't initialize the array in the constructor's initializer list, but
initializing it with a loop in the constructor is only a minor inconvenience
compared to the benefit gained from the use of smart pointers.

Thanks for the 'vector' suggestion,

- Dennis
Jun 2 '06 #9

"Dennis Jones" <no****@nospam.com> wrote in message
news:CUNfg.11754$ho6.1518@trnddc07...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:e5**********@news.datemas.de...
I have no idea what 'scoped_array' is (it's not part of the Standard
Library, AFAICT),


Surely you've heard of Boost? Many of its libraries are being considered
for inclusion in the standard.

but can you tell me what's inaproppriate about, say,
'std::vector'?


Actually, nothing...it's a perfectly acceptable solution. e.g.:

std::vector< boost::scoped_ptr<WorkerThread> > WorkerThreads;


Oops, that should have been 'boost::shared_ptr' (as scoped_ptr's won't
work).

- Dennis
Jun 2 '06 #10

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
14
by: Avi Uziel | last post by:
Hi All, I'm writing a Windows DLL which contain some utility classes. One of my classes is Singleton, therefore contain some static members. I'm using VC6 and linking to the DLL statically. ...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
14
by: Madhav | last post by:
hi all, I was trying the following code on the MS .net compiler: class Item { static int count; public: //static void init() { } Item()
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
5
by: David Mathog | last post by:
I'm looking at a program which stores perl scripts in an array. Each script is stored as a single entry in that array, and the whole set of them live in a single header file (escaped out the wazoo...
38
by: junky_fellow | last post by:
Guys, I was just looking at some code where to initialize an integer with all F's following statement is used; unsigned int i = -1; I want to know if this is the right way of doing the...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
9
by: Mark2012 | last post by:
Hello, I am new to programming with python. I am using the tutorial, "Byte of Python" and am on p. 82. I have come across something very unusual by accident and I was wondering if anybody here could...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.