Inheritance - style question

P

Paul N

I'm using inheritance - this particular case is to provide a linked
list of attributes of a piece of text (text colour, italic etc) so
that different parts of it can be shown in different ways.

class Attrib : public Listable {
public:
// stuff including where in the text the attribute starts to take
effect
};

class Col_Attrib : public Attrib {
public:
COLORREF col;
// plus more stuff
};

class Ink : public Col_Attrib {
public:
// stuff
};

class Paper : public Col_Attrib {
public:
// stuff
};

Now, if I include a constructor:

Col_Attrib::Col_Attrib(COLORREF incol) : col(incol) { }

then this doesn't, by itself, create constructors for Ink and Paper
taking a COLORREF, does it?

If I did want such constructors, would it be best to include a
constructor as above and then do:

Ink::Ink(COLORREF incol) : Col_Attrib(incol) { }

or would it be better to do:

Ink::Ink(COLORREF incol) : col(incol) { }

directly? The first form seems more natural in terms of the hierarchy,
but the second seems more efficient.

Thanks for any advice.
Paul.
 
A

Alf P. Steinbach

* Paul N:
I'm using inheritance - this particular case is to provide a linked
list of attributes of a piece of text (text colour, italic etc) so
that different parts of it can be shown in different ways.

class Attrib : public Listable {
public:
// stuff including where in the text the attribute starts to take
effect
};

Why not use a std::list.

class Col_Attrib : public Attrib {
public:
COLORREF col;
// plus more stuff
};

class Ink : public Col_Attrib {
public:
// stuff
};

class Paper : public Col_Attrib {
public:
// stuff
};

Now, if I include a constructor:

Col_Attrib::Col_Attrib(COLORREF incol) : col(incol) { }

then this doesn't, by itself, create constructors for Ink and Paper
taking a COLORREF, does it?
No.



If I did want such constructors, would it be best to include a
constructor as above and then do:

Ink::Ink(COLORREF incol) : Col_Attrib(incol) { }

or would it be better to do:

Ink::Ink(COLORREF incol) : col(incol) { }

directly? The first form seems more natural in terms of the hierarchy,
but the second seems more efficient.

The second form wouldn't compile.


Cheers & hth.,

- Alf
 
P

Paul N

* Paul N:



Why not use a std::list.

Because I have already written a linked list class, which works
perfectly well and which I understand how to use, whereas I don't know
anything about templates. Though arguably it would be a good idea for
me to find out :)
The second form wouldn't compile.

Do you mean it won't compile *if I include the above-mentioned
Col_Attrib constructor* or that it won't compile full stop? My
question was intended to include this constructor only in the first
alternative - sorry for the lack of clarity.

Thanks.
Paul.
 
P

Paul N

No, it doesn't.






The former. The latter won't compile. (yes, full stop.)

Maybe if you included all the optional stuff that happens automatically
in the code, things might make more sense to you:

Ink::Ink(COLORREF incol)
   : Col_Attrib()
   , col(incol)
{ }

The above doesn't work. You are trying to initialize 'col' but it was
already initialized in the Col_Attrib() constructor. The Col_Attrib()
constructor call happens, even if you don't include the code.
So you have to:

Col_Attrib::Col_Attrib(COLORREF incol)
   : Attrib()
   , col(incol)
{ }

Ink::Ink(COLORREF incol)
   : Col_Attrib(incol)
{ }

Hope that helps.

Thanks. Yes, it does help. I was getting confused between actual
initialisation and "things that it would be nice to do in a
constructor".
 

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,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top