473,499 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling non-default constructor with new[]

Hi
Short question for which I can't find answer:
I want to initialize the array of objects with the same values.

new MyType[n]; calls default constructor
new MyType(val1, val2); initializes only one element

Is there any way to merge these two things?
Now I have something like this:

p = new MyType[n];
for(int i=0;i<n;i++) p[i].init(val1,val2);

And this is ugly to me...

--
mati
Jun 19 '06 #1
6 4482
mati-006 wrote:
Short question for which I can't find answer:
I want to initialize the array of objects with the same values.

new MyType[n]; calls default constructor
new MyType(val1, val2); initializes only one element

Is there any way to merge these two things?
No, there is no way.
Now I have something like this:

p = new MyType[n];
for(int i=0;i<n;i++) p[i].init(val1,val2);

And this is ugly to me...


I have a pseudo-solution. It's not very nice, but it should work.

class MyType {
static ValType1 arg1;
static ValType2 arg2;
public:
static void setDefCtorArgs(ValType1 a1, ValType2 a2)
{
arg1 = a1; arg2 = a2;
}

MyType(ValType1 a1 = arg1, ValType2 = arg2); // "default"
};

...
MyType::setDefCtorArgs(val1, val2);
p = new MyType[n];

Don't foget to define 'MyType::arg1' and 'MyType::arg2' somewhere.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '06 #2
* mati-006:
Hi
Short question for which I can't find answer:
I want to initialize the array of objects with the same values.

new MyType[n]; calls default constructor
new MyType(val1, val2); initializes only one element

Is there any way to merge these two things?
If MyType is copyable you can use std::vector, like

std::vector<MyType> v( n, MyType( val1, val2 ) );

Now I have something like this:

p = new MyType[n];
for(int i=0;i<n;i++) p[i].init(val1,val2);

And this is ugly to me...


Yes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 19 '06 #3
mati-006 wrote:
...
Short question for which I can't find answer:
I want to initialize the array of objects with the same values.

new MyType[n]; calls default constructor
new MyType(val1, val2); initializes only one element

Is there any way to merge these two things?
No. Meaning that there is no syntax that would 'new'-allocate an array and
invoke non-default constructors for its elements.
Now I have something like this:

p = new MyType[n];
for(int i=0;i<n;i++) p[i].init(val1,val2);

And this is ugly to me...


You can also do

p = new MyType[n];
std::fill_n(p, n, MyType(val1, val2));

assuming that your type's copy assignment operator is implemented appropriately.

--
Best regards,
Andrey Tarasevich
Jun 19 '06 #4
Victor Bazarov posted:
mati-006 wrote:
Short question for which I can't find answer:
I want to initialize the array of objects with the same values.

new MyType[n]; calls default constructor
new MyType(val1, val2); initializes only one element

Is there any way to merge these two things?


No, there is no way.


Which begs the question. . .

Will this be fixed in the next Standard?
--

Frederick Gotham
Jun 19 '06 #5
Frederick Gotham wrote:
Victor Bazarov posted:
mati-006 wrote:
Short question for which I can't find answer:
I want to initialize the array of objects with the same values.

new MyType[n]; calls default constructor
new MyType(val1, val2); initializes only one element

Is there any way to merge these two things?


No, there is no way.


Which begs the question. . .

Will this be fixed in the next Standard?


I doubt it. Arrays need to be revamped significantly and a patch
like that isn't worth the trouble. But do ask in 'comp.std.c++' or
look for proposals in the WG21 documents (the WB21 committee site
is http://www.open-std.org/jtc1/sc22/wg21/)

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

"mati-006" <lo********@gazeta.NOSPAM.pl> wrote in message
news:e7**********@inews.gazeta.pl...
Hi
Short question for which I can't find answer:
I want to initialize the array of objects with the same values.

new MyType[n]; calls default constructor
new MyType(val1, val2); initializes only one element

Is there any way to merge these two things?
Now I have something like this:

p = new MyType[n];
for(int i=0;i<n;i++) p[i].init(val1,val2);

And this is ugly to me...

maybe what you want is this:

std::vector<SmartPtr<MyType> > sVector(32);
for (unsigned int i = 0, iMax = sVector.size(); i < iMax; i++)
sVector[i] = MyType::CreateElement(val1, val2);
template<class T>
class SmartPtr
{ private:
T *m_p;
public:
SmartPtr(void)
:m_p(0)
{
}
SmartPtr(T *_p)
:m_p(_p)
{ if (m_p)
m_p->AddRef();
}
SmartPtr(const SmartPtr<T> &_r)
:m_p(_r.m_p)
{ if (m_p)
m_p->AddRef();
}
~SmartPtr(void)
{ if (m_p)
m_p->Release();
}
operator=(const SmartPtr &_r)
{ if (m_p)
m_p->Release();
if (m_p = _r.m_p)
m_p->AddRef();
return *this;
}
operator T *(void) const
{ return m_p;
}
};

class MyType
{ private:
mutable unsigned int m_iRefCount;
double m_dVal1, m_dVal2;
MyType(void)
:m_iRefCount(0)
{
};
MyType(double _dVal1, double _dVal2)
:m_iRefCount(0),
m_dVal1(_dVal1),
m_dVal2(_dVal2)
{
}
virtual ~MyType(void)
{ assert(!m_iRefCount);
}
public:
void AddRef(void) const
{ m_iRefCount++;
}
void Release(void) const
{ assert(m_iRefCount);
--_iRefCount;
if (!m_iRefCount)
delete this;
}
SmartPtr<MyType> CreateElement(double _dVal1, double _dVal2)
{ return new MyType(_dVal1, _dVal2);
}
};

The class SmartPtr is a general class.
The methods AddRef() and Release() could be part of a general base class
(the destructor should be virtual).
Of course

std::vector<MyType*> sVector(32);

would also work -- but in this case I would rather write a derived class
from std::vector which cleans up the pointers inside the destructor, e.g.

template<class T>
class PtrVector:public std::vector<T>
{ public:
PtrVector(unsigned int _iSize = 0)
:std::vector(_iSize)
{
}
~PtrVector(void)
{ for (unsigned int i = 0, iMax = size(); i < iMax; i++)
delete (*this)[i];
}
};

Jun 20 '06 #7

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

Similar topics

12
9667
by: bhennon | last post by:
Hey all, I have a small php script that calls a random image at the following page. http://www.2006ymcanationals.com/random.php IT WORKS IF I go directly to the above link. I am trying to...
1
2300
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between...
6
1956
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
1
521
by: Ian Sedwell | last post by:
Hi guys Many thanks to all who replied to my original question. Actually, it's dead easy and the way I was doing it was correct the first time. You do indeed simply call the VBScript routine...
2
4028
by: Giulio Belrango | last post by:
Hi I need someones help I'm working in an IBM 390 batch environment. What I'm trying to do is to call from a non DB2 COBOL program a DB2 COBOL program that will access a table and perform an...
5
2204
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
4
6298
by: Martin Feuersteiner | last post by:
Dear Group I'm using VB to write an aspnet application. I would like to call a javascript function from within a VB Sub or VB Function, is it possible? My code is something like this: VB...
11
3393
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
4
4102
by: Jeronimo Bertran | last post by:
Hello, I have a WCF service that I am using to upload files to a server by using the streamed transfer method. I am currently calling the service from a WCF client. All I did was add a Service...
0
7134
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
7014
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
7180
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
7229
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...
1
6905
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...
0
7395
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...
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
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...

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.