473,396 Members | 1,748 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,396 software developers and data experts.

A question about the rule of three....

I have a class
class myImage: public std::list<myObject>
{
private:
std::string myImageName;
unsigned long size;
public:
myImage(void);
myImage & operator=(const myImage &i);
myImage(const myImage &i);
~myImage(void);

// There is no public data.
// A bunch of methods...
};

My question is:

as there is no use of 'new' is there anything that needs be done in
the
assignment operator, copy constructor and destructor?
specifically... the std::list<myObjectthat this class is deriving
from...

Is there any point in defining these three in this class?
Shouldn't the list of objects, the name and size be copied and
destroyed automatically?

Jun 13 '07 #1
10 1209
On 13 Jun, 17:27, SpreadTooThin <bjobrie...@gmail.comwrote:
I have a class
class myImage: public std::list<myObject>
{
private:
std::string myImageName;
unsigned long size;
public:
myImage(void);
myImage & operator=(const myImage &i);
myImage(const myImage &i);
~myImage(void);

// There is no public data.
// A bunch of methods...

};

My question is:

as there is no use of 'new' is there anything that needs be done in
the
assignment operator, copy constructor and destructor?
specifically... the std::list<myObjectthat this class is deriving
from...
you should know whether something else needs to be
done since you're the one who designed this class.

if you write
class myImage: public std::list<myObject>
{
private:
std::string myImageName;
unsigned long size;
};

compiler will generate default constructor,
copy constructor, assignment operator and
destructor. all doing what you would expect
them to do.

default constructor will call default constructor
of the base and members, copy constructor
will call copy constructors of the base and of
the members, etc...
Is there any point in defining these three in this class?
not unless compiler generated ones are not sufficient.
Shouldn't the list of objects, the name and size be copied and
destroyed automatically?
Of course.

DS

Jun 13 '07 #2
SpreadTooThin wrote:
I have a class
class myImage: public std::list<myObject>
{
private:
std::string myImageName;
unsigned long size;
public:
myImage(void);
myImage & operator=(const myImage &i);
myImage(const myImage &i);
~myImage(void);

// There is no public data.
// A bunch of methods...
};

My question is:

as there is no use of 'new' is there anything that needs be done in
the
assignment operator, copy constructor and destructor?
specifically... the std::list<myObjectthat this class is deriving
from...

Is there any point in defining these three in this class?
Shouldn't the list of objects, the name and size be copied and
destroyed automatically?
yes, the list will be destroyed and allocated properly, you needn't
worry about that. However, are you sure that you want to derive from
std::list? It seems a little bit strange, because conceptually an image
is not a list of object. Consider a std::list member if you don't
actually need a list interface, in order to improve the information hiding.

Also, the size is not the same as the size of the list, is it?

Regards,

Zeppe
Jun 13 '07 #3
On Jun 13, 11:09 am, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.itwrote :
SpreadTooThin wrote:
I have a class
class myImage: public std::list<myObject>
{
private:
std::string myImageName;
unsigned long size;
public:
myImage(void);
myImage & operator=(const myImage &i);
myImage(const myImage &i);
~myImage(void);
// There is no public data.
// A bunch of methods...
};
My question is:
as there is no use of 'new' is there anything that needs be done in
the
assignment operator, copy constructor and destructor?
specifically... the std::list<myObjectthat this class is deriving
from...
Is there any point in defining these three in this class?
Shouldn't the list of objects, the name and size be copied and
destroyed automatically?

yes, the list will be destroyed and allocated properly, you needn't
worry about that. However, are you sure that you want to derive from
std::list? It seems a little bit strange, because conceptually an image
is not a list of object. Consider a std::list member if you don't
actually need a list interface, in order to improve the information hiding.

Also, the size is not the same as the size of the list, is it?

Regards,

Zeppe
Well in this instance it kinda is a list of objects.. Its like a tiff
file.
and ya I think your right it probably is a better idea to stash the
list as a private memeber variable.
Jun 13 '07 #4
SpreadTooThin wrote:
On Jun 13, 11:09 am, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.itwrote :
>SpreadTooThin wrote:
>>I have a class
class myImage: public std::list<myObject>
{
private:
std::string myImageName;
unsigned long size;
public:
myImage(void);
myImage & operator=(const myImage &i);
myImage(const myImage &i);
~myImage(void);
// There is no public data.
// A bunch of methods...
};
My question is:
as there is no use of 'new' is there anything that needs be done in
the
assignment operator, copy constructor and destructor?
specifically... the std::list<myObjectthat this class is deriving
from...
Is there any point in defining these three in this class?
Shouldn't the list of objects, the name and size be copied and
destroyed automatically?
yes, the list will be destroyed and allocated properly, you needn't
worry about that. However, are you sure that you want to derive from
std::list? It seems a little bit strange, because conceptually an image
is not a list of object. Consider a std::list member if you don't
actually need a list interface, in order to improve the information hiding.

Also, the size is not the same as the size of the list, is it?

Regards,

Zeppe

Well in this instance it kinda is a list of objects.. Its like a tiff
file.
and ya I think your right it probably is a better idea to stash the
list as a private memeber variable.

It is useful also if you don't want to give access to the data in the
list directly, but you want to perform some action for example before
the insertion/deletion....
and a small suggestion: usually one may want to derive from a standard
container in order to use the standard library algorithms, which can ba
an advantage. But you can do that with a member variable providing
access to the iterators and the typedefs in order to hide the iterator
types:

class MyImage
{
public:
typedef std::list<MyObject>::iterator iterator;
typedef std::list<MyObject>::const_iterator const_iterator;

const_iterator begin() const { return components_.begin(); }
const_iterator end() const { return components_.end(); }
iterator begin() { return components_.begin(); }
iterator end() { return components_.end(); }

// something to insert and delete the parts in a safe way

private:
std::list<MyObjectcomponents_;
};

in this way you can also easily decide to change container at any time.

Regards,

Zeppe
Jun 13 '07 #5
On Jun 13, 11:25 am, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.itwrote :
SpreadTooThin wrote:
On Jun 13, 11:09 am, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.itwrote :
SpreadTooThin wrote:
I have a class
class myImage: public std::list<myObject>
{
private:
std::string myImageName;
unsigned long size;
public:
myImage(void);
myImage & operator=(const myImage &i);
myImage(const myImage &i);
~myImage(void);
// There is no public data.
// A bunch of methods...
};
My question is:
as there is no use of 'new' is there anything that needs be done in
the
assignment operator, copy constructor and destructor?
specifically... the std::list<myObjectthat this class is deriving
from...
Is there any point in defining these three in this class?
Shouldn't the list of objects, the name and size be copied and
destroyed automatically?
yes, the list will be destroyed and allocated properly, you needn't
worry about that. However, are you sure that you want to derive from
std::list? It seems a little bit strange, because conceptually an image
is not a list of object. Consider a std::list member if you don't
actually need a list interface, in order to improve the information hiding.
Also, the size is not the same as the size of the list, is it?
Regards,
Zeppe
Well in this instance it kinda is a list of objects.. Its like a tiff
file.
and ya I think your right it probably is a better idea to stash the
list as a private memeber variable.

It is useful also if you don't want to give access to the data in the
list directly, but you want to perform some action for example before
the insertion/deletion....
and a small suggestion: usually one may want to derive from a standard
container in order to use the standard library algorithms, which can ba
an advantage. But you can do that with a member variable providing
access to the iterators and the typedefs in order to hide the iterator
types:

class MyImage
{
public:
typedef std::list<MyObject>::iterator iterator;
typedef std::list<MyObject>::const_iterator const_iterator;

const_iterator begin() const { return components_.begin(); }
const_iterator end() const { return components_.end(); }
iterator begin() { return components_.begin(); }
iterator end() { return components_.end(); }

// something to insert and delete the parts in a safe way

private:
std::list<MyObjectcomponents_;

};

in this way you can also easily decide to change container at any time.

Regards,

Zeppe- Hide quoted text -

- Show quoted text -
Very nice.. Thank you. :)

Jun 14 '07 #6
On Jun 13, 6:27 pm, SpreadTooThin <bjobrie...@gmail.comwrote:
I have a class
class myImage: public std::list<myObject>
{
private:
std::string myImageName;
unsigned long size;
public:
myImage(void);
myImage & operator=(const myImage &i);
myImage(const myImage &i);
~myImage(void);
// There is no public data.
// A bunch of methods...
};
My question is:
as there is no use of 'new' is there anything that needs be
done in the assignment operator, copy constructor and
destructor? specifically... the std::list<myObjectthat this
class is deriving from...
Is there any point in defining these three in this class?
Yes. As others have pointed out, the default versions provided
by the compiler do have the appropriate semantics (probably).
But they are inline, and they are, in this case, far from
trivial, so not providing them yourself may result in
significant code bloat and unnecessary compiler dependencies.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 14 '07 #7
On 14 Jun, 08:44, James Kanze <james.ka...@gmail.comwrote:
On Jun 13, 6:27 pm, SpreadTooThin <bjobrie...@gmail.comwrote:


I have a class
class myImage: public std::list<myObject>
{
private:
std::string myImageName;
unsigned long size;
public:
myImage(void);
myImage & operator=(const myImage &i);
myImage(const myImage &i);
~myImage(void);
// There is no public data.
// A bunch of methods...
};
My question is:
as there is no use of 'new' is there anything that needs be
done in the assignment operator, copy constructor and
destructor? specifically... the std::list<myObjectthat this
class is deriving from...
Is there any point in defining these three in this class?

Yes. As others have pointed out, the default versions provided
by the compiler do have the appropriate semantics (probably).
But they are inline, and they are, in this case, far from
trivial, so not providing them yourself may result in
significant code bloat and unnecessary compiler dependencies.
Why compiler dependencies? Are you suggesting that some aspect of the
compiler-generated functions has implementation-defined behaviour?

Gavin Deane

Jun 14 '07 #8
SpreadTooThin wrote:
[very large snip]
>
Very nice.. Thank you. :)
Please trim your quotes to the minimum needed for context.


Brian
Jun 14 '07 #9
On Thu, 14 Jun 2007 03:47:22 -0700, Gavin Deane wrote:
>Yes. As others have pointed out, the default versions provided
by the compiler do have the appropriate semantics (probably).
But they are inline, and they are, in this case, far from
trivial, so not providing them yourself may result in
significant code bloat and unnecessary compiler dependencies.

Why compiler dependencies? Are you suggesting that some aspect of the
compiler-generated functions has implementation-defined behaviour?
No no, don't panic :-) He meant "unnecessary compile-time
dependencies" (IOWS unnecessary recompilations).

--
Gennaro Prota -- Need C++ expertise? I'm available
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
Jun 15 '07 #10
On Fri, 15 Jun 2007 10:40:28 +0200, Gennaro Prota wrote:
(IOWS unnecessary recompilations).
^^^^

And I meant *IOW :-)

--
Gennaro Prota -- Need C++ expertise? I'm available
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
Jun 15 '07 #11

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

Similar topics

7
by: CoolPint | last post by:
I read that there has to be only one definition of classes, functions, etc. in a project. I just realized I might have broken the rule by defining the exception classes in the header file. But...
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
3
by: comp.lang.php | last post by:
I have a counter that evokes the "Three Strikes You're Out" rule.. if you make more than N mistakes it auto-resets to avoid flooding $_SESSION with attempt after attempt, etc. However, the...
9
by: utab | last post by:
Dear all, How do experienced programmers using this group use the rule of thumb,namely copy ctor, assignment operator, and dtor. example if the class does not need them dont use and define...
5
by: Frederick Gotham | last post by:
(This sounds like a tutorial at the start, but it's actually a question when you get down into it...) When writing slightly more elaborate classes, there are Things To Be Aware Of. A good...
11
by: Kent Feiler | last post by:
I wonder if <pshould be included in the list of devalued html tags. As far as I can see it's nothing more than a <divfollowed by a <br /and it has the big disadvantage that <pforces a following...
18
by: Jon Slaughter | last post by:
"Instead of just waiting for its time slice to expire, a thread can block each time it initiates a time-consuming activity in another thread until the activity finishes. This is better than...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
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...
0
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 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.