Re: array initializer-lists for non-POD types?
Thanks.
I found a way to have my cake and eat it. That is to have both a
aggregate initializer sequence and a contructor. It tuned out to be
quite simple. I have a base class that is a proper aggregate and then
have a derived class that just implements a constructor and destructor.
Now i can initilaize the data in the base object in two different ways.
The old and the new.
Here is some example code, do you see anything wrong with it (except
that the 'old way' is bad)?
<CODE>
inline int *GetHandle() {
//return new int(rand());
return new int(123);
}
inline void ReleaseHandle(int *i) {
delete(i);
}
struct Test {
int a_;
int b_;
int c_;
int *someHandle_;
};
struct TestWrapper: public Test {
TestWrapper(int a, int b, int c) {
a_ = a;
b_ = b;
c_ = c;
someHandle_ = GetHandle(); //RAII
}
~TestWrapper() {
ReleaseHandle(someHandle_); //RAII
}
};
inline void DoStuffOnTest( Test &t) {
}
inline void TestProgram() {
/* this is the old way, it uses aggregate initializer sequence and a
later manual initialization of some variables, this is client code so
the syntax cant be changed */
Test oldTest = {6,6,6};
oldTest.someHandle_ = GetHandle();
DoStuffOnTest(oldTest);
ReleaseHandle(oldTest.someHandle_);
/* this is the new way, uses constructor and RAII, but still works
with the old client code (except that now GetHandle() and
ReleaseHandle() dont need to be called) */
TestWrapper newTest(6, 6, 6);
DoStuffOnTest(newTest);
}
</CODE>
Regards Patrik
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]