How do you define 2D arrays using New Operator?

M

Mike Wahler

template < typename T >
class Matrix {
void check_limits( unsigned h, unsigned v ) const {
if ( h >= h_limit || v >= v_limit )
throw std::eek:ut_of_range( "Matrix" );
}
std::deque<T> buffer;
unsigned h_limit, v_limit;
public:
Matrix():
h_limit( 0 ),
v_limit( 0 ) { }
Matrix( unsigned h, unsigned v ):
buffer( h * v ),
h_limit( h ),
v_limit( v ) { }
T& cell_at( unsigned h, unsigned v ) {
check_limits();
return buffer[ h_limit * h + v ];
}
const T& cell_at( unsigned h, unsigned v ) const {
check_limits();
return buffer[ h_limit * h + v ];
}
};
use it like this:

Matrix<float> m( 100, 100 )
assert( m.cell_at( 12, 5 ) == 0 );
m.cell_at( 12, 5 ) = 9;
assert( m.cell_at( 12, 5 ) == 9 );

Which method(s) are you having trouble understanding?

check_limits(unsigned h, unsigned v): checks to make sure that h & v
represent a cell in the Matrix.

Matrix(): creates an empty (0 x 0) matrix.

Matrix(unsigned h, unsigned v): creates and initializes all the elements
in an h x v matrix.

cell_at(unsigned h, unsigned v): returns a modifiable reference to a
cell in the matrix.

cell_at(unsigned h, unsigned v) const: returns an unmodifiable reference
to a cell in the matrix.

The matrix is actually held in an array (a deque in this case, but it
could as easily be held in a vector.) Something like this:

00 10 20
01 11 21
02 12 22

is held in the array like this:

00 10 20 01 11 21 02 12 22
Thanks again for your explanation.

I'm having trouble understanding:

Where does the function "cell_at" come from.

It came from Daniel, he wrote it. I suspect he used
the name 'cell_at' to mimic the name of a similar
member function from the standard class 'std::vector'.
The vector class has a subscript operator ( [] ) that
works just like with an array, but also like an array
is not bounds checked. It also has a element-access
function which does do bounds checking, called 'at()'.
So he came up with 'cell_at()' for the name.
I'm not to familiar with the use of "std::deque<T> buffer;"

'std::queue' is one of several 'container' classes provided
by the C++ standard library. It's a templated class (as
are all the containers), with the template parameter 'T'
specifying the actual type of the elements stored in it.

e.g.

where are some good
examples?

I don't know of any online, but a good textbook should have some.
The most recommended book on the standard library is:
www.josuttis.com/libbook I *highly* recommend it.
I checked the help section of vc++6.0 but the deque examples were not
the same.

Why do h and v have to be unsigned?

They don't *have* to be, but it makes sense for them to be.
A signed type would allow for negative values (and only has
half the value range of the corresponding unsigned type).
But what would a negative size or index *mean*? Also note that
'h' and 'v' shouldn't really be just any unsigned type, but type
'size_t', which is an (implementation-defined) unsigned type which
is guaranteed to be able to hold the largest possible sized object
(or the largest number of one-byte objects). Actually it should
have the same type as the container class ('deque' in this case)'s
'size_type' type. But I suppose allowing for that would make the
code even harder for you to follow. :)
Why is the "const {" there in
"void check_limits( unsigned h, unsigned v ) const{"
const T& cell_at( unsigned h, unsigned v ) const {

Look up 'const member function'. The 'const' after the
parameter list is a 'promise' to the compiler that the
function will not modify the object for which it was called.
If you write code that tries to do so, you'll get a compile
error.
If the class is defined outside the function

The class is defined outside any function (as it should be).
is "buffer" static when the
function is exited?

What function? Anyway, an object will have static storage duration
in two cases:

1. It's defined at file scope (outside any function or class)
2. It's specifically qualified with the 'static' keyword.

-Mike
 
D

Dennis

Thanks Mike that helped and I learned a lot.

Dennis

Mike Wahler said:
template < typename T >
class Matrix {
void check_limits( unsigned h, unsigned v ) const {
if ( h >= h_limit || v >= v_limit )
throw std::eek:ut_of_range( "Matrix" );
}
std::deque<T> buffer;
unsigned h_limit, v_limit;
public:
Matrix():
h_limit( 0 ),
v_limit( 0 ) { }
Matrix( unsigned h, unsigned v ):
buffer( h * v ),
h_limit( h ),
v_limit( v ) { }
T& cell_at( unsigned h, unsigned v ) {
check_limits();
return buffer[ h_limit * h + v ];
}
const T& cell_at( unsigned h, unsigned v ) const {
check_limits();
return buffer[ h_limit * h + v ];
}
};
use it like this:

Matrix<float> m( 100, 100 )
assert( m.cell_at( 12, 5 ) == 0 );
m.cell_at( 12, 5 ) = 9;
assert( m.cell_at( 12, 5 ) == 9 );

Which method(s) are you having trouble understanding?

check_limits(unsigned h, unsigned v): checks to make sure that h & v
represent a cell in the Matrix.

Matrix(): creates an empty (0 x 0) matrix.

Matrix(unsigned h, unsigned v): creates and initializes all the elements
in an h x v matrix.

cell_at(unsigned h, unsigned v): returns a modifiable reference to a
cell in the matrix.

cell_at(unsigned h, unsigned v) const: returns an unmodifiable reference
to a cell in the matrix.

The matrix is actually held in an array (a deque in this case, but it
could as easily be held in a vector.) Something like this:

00 10 20
01 11 21
02 12 22

is held in the array like this:

00 10 20 01 11 21 02 12 22
Thanks again for your explanation.

I'm having trouble understanding:

Where does the function "cell_at" come from.

It came from Daniel, he wrote it. I suspect he used
the name 'cell_at' to mimic the name of a similar
member function from the standard class 'std::vector'.
The vector class has a subscript operator ( [] ) that
works just like with an array, but also like an array
is not bounds checked. It also has a element-access
function which does do bounds checking, called 'at()'.
So he came up with 'cell_at()' for the name.
I'm not to familiar with the use of "std::deque<T> buffer;"

'std::queue' is one of several 'container' classes provided
by the C++ standard library. It's a templated class (as
are all the containers), with the template parameter 'T'
specifying the actual type of the elements stored in it.

e.g.

where are some good
examples?

I don't know of any online, but a good textbook should have some.
The most recommended book on the standard library is:
www.josuttis.com/libbook I *highly* recommend it.
I checked the help section of vc++6.0 but the deque examples were not
the same.

Why do h and v have to be unsigned?

They don't *have* to be, but it makes sense for them to be.
A signed type would allow for negative values (and only has
half the value range of the corresponding unsigned type).
But what would a negative size or index *mean*? Also note that
'h' and 'v' shouldn't really be just any unsigned type, but type
'size_t', which is an (implementation-defined) unsigned type which
is guaranteed to be able to hold the largest possible sized object
(or the largest number of one-byte objects). Actually it should
have the same type as the container class ('deque' in this case)'s
'size_type' type. But I suppose allowing for that would make the
code even harder for you to follow. :)
Why is the "const {" there in
"void check_limits( unsigned h, unsigned v ) const{"
const T& cell_at( unsigned h, unsigned v ) const {

Look up 'const member function'. The 'const' after the
parameter list is a 'promise' to the compiler that the
function will not modify the object for which it was called.
If you write code that tries to do so, you'll get a compile
error.
If the class is defined outside the function

The class is defined outside any function (as it should be).
is "buffer" static when the
function is exited?

What function? Anyway, an object will have static storage duration
in two cases:

1. It's defined at file scope (outside any function or class)
2. It's specifically qualified with the 'static' keyword.

-Mike
 
D

Daniel T.

template < typename T >
class Matrix {
void check_limits( unsigned h, unsigned v ) const {
if ( h >= h_limit || v >= v_limit )
throw std::eek:ut_of_range( "Matrix" );
}
std::deque<T> buffer;
unsigned h_limit, v_limit;
public:
Matrix():
h_limit( 0 ),
v_limit( 0 ) { }
Matrix( unsigned h, unsigned v ):
buffer( h * v ),
h_limit( h ),
v_limit( v ) { }
T& cell_at( unsigned h, unsigned v ) {
check_limits();
return buffer[ h_limit * h + v ];
}
const T& cell_at( unsigned h, unsigned v ) const {
check_limits();
return buffer[ h_limit * h + v ];
}
};
use it like this:

Matrix<float> m( 100, 100 )
assert( m.cell_at( 12, 5 ) == 0 );
m.cell_at( 12, 5 ) = 9;
assert( m.cell_at( 12, 5 ) == 9 );

Which method(s) are you having trouble understanding?

check_limits(unsigned h, unsigned v): checks to make sure that h & v
represent a cell in the Matrix.

Matrix(): creates an empty (0 x 0) matrix.

Matrix(unsigned h, unsigned v): creates and initializes all the elements
in an h x v matrix.

cell_at(unsigned h, unsigned v): returns a modifiable reference to a
cell in the matrix.

cell_at(unsigned h, unsigned v) const: returns an unmodifiable reference
to a cell in the matrix.

The matrix is actually held in an array (a deque in this case, but it
could as easily be held in a vector.) Something like this:

00 10 20
01 11 21
02 12 22

is held in the array like this:

00 10 20 01 11 21 02 12 22
Thanks again for your explanation.

I'm having trouble understanding:

Where does the function "cell_at" come from.[/QUOTE]

It sounded like a good name, you can call it something different if you
want. Origionally, I used operator(), but I thought cell_at would be
easier for a beginner understand.

I'm not to familiar with the use of "std::deque<T> buffer;" where are some
good
examples? I checked the help section of vc++6.0 but the deque examples were
not
the same.

<http://www.sgi.com/tech/stl/Deque.html>
"""
A deque is very much like a vector: like vector, it is a sequence that
supports random access to elements, constant time insertion and removal
of elements at the end of the sequence, and linear time insertion and
removal of elements in the middle.

The main way in which deque differs from vector is that deque also
supports constant time insertion and removal of elements at the
beginning of the sequence. Additionally, deque does not have any member
functions analogous to vector's capacity() and reserve(), and does not
provide any of the guarantees on iterator validity that are associated
with those member functions.
"""

You might wonder why I used a deque rather than a vector. I did it
because deque's memory doesn't have to be contigious which usually works
better for very large sequences.

Why do h and v have to be unsigned?

What would it mean to create a Matrix that is -5 elements wide?

Why is the "const {" there in
"void check_limits( unsigned h, unsigned v ) const{"
const T& cell_at( unsigned h, unsigned v ) const {

That means that 'this' is a const pointer rather than a normal pointer.
It allows those functions to be called on const objects. For example:

void foo( const Matrix<double>& matrix ) {
double i = matrix.cell_at( 5, 3 );
// the above will call 'cell_at(unsigned, unsigned)const'
// because the matrix cannot be modified.
}

If the class is defined outside the function is "buffer" static when the
function is exited?

The class must be defined outside of any function. Creation of Matrix
objects follows the same rules as any other object or int for that
matter.
 
D

Dennis

* (e-mail address removed):

I doubt that.
Not sure what you mean, but if the DLL is not dynamically unloaded then
there is no problem (you have ordinary C++ rules of destruction in opposite
order of construction, and a std::vector will do), and if it is dynamically
unloaded then you can either go the OS/compiler-specific route (ask about
that in an appropriate newsgroup), or you can inflict some design on the
code, e.g. giving client code responsibility for allocation & deallocation.

This IS a C++ lang question and here's why:

Either the C++ compiler cleans up global and static memory created by a function
in a standard DLL when the DLL is unloaded or not!

Simple question that requres a C++ lang answer.
 
A

Alf P. Steinbach

* (e-mail address removed):
This IS a C++ lang question and here's why:

Either the C++ compiler cleans up global and static memory created by a function
in a standard DLL when the DLL is unloaded or not!

Simple question that requres a C++ lang answer.

It would be nice if the C++ standard acknowledged the existence of
dynamic libraries, threads, and goddamit, linkers.

But it doesn't.

So there's no C++ answer, and the question is not a C++ one except if you
formulate it as a language change proposal and post it in relevant group.
 
M

Mike Wahler

deallocation.

This IS a C++ lang question and here's why:

Either the C++ compiler cleans up global and static memory created by a function
in a standard DLL when the DLL is unloaded or not!

Simple question that requres a C++ lang answer.

Here's why this is NOT a C++ language question:
DLL's are NOT defined by C++. So the language has
NOTHING to say about how a DLL behaves, or affects
applications that use one.

-Mike
 
J

Jon Bell

This IS a C++ lang question

No, it's not, it's a compiler- and operating-system-specific question.
and here's why: Either the C++ compiler

.... that you are using, under your particular operating system, ...
cleans up global and static
memory created by a function >in a standard DLL when the DLL is unloaded
or not!

What is a "standard DLL", and how can I use one under Unix, MacOS, etc.?
 
D

Dennis

It would be nice if the C++ standard acknowledged the existence of
dynamic libraries, threads, and goddamit, linkers.

But it doesn't.

So there's no C++ answer, and the question is not a C++ one except if you
formulate it as a language change proposal and post it in relevant group.

Well let's see. Maybe this is a VC++ lang question?

When I load my VC++ gui, one of the selections it asks me when I click on "New"
is do I want to make a Win32 Dynamic-link Library Project.

If I answer yes then it allows my to set up various C++, .h, etc modules and
then compiles and builds the C++ DLL. I'm sure other C++ compiliers are similar
in their construction of a C++ language DLL.

No I don't get why this is not a C++ question. Granted that different C++
compiliers handle Global memory release generated by functions in a called DLL
differently. Doesn't the C++ Lang define how a DLL is loaded and freed? If so
then it defines what it does when it frees the DLL. I'll bet that MS VC++ has
the majority of C++ programmers in this ng so this question can be answered
here.

Dennis
 
M

Mike Wahler

Well let's see. Maybe this is a VC++ lang question?

VC++ is not a language, it's an implementation of C++,
which also includes many extensions for working with
Windows. Those extensions (such as keywords for DLL
support) are *not* part of the ISO standard C++ language,
which is the only topic here.
When I load my VC++ gui, one of the selections it asks me when I click on "New"
is do I want to make a Win32 Dynamic-link Library Project.

If I answer yes then it allows my to set up various C++, .h, etc modules and
then compiles and builds the C++ DLL. I'm sure other C++ compiliers are similar
in their construction of a C++ language DLL.

Try asking in a VC++ group. Since you're asking about how to
build from the IDE, the group 'microsoft.public.vc.ide_general'
springs to mind. Another great place for information about
Microsoft products and technologies is www.msdn.microsoft.com
(which btw has a list of newsgroups which discuss their products).
Perhaps if you'd searched for a VC++ group instead of continuing
to insist that VC++ is topical here when it's not, you'd have
solved your problem by now.
No I don't get why this is not a C++ question.

Because you're asking about a particular implementation. Only
the (ISO standard) *language* is topical here. Please see:
http://www.slack.net/~shiva/welcome.txt
... which explains this very clearly.
Granted that different C++
compiliers

There are many different C++ implementations, yes. And many of
them provide 'extensions' for working with their target platforms.
Here, we only discuss what they have in common: the *language*
itself.
handle Global memory release

The C++ language has no notion of 'Global memory' or 'Global memory
release'.
generated by functions in a called DLL
differently. Doesn't the C++ Lang define how a DLL is loaded and freed?

Absolutely not. C++ has no notion of DLL's or shared libraries. DLL's
are a Microsoft invention, for use with the Windows operating system.
ISO standard C++ is a platform-independent language.

It does not.
then it defines what it does when it frees the DLL.

It does not.
I'll bet that MS VC++ has
the majority of C++ programmers in this ng

I seriously doubt that. I cannot know for sure (for example we cannot
know how many folks read here who never post), but were I to hazard
a guess, I'd suspect that g++ was the most widely used by readers/
posters here. But suppose that most folks here do indeed use VC++.
That still doesn't make it topical here, any more than would Ford
automobiles, even if we all drove one.
so this question can be answered
here.

It could be, but anyone who wishes to observe this group's
topicality will not answer. More than one of use has already
done you the courtesy of suggesting a more appropriate group.
You respond by insisting that nontopical material is topical.
That's hardly productive or courteous.


-Mike
 
M

Mabden

Mike Wahler said:
I seriously doubt that. I cannot know for sure (for example we cannot
know how many folks read here who never post), but were I to hazard
a guess, I'd suspect that g++ was the most widely used by readers/
posters here. But suppose that most folks here do indeed use VC++.
That still doesn't make it topical here, any more than would Ford
automobiles, even if we all drove one.

I drive a Ford F150 truck. Is that considered an automobile? And, if so,
is it still OT?
 
D

Dennis

Mike Wahler said:
VC++ is not a language, it's an implementation of C++,
which also includes many extensions for working with
Windows. Those extensions (such as keywords for DLL
support) are *not* part of the ISO standard C++ language,
which is the only topic here.


Because you're asking about a particular implementation. Only
the (ISO standard) *language* is topical here. Please see:
http://www.slack.net/~shiva/welcome.txt
.. which explains this very clearly.
There are many different C++ implementations, yes. And many of
them provide 'extensions' for working with their target platforms.
Here, we only discuss what they have in common: the *language*
itself.

Ok thanks Mike. You've finally convinced me that this ng is NOT the forum to
answer my DLL global memory question.

I apologize to all members of this ng for taking up your time and I appreciate
your efforts and responses.

Dennis
 
D

Dennis

Daniel T. said:
template < typename T >
class Matrix {
void check_limits( unsigned h, unsigned v ) const {
if ( h >= h_limit || v >= v_limit )
throw std::eek:ut_of_range( "Matrix" );
}
std::deque said:
unsigned h_limit, v_limit;
public:
Matrix():
h_limit( 0 ),
v_limit( 0 ) { }
Matrix( unsigned h, unsigned v ):
buffer( h * v ),
h_limit( h ),
v_limit( v ) { }
T& cell_at( unsigned h, unsigned v ) {
check_limits();
return buffer[ h_limit * h + v ];
}
const T& cell_at( unsigned h, unsigned v ) const {
check_limits();
return buffer[ h_limit * h + v ];
}
};
Which method(s) are you having trouble understanding?
Daniel it's been a couple of months since you sent this but I had a few more
questions about this Matrix class.

Why when making the function cell_at in the Matrix class why do you define two
cell_at functions? That is why do you need to have:
T& cell_at( unsigned h, unsigned v ) {
check_limits();
return buffer[ h_limit * h + v ];
}
and
const T& cell_at( unsigned h, unsigned v ) const {
check_limits();
return buffer[ h_limit * h + v ];
}
?

Also if I wanted to have a resize ability would I add?:

T& resize( unsigned h, unsigned v ) {
buffer.resize( h * v );
h_limit= h ;
v_limit= v ;
}
would I need a "const" definition of this function also?

Thanks for your help in understanding this .

Dennis
 
D

Dennis

Daniel T. said:
template < typename T >
class Matrix {
void check_limits( unsigned h, unsigned v ) const {
if ( h >= h_limit || v >= v_limit )
throw std::eek:ut_of_range( "Matrix" );
}
std::deque said:
unsigned h_limit, v_limit;
public:
Matrix():
h_limit( 0 ),
v_limit( 0 ) { }
Matrix( unsigned h, unsigned v ):
buffer( h * v ),
h_limit( h ),
v_limit( v ) { }
T& cell_at( unsigned h, unsigned v ) {
check_limits();
return buffer[ h_limit * h + v ];
}
const T& cell_at( unsigned h, unsigned v ) const {
check_limits();
return buffer[ h_limit * h + v ];
}
};
Which method(s) are you having trouble understanding?
Daniel it's been a couple of months since you sent this but I had a few more
questions about this Matrix class.

Why when making the function cell_at in the Matrix class why do you define two
cell_at functions? That is why do you need to have:
T& cell_at( unsigned h, unsigned v ) {
check_limits();
return buffer[ h_limit * h + v ];
}
and
const T& cell_at( unsigned h, unsigned v ) const {
check_limits();
return buffer[ h_limit * h + v ];
}
?

Also if I wanted to have a resize ability would I add?:

T& resize( unsigned h, unsigned v ) {
buffer.resize( h * v );
h_limit= h ;
v_limit= v ;
}
Actually the resize function should be

void resize( unsigned h, unsigned v ) {
buffer.resize( h * v );
h_limit= h ;
v_limit= v ;
}

and the Matrix size function should be
long msize( ) { return=buffer.size(); }

Do I need const's for these functions? I don't quite understand the need for
the const in the cell_at and check_limits functions.

Thanks.

Dennis
 
J

Jonathan Mcdougall

template < typename T >
class Matrix {
void check_limits( unsigned h, unsigned v ) const {
if ( h >= h_limit || v >= v_limit )
throw std::eek:ut_of_range( "Matrix" );
}
std::deque<T> buffer;

unsigned h_limit, v_limit;

public:
Matrix():
h_limit( 0 ),
v_limit( 0 ) { }
Matrix( unsigned h, unsigned v ):
buffer( h * v ),
h_limit( h ),
v_limit( v ) { }
T& cell_at( unsigned h, unsigned v ) {
check_limits();
return buffer[ h_limit * h + v ];
}
const T& cell_at( unsigned h, unsigned v ) const {
check_limits();
return buffer[ h_limit * h + v ];
}
};
Which method(s) are you having trouble understanding?

Daniel it's been a couple of months since you sent this but I had a few more
questions about this Matrix class.

Why when making the function cell_at in the Matrix class why do you define two
cell_at functions? That is why do you need to have:
T& cell_at( unsigned h, unsigned v ) {
check_limits();
return buffer[ h_limit * h + v ];
}
and
const T& cell_at( unsigned h, unsigned v ) const {
check_limits();
return buffer[ h_limit * h + v ];
}
?

Because they return a reference to a private object. You definitely
don't want to return that to const callers. For example

template <class T>
void f(const Matrix<T> &m)
{
T &cell = m.cell_at(1, 1);

// fiddle with cell, which is a reference so you are
// able to modify the original object.
}

The thing is, m was defined as const so you should not be able to do
that. Having two different versions of these (const and non const)
allows const and non const Matric objects to access the cell they want,
but only non const Matrix objects will be able to modify them.

template <class T>
void f(const Matrix<T> &cm, Matrix<T> &m)
{
T &cell = cm.cell_at(1, 1); // invalid, cannot convert const T&
// to T&

const T &cell = cm.cell_at(1, 1); // ok
cell.fiddle(); // error if fiddle() is not const

T &cell = m.cell_at(1, 1); // always ok
cell.fiddle(); // always ok
}

cm.cell_at() calls Matrix said:
Actually the resize function should be

void resize( unsigned h, unsigned v ) {
buffer.resize( h * v );
h_limit= h ;
v_limit= v ;
}

and the Matrix size function should be
long msize( ) { return=buffer.size(); }

Do I need const's for these functions?

Definitely not, since these functions are *not* const.
I don't quite understand the need for
the const in the cell_at and check_limits functions.

A const member function such as

class C
{
public:
void f() const;
};

receives a const this pointer, which means it can only call const member
functions.

class C
{
public:

void const_f() const
{
this->nonconst_f(); // error, since this is of type const C*
}

void nonconst_f()
{
this->const_f(); // ok, since this is of type C*
}
};

You can call const member functions from non const member functions, but
not the other way around.

A member function should be const when it does not modify the visible
state of the object (look up "mutable" for some interesting features).
Examples of const member functions are functions which return by value
or by const reference/pointer (such as length(), size(), state() and
distance()) and non const member functions modify the visible state
(such as resize(), clear(), add() and remove()).

You should definitely read more about that, since const member functions
are a powerful feature for preventing silly programmers (as we all are)
from touching something they shouldn't touch (as we all do).


Jonathan
 
D

Dennis

Jonathan Mcdougall said:
(e-mail address removed) wrote:
Why when making the function cell_at in the Matrix class why do you define two
cell_at functions? That is why do you need to have:
T& cell_at( unsigned h, unsigned v ) {
check_limits();
return buffer[ h_limit * h + v ];
}
and
const T& cell_at( unsigned h, unsigned v ) const {
check_limits();
return buffer[ h_limit * h + v ];
}
?

Because they return a reference to a private object. You definitely
don't want to return that to const callers. For example

template <class T>
void f(const Matrix<T> &m)
{
T &cell = m.cell_at(1, 1);

// fiddle with cell, which is a reference so you are
// able to modify the original object.
}

The thing is, m was defined as const so you should not be able to do
that. Having two different versions of these (const and non const)
allows const and non const Matric objects to access the cell they want,
but only non const Matrix objects will be able to modify them.

template <class T>
void f(const Matrix<T> &cm, Matrix<T> &m)
{
T &cell = cm.cell_at(1, 1); // invalid, cannot convert const T&
// to T&

const T &cell = cm.cell_at(1, 1); // ok
cell.fiddle(); // error if fiddle() is not const

T &cell = m.cell_at(1, 1); // always ok
cell.fiddle(); // always ok
}

cm.cell_at() calls Matrix said:
Actually the resize function should be

void resize( unsigned h, unsigned v ) {
buffer.resize( h * v );
h_limit= h ;
v_limit= v ;
}

and the Matrix size function should be
long msize( ) { return=buffer.size(); }

Do I need const's for these functions?

Definitely not, since these functions are *not* const.
I don't quite understand the need for
the const in the cell_at and check_limits functions.

A const member function such as

class C
{
public:
void f() const;
};

receives a const this pointer, which means it can only call const member
functions.

class C
{
public:

void const_f() const
{
this->nonconst_f(); // error, since this is of type const C*
}

void nonconst_f()
{
this->const_f(); // ok, since this is of type C*
}
};

You can call const member functions from non const member functions, but
not the other way around.

A member function should be const when it does not modify the visible
state of the object (look up "mutable" for some interesting features).
Examples of const member functions are functions which return by value
or by const reference/pointer (such as length(), size(), state() and
distance()) and non const member functions modify the visible state
(such as resize(), clear(), add() and remove()).

You should definitely read more about that, since const member functions
are a powerful feature for preventing silly programmers (as we all are)
from touching something they shouldn't touch (as we all do).


Jonathan

Jonathan thanks for the reasons for "const objects" reply above. Your reply
taught me a lot of things I couldn't quite grasp in the books I've read.

I have to admit though, I had to read over your reply five times before it sunk
in!

Thanks again.

Dennis
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top