473,566 Members | 2,908 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 4493
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::setDefC torArgs(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<MyT ype> 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********@gaz eta.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<Sma rtPtr<MyType> > sVector(32);
for (unsigned int i = 0, iMax = sVector.size(); i < iMax; i++)
sVector[i] = MyType::CreateE lement(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_iRefC ount);
}
public:
void AddRef(void) const
{ m_iRefCount++;
}
void Release(void) const
{ assert(m_iRefCo unt);
--_iRefCount;
if (!m_iRefCount)
delete this;
}
SmartPtr<MyType > CreateElement(d ouble _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<MyT ype*> 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:publi c std::vector<T>
{ public:
PtrVector(unsig ned int _iSize = 0)
:std::vector(_i Size)
{
}
~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
9675
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 call that in another page so that i get a random image the page is http://2006ymcanationals.com/index.php using <img
1
2307
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 static and non-static class member function? 4. Could it be possible to specify different calling convention for each "global" or each class member...
6
1962
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 parameter.and no of parameter is calling function can be anything. for example.suppose my function is function2. then when i call function1(int i ,char...
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 from the JavaScript routine. It doesn't matter whether the scripts are in external files, or embedded in the HTML document, so long as the VBScript is...
2
4039
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 edit. I have several programs to add this to and I thought of adding a common subroutine versus adding duplicate code would be easier to maintain. The...
5
2209
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 nitty gritty but its necessary for my program. thanks dave
4
6306
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 Code: Public Sub VBProcedure() .. ..
11
3403
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 vtable pointer is made use of.. eg. class one {
4
4116
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 Refrence to my Windows Forms project using VS2008. I now want to allow non WCF clients to use the service. How can I do this? Thanks,
0
7888
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. ...
0
8108
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...
0
7951
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...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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...
0
5213
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
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
0
925
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...

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.