A question about the rule of three....

S

SpreadTooThin

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<myObject> that 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?
 
D

dasjotre

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<myObject> that 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
 
Z

Zeppe

SpreadTooThin said:
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<myObject> that 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
 
S

SpreadTooThin

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.
 
Z

Zeppe

SpreadTooThin said:
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<MyObject> components_;
};

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

Regards,

Zeppe
 
S

SpreadTooThin

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<MyObject> components_;

};

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. :)
 
J

James Kanze

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<myObject> that 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.
 
G

Gavin Deane

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
 
G

Gennaro Prota

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top