construction initialization list

B

bluekite2000

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
 
V

Victor Bazarov

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]?
 
B

bluekite2000

My question is is there another way to call Array constructor without
using the described initialization list ?
 
V

Victor Bazarov

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
 
B

bluekite2000

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
 
V

Victor Bazarov

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.
 
B

bluekite2000

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.
 
A

Andrey Tarasevich

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.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

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)
{
}
 
A

Andre Kostur

(e-mail address removed) wrote in @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?
 
A

Andrey Tarasevich

Julián Albo said:
...

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.
 
A

Alf P. Steinbach

* Andrey Tarasevich:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,664
Latest member
RoseannBow

Latest Threads

Top