473,734 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the pointer this

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
Aug 15 '05 #1
6 1355
"Tony Johansson" <jo************ *****@telia.com > wrote in message
news:3W******** *************@n ewsc.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

Aug 15 '05 #2
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
Aug 15 '05 #3

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?

Aug 15 '05 #4
In message <11************ *********@g43g2 000cwa.googlegr oups.com>, Earl
Purple <ea*********@ya hoo.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
Aug 15 '05 #5
Richard Herring schreef:
In message <11************ *********@g43g2 000cwa.googlegr oups.com>, Earl
Purple <ea*********@ya hoo.com> writes
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?


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

Aug 16 '05 #6
Tony Johansson wrote:
Assume we have the definition of class Matrix below.


[snip]

Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library

http://www.netwood.net/~edwin/svmtl/

Also, take a look at
The Object-Oriented Numerics Page

http://www.oonumerics.org/oon/
Aug 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2343
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()
9
2523
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
20
6561
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 area of ram that's not reserved by the program. Accessing memory through such pointer is likely to result in core dump (memory access violation)"
67
4466
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 achieve this would be a linear search through the array: int ptrInArrSafe(T *p,T *a,int max) /* check whether p points to an element of a */
33
4877
by: dough | last post by:
Is it possible in C to declare and initialize a pointer that points to itself? Why or why not?
1
3102
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 ----------------------------------------------------------------
16
10390
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 do you mean by pointing to a void and why is it done? Aso, what happens when we typecast a pointer to another type. say for example int *i=(char *)p; under different situations? I am kind of confused..can anybody clear this confusion by clearly...
15
3769
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 bit-pattern. My question is if a program refers to that specific value which is used by the machine to refer to null pointer, how compiler treats that?.
6
2319
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 one- after-last thingy) is undefined behaviour. Actually I was trying to associate a function pointer with a key, through an AVL tree that managed void* data. Function pointers can't be stored in void* (that is, the standard does not garantee...
14
1984
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 convert an object of this type to the following type?
0
8946
marktang
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...
0
9310
jinu1996
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...
0
9184
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8187
agi2029
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...
1
6737
isladogs
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...
0
6033
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3262
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
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.