A few (advanced?) questions

A

Allan Bruce

I have a few questions which I am unsure about, and would appreciate if
somebody could clarify them.

If I have the following example class:

class List
{
public:
List();
virtual ~List();

// ...
protected:
Node *mHeadNode;
};


This is a working list that I have implemented to test my programming
skills. Now I want to create another type of list derived from this (for
synchrnozing memory access, but thats not the point of the question). This
new class is basically a wrapper which does some initialising on each method
call, then executes the List:: method and then some cleaning up. I have
everything fine apart from I want to be sure about the constructors and
destructors. If I have my new class defined as:

class ListSync : public List
{
ListSync();
virtual ~ListSync();

then when I create a new ListSync it executes the constructor of List first
followed by the constructor of ListSync, correct? What happens when I
delete myListSync? Does it only call the destructor of ListSync?

The last question I have is: What if, in my new class ListSync, I want to
change the type of mHeadNode to AdvancedNode. I take it I cant merely
define it as:

class ListSync : public List
{
ListSync();
virtual ~ListSync();

// ...
protected:
AdvancedNode *mHeadNode;
};

Because when the following line is executed in List::
mHeadNode = new Node();
A Node will be created instead of an AdvancedNode. Is there a way to make
ListSync use an AdvancedNode instead of a Node?

Thanks,
Allan
 
M

Matthias Kaeppler

Allan said:
then when I create a new ListSync it executes the constructor of List first
followed by the constructor of ListSync, correct? What happens when I
delete myListSync? Does it only call the destructor of ListSync?

No, of course the base class part of ListSync has to be destroyed as
well. This happens implicitly, after destroying the subclass part. This
is covered in the FAQ:
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.12
The last question I have is: What if, in my new class ListSync, I want to
change the type of mHeadNode to AdvancedNode. I take it I cant merely
define it as:

class ListSync : public List
{
ListSync();
virtual ~ListSync();

// ...
protected:
AdvancedNode *mHeadNode;
};

Because when the following line is executed in List::
mHeadNode = new Node();
A Node will be created instead of an AdvancedNode. Is there a way to make
ListSync use an AdvancedNode instead of a Node?

Have you considered using templates? Otherwise, some factory mechanism
could work.
 
A

Allan Bruce

Matthias Kaeppler said:
No, of course the base class part of ListSync has to be destroyed as well.
This happens implicitly, after destroying the subclass part. This is
covered in the FAQ:
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.12


Have you considered using templates? Otherwise, some factory mechanism
could work.

I don't know how to use templates. I looked at them a while back but never
really seen the use for them. Perhaps now is the time?
Thanks,
Allan
 
P

Phil Staite

then when I create a new ListSync it executes the constructor of List first
followed by the constructor of ListSync, correct? What happens when I
delete myListSync? Does it only call the destructor of ListSync?

Yes, see any decent C++ text/reference/FAQ for the order of
construction/destruction with instances of derived classes. Note,
you've covered the tricky part by declaring the base class' destructor
as virtual. This will allow you to safely destruct an instance of the
derived list even if you only "know" it via a pointer to the base class
type. That is:

List* ptr = new ListSync;
....
delete ptr;

Will call ~ListSync, then ~List...
The last question I have is: What if, in my new class ListSync, I want to
change the type of mHeadNode to AdvancedNode. I take it I cant merely
define it as:

No, what you might want to do is divert allocation of nodes to a
protected virtual "generator" function. Something like:

class Node {}; ...

class AdvancedNode : public Node {}; ...

class List {
....
protected:
Node* mHeadNode;

virtual Node* getNode() { return new Node; }
};

class ListSync : public List {
....
protected:
virtual Node* getNode() { return AdvancedNode; }
};

That way, even methods in List will get AdvancedNodes if that List
happens to be part of a ListSync...
 
M

Matthias Kaeppler

Allan said:
I don't know how to use templates. I looked at them a while back but never
really seen the use for them. Perhaps now is the time?
Thanks,
Allan

Yes. Generic programming is a central aspect of the C++ programming
language, and large parts of the standard library like containers and
algorithms are generated from templates. In order to use them
efficiently, you should understand them in the first place.
 
P

Puppet_Sock

Allan Bruce wrote:
[snip]
class ListSync : public List

In addition to what others have said, you might want to reconsider
the wisdom of achieving this through inheritance. The slogan that
goes with this is: A container of children is not a child of
container of parents. Or, you can't park your nuclear submarine
in the motorcycle parking lot.

So instead of public inheritance, you might want to consider having
class ListSync have a member element that is a List. Or, more likely,
you want to make a template of your List class and then have generic
items be storable. Or better still, once you've learned the stuff
that goes with making a list, drop the notion of rolling your own
containers, and learn the standard containers.
Socks
 
P

Phil Staite

Phil said:
Yes, see any decent C++ text/reference/FAQ for the order of

I should have read your text more carefully. No, it does not *only*
call the ListSync destructor. The ListSync destructor will be called
first, then the List destructor. (assuming you're destructing a ListSync
object and not simply a List object)

Should've had more coffee...
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top