By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

A few (advanced?) questions

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Allan Bruce wrote:
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-lit...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.
Thanks,
Allan

--
Matthias Kaeppler
Jul 23 '05 #2

P: n/a

"Matthias Kaeppler" <no****@digitalraid.com> wrote in message
news:d2*************@news.t-online.com...
Allan Bruce wrote:
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-lit...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.
Thanks,
Allan



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
Jul 23 '05 #3

P: n/a
> 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...
Jul 23 '05 #4

P: n/a
Allan Bruce wrote:
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.

--
Matthias Kaeppler
Jul 23 '05 #5

P: n/a
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

Jul 23 '05 #6

P: n/a
Phil Staite wrote:
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


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...
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.