By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,795 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,795 IT Pros & Developers. It's quick & easy.

construction initialization list

P: n/a
I have a Matrix class derived from an Array class as followed:
Matrix(int nM, int nN)
:Array<T>(nM*nN),mnM(nM),mnN(nN)
{
}
However, I dont want to call Array constructor if nM!=nN. In other
words, I dont want to use the initialization list.
Regards

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
bl**********@gmail.com wrote:
I have a Matrix class derived from an Array class as followed:
Matrix(int nM, int nN)
:Array<T>(nM*nN),mnM(nM),mnN(nN)
{
}
However, I dont want to call Array constructor if nM!=nN. In other
words, I dont want to use the initialization list.


OK. Now we know what you *don't* want. What *do* you want [instead]?
Jul 23 '05 #2

P: n/a
My question is is there another way to call Array constructor without
using the described initialization list ?

Jul 23 '05 #3

P: n/a
bl**********@gmail.com wrote:
My question is is there another way to call Array constructor without
using the described initialization list ?


If you don't put it into the initialiser list, it will be
default-initialised. You can always try doing

:Array<T>(nM == nN ? nM*nN : 1),mnM(nM),mnN(nN)
{
// do something about nM not being equal nN
}

V
Jul 23 '05 #4

P: n/a
Actually I want to do something along the line of
Matrix(int nM,int nN)
:
{
if (nM==nN)
{
call Array constructor to alllocate memory of size nM*nN
mnM=nM;
mnN=nN;
}
else
throw exception

Jul 23 '05 #5

P: n/a
bl**********@gmail.com wrote:
Actually I want to do something along the line of
Matrix(int nM,int nN)
:
{
if (nM==nN)
{
call Array constructor to alllocate memory of size nM*nN
You cannot call a constructor. The only option you have is to do
all construction in the initialiser list.
mnM=nM;
mnN=nN;
}
else
throw exception


Do what I showed (allocate an array of 1 element), and inside
the body throw, it should call the d-tor of the array of 1 element
and everything would be fine.
Jul 23 '05 #6

P: n/a
Actually my condition check is a little more complicated than a
ternary operator (The nM==nM is just a simplied example). That is why I
need to use the if statement.

Jul 23 '05 #7

P: n/a
bl**********@gmail.com wrote:
Actually I want to do something along the line of
Matrix(int nM,int nN)
:
{
if (nM==nN)
{
call Array constructor to alllocate memory of size nM*nN
mnM=nM;
mnN=nN;
}
else
throw exception


In this particular case you can do literally that (still in the
initializer list)

Matrix(int nM,int nN) :
Array<T>(nM == nN ? nM * nN : throw <your exception>),
mnM(nM), mnN(nN)
{}

There's no other way to do that, unless your 'Array<T>' class provides
facilities for "postponed initialization", i.e. default constructor +
member functions for post-construction memory allocation.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #8

P: n/a
bl**********@gmail.com wrote:
Actually my condition check is a little more complicated than a
ternary operator (The nM==nM is just a simplied example). That is why I
need to use the if statement.


You can do something like that:

class ArrayCheck {
public:
ArrayCheck (int nM, int nN)
{
if (nM != nN)
throw something;
}
};

Matrix(int nM, int nN) :
ArrayCheck (nM, nN),
Array<T>(nM*nN),mnM(nM),mnN(nN)
{
}
--
Salu2
Jul 23 '05 #9

P: n/a
bl**********@gmail.com wrote in news:1113002116.576152.3790
@l41g2000cwc.googlegroups.com:
Actually my condition check is a little more complicated than a
ternary operator (The nM==nM is just a simplied example). That is why I
need to use the if statement.


Hmm... the base class must be completely constructed before the subclass's
constructor body can be entered. Perhaps a factory function might be more
appropriate for what you want to do?
Jul 23 '05 #10

P: n/a
Julián Albo wrote:
...
Actually my condition check is a little more complicated than a
ternary operator (The nM==nM is just a simplied example). That is why I
need to use the if statement.


You can do something like that:

class ArrayCheck {
public:
ArrayCheck (int nM, int nN)
{
if (nM != nN)
throw something;
}
};

Matrix(int nM, int nN) :
ArrayCheck (nM, nN),
Array<T>(nM*nN),mnM(nM),mnN(nN)
{
}


Of course when one does that, one has to remember that the order of
construction is determined by the order of declaration (in your example
- order of base class names in the 'Matrix's base class list), not by
the order of initializers in the initializer list. This means that for
this to work as intended, 'ArrayCheck' shall precede 'Array' in the
'Matrix's base class list.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #11

P: n/a
* Andrey Tarasevich:
bl**********@gmail.com wrote:
Actually I want to do something along the line of
Matrix(int nM,int nN)
:
{
if (nM==nN)
{
call Array constructor to alllocate memory of size nM*nN
mnM=nM;
mnN=nN;
}
else
throw exception

In this particular case you can do literally that (still in the
initializer list)

Matrix(int nM,int nN) :
Array<T>(nM == nN ? nM * nN : throw <your exception>),
mnM(nM), mnN(nN)
{}


I don't understand why

Matrix( std::size_t size )

isn't acceptable.
There's no other way to do that, unless your 'Array<T>' class provides
facilities for "postponed initialization", i.e. default constructor +
member functions for post-construction memory allocation.


As others have remarked, a member or base class that does the checking
is technically a solution.

Although I'd prefer to simply remove the redundancy...

--
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?
Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.