alroirm
03-04-2007, 11:32 PM
Hi all,
I'm working on a program. One of the things it has to do is to load a datafile into an NRMat. At first, i tried to write a derived class with a new constructor and the datafile as an argument; This was silly, because in C++ the base class constructor are called first, so there's no way to do this because i don't know the size of the datafile before reading it into the derived class constructor (I'm just learning C++ for this programs, no previous knownledge of it). The solution for me was to make a template function which reads the datafile and returns an NRMat object; this worked because of the copy '=' operator inside the NRMat class.
All of this makes me think the following: In C++ the '=' operator is NEVER inherited, and it is the only method provided for extending the bounds of an NRMat; so, this means that one can't never make derived classes from NRMat (or NRVec too) if planning to hot-extend the bounds of the matrix.
Why wasn't the design decision to provide protected 'realloc' (new and delete based) methods for those classes, and use that method/s for the '=' operator? Protected members are inherited, so this permits writting a derived class which implements the '=' operator, as well as like in my case, writting derived classes which needs to know things before fixing the bounds of the matrix.
Is this accurate? Am i wrong with something (remember, i do not know C++ at all, i'm an experienced C coder, but nothing about c++)?
Thanks,
Roi Rodriguez
I'm working on a program. One of the things it has to do is to load a datafile into an NRMat. At first, i tried to write a derived class with a new constructor and the datafile as an argument; This was silly, because in C++ the base class constructor are called first, so there's no way to do this because i don't know the size of the datafile before reading it into the derived class constructor (I'm just learning C++ for this programs, no previous knownledge of it). The solution for me was to make a template function which reads the datafile and returns an NRMat object; this worked because of the copy '=' operator inside the NRMat class.
All of this makes me think the following: In C++ the '=' operator is NEVER inherited, and it is the only method provided for extending the bounds of an NRMat; so, this means that one can't never make derived classes from NRMat (or NRVec too) if planning to hot-extend the bounds of the matrix.
Why wasn't the design decision to provide protected 'realloc' (new and delete based) methods for those classes, and use that method/s for the '=' operator? Protected members are inherited, so this permits writting a derived class which implements the '=' operator, as well as like in my case, writting derived classes which needs to know things before fixing the bounds of the matrix.
Is this accurate? Am i wrong with something (remember, i do not know C++ at all, i'm an experienced C coder, but nothing about c++)?
Thanks,
Roi Rodriguez