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

complex inheritance

P: n/a
I have a problem with inheritance. I have a class:

TCellVisual : public TCell, public QWidget

TCell is a class that represents a cell in a board. TCellVisual implements
some methods to draw itself on the screen.

I have also a class:

struct TCells
{
TCell item[X][Y];
}

and

struct TCellsVisual
{
TCellVisual item[X][Y]
}

and:

TBoard : public TCells
that implements a lots of methods to analyse the board, if it's correct, ...

now I want to create a TBoardVisual, but how? I want to use the TCellsVisual
as base class:

TBoardVisual : TCellsVisual

but now, all the methods in TBoard?

TBoardVisual : public TBoard, TCellsVisual

but all the methods in TBoard refers to TCells and not to TCellsVisual.
Nov 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"wiso" <eh******@gmail.comwrote in message
news:45**********************@reader1.news.tin.it. ..
>I have a problem with inheritance. I have a class:

TCellVisual : public TCell, public QWidget

TCell is a class that represents a cell in a board. TCellVisual implements
some methods to draw itself on the screen.

I have also a class:

struct TCells
{
TCell item[X][Y];
}

and

struct TCellsVisual
{
TCellVisual item[X][Y]
}

and:

TBoard : public TCells
that implements a lots of methods to analyse the board, if it's correct,
...

now I want to create a TBoardVisual, but how? I want to use the
TCellsVisual
as base class:

TBoardVisual : TCellsVisual

but now, all the methods in TBoard?

TBoardVisual : public TBoard, TCellsVisual

but all the methods in TBoard refers to TCells and not to TCellsVisual.
Some thoughts off the top of my head:

1) The board doesn't need to know the exact type of the cell, it only needs
to know that the cell has a certain interface. This makes it quite a good
template candidate.

2) Having a struct with public data representing a cell array may not
necessarily be the best plan.

3) One way of doing it would be something like:

class TCell
{
//...
};

class TCellVisual : public TCell, public QWidget
{
//...
};

template <typename Cell>
class Board
{
protected:
Cell cells[X][Y]; // whatever X, Y happen to be (I wouldn't make them
global if that's what you've done)

// ... Analysis functions go here ...
};

Then what was TBoard will be Board<TCell>, or whatever. Now, suppose you
want to write a render function for your board with visual cells - you just
inherit from Board<TCellVisual>:

class VisualBoard : public Board<TCellVisual>
{
public:
void render() const
{
//...
}
};

Or something like that, anyway :) It's an idea.

Hope this helps,
Stu
Nov 5 '06 #2

P: n/a

wiso wrote:
I have a problem with inheritance. I have a class:

TCellVisual : public TCell, public QWidget
This "T" business at the beginning of the class names. You'll see this
sort of prefix in many older C++ class libraries that were designed
before namespaces where available in many compilers. These days you
should use a namespace:

namespace T {
class CellVisual;
}

Although a better name is probably in order.
>
TCell is a class that represents a cell in a board. TCellVisual implements
some methods to draw itself on the screen.
Not sure how this fits with the classes below. I expect that you want
something like this:

class Renderable {
public:
virtual ~Renderable();
virtual void render( /* arguments */ ) const = 0;
};

You can use this to draw anything. People often call classes like this
'interfaces' or their use a 'mix-in'. Anything that wishes to be drawn
should inherit from this and then implement the render() method.
Classes that implement this will probably register themselves with a
singleton that represents the screen which then calls render() on each
in turn.

You may need some extra things in the class depending on how the
drawing works - for example a Z index so that they're drawn in the
right order if overlapping.
>
I have also a class:

struct TCells
{
TCell item[X][Y];
}

and

struct TCellsVisual
{
TCellVisual item[X][Y]
}

and:

TBoard : public TCells
that implements a lots of methods to analyse the board, if it's correct, ...
These sorts of analysis functions are often better outside of the
class. This will reduce clutter in the class.
>
now I want to create a TBoardVisual, but how? I want to use the TCellsVisual
as base class:

TBoardVisual : TCellsVisual

but now, all the methods in TBoard?

TBoardVisual : public TBoard, TCellsVisual

but all the methods in TBoard refers to TCells and not to TCellsVisual.
This should get solved by using the Renderable class earlier. TBoard
should only know about the TCells stuff and it can just ignore the fact
that TCells also implements Renderable.
K

Nov 6 '06 #3

P: n/a
Kirit Sælensminde wrote:
>
wiso wrote:
>I have a problem with inheritance. I have a class:

TCellVisual : public TCell, public QWidget

This "T" business at the beginning of the class names. You'll see this
sort of prefix in many older C++ class libraries that were designed
before namespaces where available in many compilers. These days you
should use a namespace:

namespace T {
class CellVisual;
}

Although a better name is probably in order.
T stay for Type so I can write: TCell Cell
Nov 6 '06 #4

P: n/a
wiso wrote:
Kirit Sælensminde wrote:

>>wiso wrote:
>>>I have a problem with inheritance. I have a class:

TCellVisual : public TCell, public QWidget

This "T" business at the beginning of the class names. You'll see this
sort of prefix in many older C++ class libraries that were designed
before namespaces where available in many compilers. These days you
should use a namespace:

namespace T {
class CellVisual;
}

Although a better name is probably in order.


T stay for Type so I can write: TCell Cell
Cell cell;

Would be more idiomatic.

--
Ian Collins.
Nov 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.