Hi,
I'm writing a vector class:
//mv3.h
class mv3Impl;
class mv3
{
public:
mv3();
mv3(mv3&);
mv3& operator= ( const mv3& RHS);
private:
mv3Impl* pImpl;
};
//mv3.cpp
class mv3Impl
{
public:
float x,y,z;
};
mv3::mv3()
{
pImpl = new mv3Impl;
pImpl->x = pImpl->y = pImpl->z = 1.0f;
}
mv3::mv3(mv3& v)
{
pImpl = new mv3Impl;
*pImpl = *(v->pImpl);
}
mv3& mv3::operator= ( const mv3& RHS)
{
pImpl = new mv3Impl; /* <----------------Do I need this line??
*/
*pImpl = *(RHS->pImpl);
}
Do i need the line that allocates memory or will this create a memory leak?
What functions are called in:
mv3 a;
mv3 b = a;
??
Thanks
Mike 7 2814
"Michael" <sl***********@hotmail.com> wrote in message
news:ck**********@titan.btinternet.com... Hi, I'm writing a vector class:
//mv3.h
class mv3Impl;
class mv3 { public: mv3(); mv3(mv3&);
Should be
mv3(const mv3&);
mv3& operator= ( const mv3& RHS);
private: mv3Impl* pImpl; }; //mv3.cpp
class mv3Impl { public: float x,y,z; };
mv3::mv3() { pImpl = new mv3Impl; pImpl->x = pImpl->y = pImpl->z = 1.0f; }
mv3::mv3(mv3& v) { pImpl = new mv3Impl; *pImpl = *(v->pImpl); }
mv3& mv3::operator= ( const mv3& RHS) { pImpl = new mv3Impl; /* <----------------Do I need this line?? */
No. You are assigning to an already existing object, so it will already have
allocated some memory.
*pImpl = *(RHS->pImpl);
}
Do i need the line that allocates memory or will this create a memory leak?
A memory leak.
What functions are called in:
mv3 a; mv3 b = a;
First line - default constructor
Second line - copy constructor
John
"Michael" <sl***********@hotmail.com> wrote in message
news:ck**********@titan.btinternet.com... Hi, I'm writing a vector class:
//mv3.h
class mv3Impl;
class mv3 { public: mv3(); mv3(mv3&); mv3& operator= ( const mv3& RHS);
private: mv3Impl* pImpl; }; //mv3.cpp
class mv3Impl { public: float x,y,z; };
mv3::mv3() { pImpl = new mv3Impl; pImpl->x = pImpl->y = pImpl->z = 1.0f; }
mv3::mv3(mv3& v) { pImpl = new mv3Impl; *pImpl = *(v->pImpl); }
mv3& mv3::operator= ( const mv3& RHS) { pImpl = new mv3Impl; /* <----------------Do I need this line?? */ *pImpl = *(RHS->pImpl);
}
Do i need the line that allocates memory or will this create a memory
leak?
Yes, In addition to the already existing memory leak(s). Think about it.
Jeff F
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2t*************@uni-berlin.de... "Michael" <sl***********@hotmail.com> wrote in message news:ck**********@titan.btinternet.com... Hi, I'm writing a vector class:
//mv3.h
class mv3Impl;
class mv3 { public: mv3(); mv3(mv3&); Should be
mv3(const mv3&);
mv3& operator= ( const mv3& RHS);
private: mv3Impl* pImpl; }; //mv3.cpp
class mv3Impl { public: float x,y,z; };
mv3::mv3() { pImpl = new mv3Impl; pImpl->x = pImpl->y = pImpl->z = 1.0f; }
mv3::mv3(mv3& v)
Also here:
Should be
mv3(const mv3&);
{ pImpl = new mv3Impl; *pImpl = *(v->pImpl); }
mv3& mv3::operator= ( const mv3& RHS) { pImpl = new mv3Impl; /* <----------------Do I need this
line?? */
No. You are assigning to an already existing object, so it will already
have allocated some memory.
*pImpl = *(RHS->pImpl);
}
Do i need the line that allocates memory or will this create a memory leak?
A memory leak.
What functions are called in:
mv3 a; mv3 b = a;
First line - default constructor Second line - copy constructor
John
delete pImpl;
in the destructor :-)
Cheers guys
"Jeff Flinn" <NO****@nowhere.com> wrote in message
news:ck**********@bluegill.adi.com... "Michael" <sl***********@hotmail.com> wrote in message news:ck**********@titan.btinternet.com... Hi, I'm writing a vector class:
//mv3.h
class mv3Impl;
class mv3 { public: mv3(); mv3(mv3&); mv3& operator= ( const mv3& RHS);
private: mv3Impl* pImpl; }; //mv3.cpp
class mv3Impl { public: float x,y,z; };
mv3::mv3() { pImpl = new mv3Impl; pImpl->x = pImpl->y = pImpl->z = 1.0f; }
mv3::mv3(mv3& v) { pImpl = new mv3Impl; *pImpl = *(v->pImpl); }
mv3& mv3::operator= ( const mv3& RHS) { pImpl = new mv3Impl; /* <----------------Do I need this
line?? */ *pImpl = *(RHS->pImpl);
}
Do i need the line that allocates memory or will this create a memory leak?
Yes, In addition to the already existing memory leak(s). Think about it.
Jeff F
Sorry But I can't see the relevance, please explain further.
Fanks
Mike
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:ck**********@nntp1.jpl.nasa.gov... Michael wrote:
[snip]
Take a look at The C++ Scalar, Vector, Matrix and Tensor class library
http://www.netwood.net/~edwin/svmtl/
"Michael" <sl***********@hotmail.com> wrote in message
news:ck**********@titan.btinternet.com... Sorry But I can't see the relevance, please explain further. Fanks Mike
More or less any post that is about vectors and the like, Robert advertises
his vector library.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Peter Olcott |
last post by:
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?
//
#include <stdio.h>
#include <stdlib.h>
...
|
by: William Payne |
last post by:
Hello, I was under the impression that if I made a class Foo and if I didn't
specify a copy constructor I would get one anyway that simply assigns the
member variables (and that won't work for...
|
by: MSR |
last post by:
I have a couple of questions.
1. Copy Constructor.
class A {
private:
int a1;
double d1;
char *ptr;
|
by: Martijn van Buul |
last post by:
Hi.
I'm having a peculiar problem at work. I've been googling for it, but haven't
found an authorative answer. In a nutshell (Long story follows),
what I'd like to know is:
If I have a C++...
|
by: rKrishna |
last post by:
I was trying to understand the real need for copy constructors. From
literature, the main reason for redfinition of copy constructor in a
program is to allow deep copying; meaning ability to make...
|
by: indrawati.yahya |
last post by:
According to the FAQ, the best way to inform a class user of an error
that occurs inside a constructor is to throw an exception. My question
is, what happens when an object is instantiated using...
|
by: sam_cit |
last post by:
Hi Everyone,
I was just wondering, about the overloaded assignment operator for
user defined objects. It is used to make sure that the following works
properly,
obj1 = obj;
so the...
|
by: ecestd |
last post by:
I did implement the copy constructor but still have a problem with
it. It is not working. What could be wrong?
#include "QueueP.h"
#include <cassert // for assert
#include <new // for...
|
by: Rahul |
last post by:
Hi Everyone,
I have the following code and i'm able to invoke the destructor
explicitly but not the constructor. and i get a compile time error
when i invoke the constructor, why is this so?
...
|
by: Jeffrey Barish |
last post by:
(Pdb) myclass
MyClass( 0, 0, 'A string', 123.45)
(Pdb) copy.copy(myclass)
*** TypeError: TypeError('__new__() takes at least 4 arguments (2 given)',)
I see 4 arguments (actually, 5 because...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |