473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Do i need a copy contructor?

Hi,

if I have a structure like...

struct sMyPointers{
ClassA *m_pPointerA;
ClassB *m_pPointerB;
void *m_pPointerToSomethingElse;
};

And then

std::vector<sMyPointers *> pPointers;

sMyPointers *pA = new sMyPointers();
pPointers->push_back(pA);
pA = new sMyPointers();
pPointers->push_back(pA);
pA = new sMyPointers();
pPointers->push_back(pA);

The reason why I want pointers is mainly or speed but also to make sure that
I only have one set of data, rather than a bunch of copies all over the
place.
The data is created one and deleted once.

But is the above correct? Is it good coding practice?

regards.
Simon
Jul 23 '05 #1
9 2822
"Simon" <sp********@example.com> schrieb:
struct sMyPointers{
ClassA *m_pPointerA;
ClassB *m_pPointerB;
void *m_pPointerToSomethingElse;
};
Where is the initialization of the pointers done? And especially:
where are the pointers deleted? Do you have a destructor doing this?
This question is relevant for answering on "Do I need a copy
constructor?" If these instances of sMyPointers own the inner
pointers, I would suggest to make a copy constructor.
std::vector<sMyPointers *> pPointers;

sMyPointers *pA = new sMyPointers();
pPointers->push_back(pA);
pA = new sMyPointers();
pPointers->push_back(pA);
pA = new sMyPointers();
pPointers->push_back(pA);
This is legal. You could avoid the variable pA. You do not need a copy
constructor here.
The reason why I want pointers is mainly or speed but also to make
sure that I only have one set of data, rather than a bunch of copies
all over the place. The data is created one and deleted once.

But is the above correct? Is it good coding practice?


Notice, the allocation and deallocation of objects on the heap can
take more time than to make a copy. It depends on the objects and
their data.

T.M.
Jul 23 '05 #2
Simon wrote:
struct sMyPointers{
ClassA *m_pPointerA;
ClassB *m_pPointerB;
void *m_pPointerToSomethingElse;
};


The real question is "Who owns the resources that the pointers points
to?"

Is this struct just a "holder" for grouping three pieces of data
together, or is it actually responsible for managing the resources? In
general, whichever class does the actual resource management should
have a copy constructor, destructor, and assignment operator.

Technically, this struct is okay for use in standard containers, but if
you want your code to be reliable you really have to answer the above
questions.

Jul 23 '05 #3
> Simon wrote:
struct sMyPointers{
ClassA *m_pPointerA;
ClassB *m_pPointerB;
void *m_pPointerToSomethingElse;
};


The real question is "Who owns the resources that the pointers points
to?"

Is this struct just a "holder" for grouping three pieces of data
together, or is it actually responsible for managing the resources? In
general, whichever class does the actual resource management should
have a copy constructor, destructor, and assignment operator.

Technically, this struct is okay for use in standard containers, but if
you want your code to be reliable you really have to answer the above
questions.


Well my class is in charge of the pointers, so it will create them and
delete them as needed.
The idea was to 'tidy' the code a little, rather than having

The number of items in the vector is unknown, so it is nice to be able to
create them on the fly and just add them to a structure.
That way I can manage the data better.

By having the structure, (and then the vector), the number of items is
immaterial.
I can create 5, 10, 100 instances as long as I delete every single items.

Simon.


Jul 23 '05 #4
> Well my class is in charge of the pointers, so it will create them and
delete them as needed.
The idea was to 'tidy' the code a little, rather than having

The number of items in the vector is unknown, so it is nice to be able to
create them on the fly and just add them to a structure.
That way I can manage the data better.

By having the structure, (and then the vector), the number of items is
immaterial.
I can create 5, 10, 100 instances as long as I delete every single items.


Imagine this:

class C
{
private:
int *i;

public:
C()
: i(new int)
{
}

~C()
{
delete i;
}
};

int main()
{
C c1;
C c2(c1);

// here, both c1 and c2 have their pointer 'i' pointing to the same
// int
}

When c2 goes out of scope, it deletes its int. Then c1 goes out of
scope and the deletes the very same one, which is undefined behavior.

When a class manages memory, it usually needs a copy constructor and an
assignment operator:

class C
{
private:
int *i;

public:
C()
: i(new int)
{
}

C(const C &c)
: i(new int(c.i))
{
}

C &operator=(const C &c)
{
delete i;
i = new int(c.i);
}

~C()
{
delete i;
}
};

But you'd be better with a kind of smart pointer, which manages this
automatically. std::auto_ptr does not.
Jonathan

Jul 23 '05 #5
Just wanted to know if you were paying attention... The second code
should be

class C
{
private:
int *i;

public:
C()
: i(new int)
{
}

C(const C &c)
: i(new int(*c.i))
{
}

C &operator=(const C &c)
{
delete i;
i = new int(*c.i);

return *this;
}

~C()
{
delete i;
}
};
Jonathan

Jul 23 '05 #6
Just wanted to know if you were paying attention... The second code
should be


Thanks, I didn't want to say anything.

but while we are on the subject, isn't

const C &operator=(const C &c)
....

better than

C &operator=(const C &c)
?

Simon
Jul 23 '05 #7
On 2005-07-15 07:30:10 -0400, "Simon" <sp********@example.com> said:
Just wanted to know if you were paying attention... The second code
should be


Thanks, I didn't want to say anything.

but while we are on the subject, isn't

const C &operator=(const C &c)
...

better than

C &operator=(const C &c)


No, why do you think it would be? Generally, when overloading
operators, a good goal is to make them behave as closely as possible to
how they would behave when applied to built-in types; using the
assignment operator on a built in type results in what is essentially a
non-const reference to the left operand:

int i;
int &ir = i = 5; //Assigns 5 to i, and binds ir to i
--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #8
>>
Just wanted to know if you were paying attention... The second code
should be


Thanks, I didn't want to say anything.

but while we are on the subject, isn't

const C &operator=(const C &c)
...

better than

C &operator=(const C &c)


No, why do you think it would be? Generally, when overloading operators, a
good goal is to make them behave as closely as possible to how they would
behave when applied to built-in types; using the assignment operator on a
built in type results in what is essentially a non-const reference to the
left operand:

int i;
int &ir = i = 5; //Assigns 5 to i, and binds ir to i


Truth be told, I am not sure, I remember reading somewhere that it was
better to do the "const", but I don't know where I got such an idea from.

Maybe someone could help me on that one.

Simon
Jul 23 '05 #9
> Truth be told, I am not sure, I remember reading somewhere that it was
better to do the "const", but I don't know where I got such an idea from.

Maybe someone could help me on that one.


Well for myself, I remember reading that void main() was better, so you
can't count on that.

An argument you may have heard is that it allows

(c1=c2) = c3;

which looks weird, but is allowed for ints:

int i1=1, i2=2, i3=3;

(i1=i2)=i3;

As our good Scott Meyers said, "do as the ints do".

Jonathan

Jul 23 '05 #10

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

Similar topics

2
by: Ishira | last post by:
Hello, Please help. I am seriously at my wits end. Just when I feel I have completely understood copy contructor, I am stumped yet again. I have a class which has a CARRAY of objects. I am...
4
by: Matthias Spiller | last post by:
Hi, following problem. I'm writing an Image class and want to use templates: .... template<class TType, int TLayers> class Image { public: Image();
2
by: | last post by:
I have the some HTML and JavaScript code that implements a type-ahead capability for a text input control linked to a select control (see end of message). The type-ahead functionality works well....
2
by: Bruno Ripa | last post by:
Hallo, imagine to have the following simple code: #include <iostream> class C { public: C() {}
6
by: nutty | last post by:
Hi all, I have the following problem ( explanation below code ): // main.cpp class noncopyable { protected: noncopyable() {}
15
by: Brett Wickard | last post by:
Ok, this should be simple, but how do I copy a reference type by value? Meaning SomeComplexObject s1 = new SomeComplexObject (); SomeComplexObject s2 = new SomeComplexObject (); s1.color =...
15
by: Frederick Gotham | last post by:
What's the canonical way to copy an array in C++? If we're copying a POD, we can use memcpy (but there could be a more efficient alternative if we know that the blocks are suitably aligned). ...
0
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...
7
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...

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.