Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Member struct declared in base, defined in derived class

Reply
Thread Tools

Member struct declared in base, defined in derived class

 
 
Ninereeds
Guest
Posts: n/a
 
      04-03-2007
I'm messing around with using mixin-layers (look for papers by Yannis
Smaragdakis and Don Batory) to define data structures. One issue is
that nodes tend to have pointers to other nodes - the pointers have to
point to the full node type, and have to be referenced before that
full node type is known.

One solution is to use the 'fixpoint construction' to get an apparent
circular dependency...

class c_Final : public c_Layer2< c_Layer1 <c_Final> > {};

I'm always a bit nervous about this, though, so I was wondering about
alternatives. It seemed to me that all I really need to do is to
declare the final node type in a base class, but define it in a
derived class.

So, I tried the following experiment...

class c_Base
{
public:
struct c_Full_Node;

struct c_Node
{
c_Full_Node *m_Parent;
};
};

class c_Derived1 : public c_Base
{
public:
struct c_Base::c_Full_Node : public c_Base::c_Node
{
int m_Data;
};

typedef c_Base::c_Full_Node c_Full_Node;
};

//class c_Derived2 : public c_Base
//{
// public:
// struct c_Base::c_Full_Node : public c_Base::c_Node
// {
// char m_Data;
// };
//
// typedef c_Base::c_Full_Node c_Full_Node;
//};

//////////

int main(int argc, char* argv[])
{
c_Derived1::c_Full_Node l_Node1;

l_Node1.m_Parent = 0;
l_Node1.m_Data = 0;

//c_Derived2::c_Full_Node l_Node2;

//l_Node2.m_Parent = 0;
//l_Node2.m_Data = 0;

return EXIT_SUCCESS;
}

The commented out code does not compile, for fairly obvious reasons -
it tries to create a second definition of c_Base::c_Full_Node.

This seems like a perfect solution to the problem to me, since it
doesn't give the appearance of peering into the future. I haven't
wrapped it up in templates to do real mixin layer stuff yet, but I
can't see any *additional* reasons why that shouldn't work.

What can I say. I've learned to think twice before saying "that would
never work" with C++, or I would never have even tried this
experiment.

That said, all I have proved is that it seems to work with Microsoft
Visual C++ 2003. This doesn't mean that it *should* work, according to
the standard.

So - can anyone tell me whether this is standard-compliant, portable
code? I haven't got a clue myself.

 
Reply With Quote
 
 
 
 
Ninereeds
Guest
Posts: n/a
 
      04-03-2007

Applying this with mixin layer templates turned out to be a little
fiddly, because the base for the mixin layers cannot be templated. I
have built a working example, though.

It does require more code to do the composition than using the
fixpoint thing, since it needs the initial struct to be defined before
the composition, and then needs 'capping off' at the end with another
non-template struct.

Also, it looks like there's no real point to this when the base class
for the composition isn't templated. In the following example,
c_Base::c_Full_Node could have been defined outside of c_List, and it
is probably less confusing to do so.

I have achieved my goal, in that I can build data structures using
mixin layers but without using the fixpoint construct anyway, and I'm
kicking myself for not realising it's this simple.

The question remains, though - does the C++ standard require that it
is legal to have a struct/class that is declared in a base class, but
defined in its derived class?

The full example follows...


namespace mixins
{
// Required base class for starting the mixin
// composition - defines the empty base for the
// node structure that other mixin layers refine.
template<class B> struct c_List_Base : public B
{
typedef typename B::c_Full_Node c_Full_Node;

struct c_Node { };
};

template<class B> struct c_List_Fwd : public B
{
typedef typename B::c_Full_Node c_Full_Node;

struct c_Node : public B::c_Node
{
c_Full_Node *m_Next;
};
};

template<class B> struct c_List_Bwd : public B
{
typedef typename B::c_Full_Node c_Full_Node;

struct c_Node : public B::c_Node
{
c_Full_Node *m_Prev;
};
};

template<class B, class T> struct c_List_Data : public B
{
public:
typedef typename B::c_Full_Node c_Full_Node;

struct c_Node : public B::c_Node
{
T m_Data;
};
};
};

//////////
// Composition needs more code using this technique, though the
// principle is simple enough.

struct c_Base
{
// We need a new initial mixin layer for each composition,
// since we need a new c_Full_Node type declaration for
// composition or else the definitions would conflict.
struct c_Full_Node;

// May as well define the empty base for the node here as well
struct c_Node {};
};

// Step by step composition
struct c_List_000 : public mixins::c_List_Base< c_Base > {};
struct c_List_001 : public mixins::c_List_Fwd < c_List_000 > {};
struct c_List_002 : public mixins::c_List_Bwd < c_List_001 > {};
struct c_List_003 : public mixins::c_List_Data< c_List_002, int > {};

// Cap off the composition

struct c_List : public c_List_003
{
struct c_Base::c_Full_Node : public c_List_003::c_Node {};
typedef c_Base::c_Full_Node c_Node;
};

//////////

int main(int argc, char* argv[])
{
c_List::c_Full_Node l_Node1;

l_Node1.m_Next = 0;
l_Node1.m_Prev = 0;
l_Node1.m_Data = 0;

return EXIT_SUCCESS;
}

 
Reply With Quote
 
 
 
 
Ninereeds
Guest
Posts: n/a
 
      04-03-2007

The whole of this thread so far turns out to be pointless. What I
should be doing is...

struct c_Node;
struct c_List : public c_Mixin2< c_Mixin1< c_Node* > > {};
struct c_Node : public c_List::c_Node {};

That is, the mixin layers simply accept a node identifier type as one
of the arguments at the start. This can be a pointer to a struct that
hasn't been defined yet (as above) or it could be an integer
identifier or whatever.

If the node identifier is a pointer, simply forward-reference the
pointed-to type.

This creates a problem with any algorithms that are defined by the
mixin-layers to work on the data structure, since they cannot assume
that node IDs are pointers. This can be resolved by using policies
passed to the appropriate mixin layers as arguments. Policy members
might be simple inlines to access fields using "->", or they might be
more complex functions to look nodes up in a separate container or
file.

Interesting, how policies and mix-in layers can complement each other.
Though since they're basically the template versions of existing OOP
techniques, it's no big surprise.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Access DataGrid declared in derived class from base class?? Jonas ASP .Net 1 08-12-2003 04:13 PM
How to access data or functions of the derived class if it was declared as the base class? Titan C++ 1 06-30-2003 03:04 PM



Advertisments