Hello experts!
Assume we have the definition of class Matrix below.
Now to my question how is it possible to have this expression (*this = m;)
in the copy constructor part of class Matris.
Isn't this a constant pointer to the current object and can't be changed.
Im I wrong?
class Matrix
{
public:
Matrix(int i=0, int j=0 : r(i), k(j), a(new double[r*k]) {}
Matrix(const Matrix& m) : a(0)
{
*this = m; //?????????????
}
~Matrix()
{ delete [] a; }
int ant_rad()
{ return r; }
int ant_kol()
{ return k; }
Matrix& operator= (const Matris&);
double operator() (int i, int j);
private:
double *a;
int r, k;
};
Many thanks
//Tony 6 1342
"Tony Johansson" <jo*****************@telia.com> wrote in message
news:3W*********************@newsc.telia.net Hello experts!
Assume we have the definition of class Matrix below. Now to my question how is it possible to have this expression (*this = m;) in the copy constructor part of class Matris. Isn't this a constant pointer to the current object and can't be changed. Im I wrong?
You need to distinguish between a constant pointer and a pointer to a
constant object. this is the former, not the latter, and it is only the
latter that would rule out *this = m; since the pointer is not being
changed, only the contents of the object being pointed to.
--
John Carson
In message <3W*********************@newsc.telia.net>, Tony Johansson
<jo*****************@telia.com> writes Hello experts!
Assume we have the definition of class Matrix below. Now to my question how is it possible to have this expression (*this = m;) in the copy constructor part of class Matris. Isn't this a constant pointer to the current object and can't be changed.
That's correct. 'this' is a constant pointer to the current Matrix
object. The *pointer* can't be changed, but what it points to can. *this
is a reference to the current object, which can be changed.
Im I wrong?
Matrix * const is not the same thing as Matrix const *. class Matrix {
[snip irrelevant details]public: Matrix(int i=0, int j=0 : r(i), k(j), a(new double[r*k]) {} Matrix(const Matrix& m) : a(0) { *this = m; //?????????????
No problem. That invokes operator= on the current object. It doesn't
change the pointer.
} ~Matrix() { delete [] a; }
Matrix& operator= (const Matris&);
You don't show the definition of this function. It's a potential source
of problems (see the following comment) private: double *a; int r, k; };
Many thanks
As a matter of style, it's often better to write operator= in terms of
the copy constructor using the copy-and-swap idiom, rather than vice
versa. The assignment operator has to deal with the complication of
disposing of the previous state of the object, which the constructor
doesn't.
--
Richard Herring
Richard Herring wrote: As a matter of style, it's often better to write operator= in terms of the copy constructor using the copy-and-swap idiom, rather than vice versa. The assignment operator has to deal with the complication of disposing of the previous state of the object, which the constructor doesn't.
Indeed, but in this case, using std::vector instead of an array would
mean you would not have to implement the big 3 at all
(copy-construction, assignment and destruction) as all 3 would work
with their default implementations.
By the way, what does the standard do if you call new[] with a 0
argument? (I wouldn't know because I never do it, I always use vector).
You might also want to make the class a template. Why specify double as
the type?
In message <11*********************@g43g2000cwa.googlegroups. com>, Earl
Purple <ea*********@yahoo.com> writes Richard Herring wrote: As a matter of style, it's often better to write operator= in terms of the copy constructor using the copy-and-swap idiom, rather than vice versa. The assignment operator has to deal with the complication of disposing of the previous state of the object, which the constructor doesn't. Indeed, but in this case, using std::vector instead of an array would mean you would not have to implement the big 3 at all (copy-construction, assignment and destruction) as all 3 would work with their default implementations.
Indeed indeed. That's a big win: the compiler is far better at keeping
track of proliferating member variables than I am. By the way, what does the standard do if you call new[] with a 0 argument? (I wouldn't know because I never do it, I always use vector).
It returns a distinct non-null pointer to an array with no elements
(5.3.4/7)
By the way, does it constitute a memory leak if you fail to delete[] it?
You might also want to make the class a template. Why specify double as the type?
*He* might. I haven't designed yet another 2D matrix class for several
years now ;-)
--
Richard Herring
Richard Herring schreef: In message <11*********************@g43g2000cwa.googlegroups. com>, Earl Purple <ea*********@yahoo.com> writesBy the way, what does the standard do if you call new[] with a 0 argument? (I wouldn't know because I never do it, I always use vector).
It returns a distinct non-null pointer to an array with no elements (5.3.4/7)
By the way, does it constitute a memory leak if you fail to delete[] it?
Yes. There are only a limited number of distinct pointers, so you can
run out of legal return values. That means new[] eventually will throw
a
std::bad_alloc. I call that a memory leak.
HTH,
Michiel Salters This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ali |
last post by:
Hi,
I'm trying to understand the reason for different output on the
following codes
Code1:
#include <iostream.h>
int main()
|
by: iceColdFire |
last post by:
HI,
I have a function as
void f(int p)
{
return p++;
}
now I have created a function pointer as
|
by: __PPS__ |
last post by:
Hello everybody
in a quiz I had a question about dangling pointer:
"What a dangling pointer is and the danger of using it"
My answer was:
"dangling pointer is a pointer that points to some...
|
by: Ike Naar |
last post by:
Hi,
Asking your advice on the following subject:
Suppose I want to find out whether a given pointer (say, p) of type *T
points to an element of a given array (say, a) of type T.
A way to...
|
by: dough |
last post by:
Is it possible in C to declare and initialize a pointer that points to
itself? Why or why not?
|
by: Jeff |
last post by:
I am struggling with the following
How do I marshal/access a pointer to an array of strings within a structure
Than
Jef
----------------------------------------------------------------
|
by: Abhishek |
last post by:
why do I see that in most C programs, pointers in functions are
accepted as:
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what...
|
by: khan |
last post by:
Hi,
I read that pointer representation can non-zero bit pattern, machine
specific.Compiler when comes accross value '0' in pointer context,
converts it to machine specific null pointer...
|
by: lithiumcat |
last post by:
Hi,
maybe you remember me, some time ago I asked about how to store an
integer value into a void*, and I learned that doing pointer
arithmetic yeilding a pointer outside of an object (except the...
|
by: Szabolcs Borsanyi |
last post by:
Deal all,
The type
typedef double ***tmp_tensor3;
is meant to represent a three-dimensional array. For some reasons the
standard array-of-array-of-array will not work in my case.
Can I...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |