I have a Matrix class derived from an Array class as followed:
Matrix(int nM, int nN)
:Array<T>(nM*nN ),mnM(nM),mnN(n N)
{
}
However, I dont want to call Array constructor if nM!=nN. In other
words, I dont want to use the initialization list.
Regards 11 2080 bl**********@gm ail.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(n N) { } 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]?
My question is is there another way to call Array constructor without
using the described initialization list ? bl**********@gm ail.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
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 bl**********@gm ail.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.
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. bl**********@gm ail.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 bl**********@gm ail.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 bl**********@gm ail.com wrote in news:1113002116 .576152.3790
@l41g2000cwc.go oglegroups.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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jacek Dziedzic |
last post by:
Hi!
A) Why isn't it possible to set a member of the BASE class in
an initialization list of a DERIVED class constructor (except for
'calling' the base constructor from there, of course)? I even
tried prefixing them with BASE:: but to no avail. Still it's
ok when I set them in the construtor body, not the init list.
Does this mean that it's a small advantage of the initialization
inside the constructor over initialization in the init...
|
by: trying_to_learn |
last post by:
i am on the chapter on copy construction in C++
in the code (see below), the author says if u pass the object by value
as in HowMany h2 = f(h); ....then a bitwise object is created w/o
calling the constructor of the class. However he says when we leave
scope of function HowMany f(HowMany x) then the destructor is called.
why this inconsistency?. Accdng to me even the destructor should *not*
be called. i can understand that bit wise copy...
|
by: Jakob Bieling |
last post by:
Hi,
I am aware of the fact, that the order of construction of global
objects is unspecified. But I am in a situation, where I need to
guarantee, that one object is created before others (not all others,
just all objects of a specified type). I came up with the following
solution, but I am not sure, if it does solve my problem:
class first
{
|
by: Anton Pervukhin |
last post by:
Hi everybody!
I have a small problem regarding the initialization of pointer to the
file stream under some conditions.
Imagine a class which has a pointer to output file stream and some
additinional methods to deal with it, i.e. open/close/write:
classA {
private:
const std::auto_ptr<std::ofstream> filePtr;
|
by: anongroupaccount |
last post by:
class CustomType
{
public:
CustomType(){_i = 0;}
CustomType(int i) : _i(i) {}
private:
int _i;
};
class MyClass
| |
by: toton |
last post by:
Hi,
I can initialize an array of class with a specific class as,
class Test{
public:
Test(int){}
};
Test x = {Test(3),Test(6)}; using array initialization list. (Note
Test do NOT have a default ctor).
Is it possible to do so in the class parameter initialization using
specific ctor?
|
by: BeautifulMind |
last post by:
In case of inheritence the order of execution of constructors is in
the order of derivation and order of destructor execution is in
reverse order of derivation.
Is this case also true in case class is derived as virtual?
How does the order of construction/destruction is impacted if the base
class is derived as virtual or non virtual? just see the example
below.
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and value-initialization. I
think default-init calls default constructor for class objects and
sets garbage values to PODs. Value-init also calls default
constructor for class objects and sets 0s to POD types. This is what
I've learned from the books (especially...
|
by: JohnQ |
last post by:
The way I understand the startup of a C++ program is:
A.) The stuff that happens before the entry point.
B.) The stuff that happens between the entry point and the calling of
main().
C.) main().
So, if the above is OK, does static initialization occur during A or B? What
happens during A?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |