473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Circular template reference

I was making a part of an application more and more generic when I suddenly
ended up with code like this:

template <class T>
struct MenuItems {
T &m;
MenuItems(T &m);
};

template <class T>
struct Menu {
T items;
Menu() : items(*this) { }
};

template <class T>
MenuItems<T>::MenuItems(T &m) : m(m) { }

If these classes were no templates the code would compile without any
problems. But now with these templates it's impossible to instantiate them
with each other. You end up with something like
Menu<MenuItems<Menu<MenuItems<Menu ... Is there any trick or I went a bit
too far and have to go back to complete types?

Boris
Oct 14 '06 #1
3 5075
Boris wrote:
I was making a part of an application more and more generic when I suddenly
ended up with code like this:

template <class T>
struct MenuItems {
T &m;
MenuItems(T &m);
};

template <class T>
struct Menu {
T items;
Menu() : items(*this) { }
};
Are you suggesting that a Menu can only hold one item?
>
template <class T>
MenuItems<T>::MenuItems(T &m) : m(m) { }

If these classes were no templates the code would compile without any
problems. But now with these templates it's impossible to instantiate them
with each other. You end up with something like
Menu<MenuItems<Menu<MenuItems<Menu ... Is there any trick or I went a bit
too far and have to go back to complete types?
I'm still failing to see why an item needs a pointer to its own menu /
container.
If the menu needs to somehow modify the display, thats not the item's
concern.

#include <iostream>
#include <ostream>
#include <list>
#include <iterator>

template < class T >
struct MenuItem
{
MenuItem( T obj ) : t(obj) { }
private:
T t;
/* friend op<< */
friend std::ostream& operator<<(std::ostream& os, const MenuItem< T
>& r_i)
{
return os << r_i.t;
}
};

template< class T >
struct Menu
{
std::list< T menuitems; // or vector, deque, set
};

template< class T >
std::ostream& operator<<(std::ostream& os, const Menu< T >& r_m)
{
std::copy( r_m.menuitems.begin(),
r_m.menuitems.end(),
std::ostream_iterator< T >(os,"\n") );
return os;
}

int main()
{
Menu< MenuItem< std::string menu;
menu.menuitems.push_back(MenuItem< std::string >("string 0"));
menu.menuitems.push_back(MenuItem< std::string >("string 1"));
menu.menuitems.push_back(MenuItem< std::string >("string 2"));

std::cout << menu;
}

/*
string 0
string 1
string 2
*/

Oct 14 '06 #2
Boris wrote:
I was making a part of an application more and more generic when I
suddenly ended up with code like this:

template <class T>
struct MenuItems {
T &m;
MenuItems(T &m);
};

template <class T>
struct Menu {
T items;
Menu() : items(*this) { }
};

template <class T>
MenuItems<T>::MenuItems(T &m) : m(m) { }

If these classes were no templates the code would compile without any
problems. But now with these templates it's impossible to instantiate
them with each other. You end up with something like
Menu<MenuItems<Menu<MenuItems<Menu ... Is there any trick or I went a
bit too far and have to go back to complete types?
OK, you have found out that such circular reference is unsolvable in
C++ terms. So, what do you want from us? Your design is apparently
either too generic or not generic enough. Too generic in the sense
that you want both of those classes not be concrete. Not generic
enough in the sense that there is a dependency between them that
causes you grief. You eigher need to break the dependency or make
one of them concrete, or make both of them depend on the third type.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 14 '06 #3
Victor Bazarov wrote:
Boris wrote:
>I was making a part of an application more and more generic when I
suddenly ended up with code like this:

template <class T>
struct MenuItems {
T &m;
MenuItems(T &m);
};

template <class T>
struct Menu {
T items;
Menu() : items(*this) { }
};

template <class T>
MenuItems<T>::MenuItems(T &m) : m(m) { }

If these classes were no templates the code would compile without any
problems. But now with these templates it's impossible to instantiate
them with each other. You end up with something like
Menu<MenuItems<Menu<MenuItems<Menu ... Is there any trick or I went a
bit too far and have to go back to complete types?

OK, you have found out that such circular reference is unsolvable in
C++ terms. So, what do you want from us? Your design is apparently
I got the answer to my question: It's unsolvable and I either have to change
the classes or the programming language.

Thanks,
Boris
Oct 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vera | last post by:
I have two assemblies that each consist of several classes. Each object instantiated from those classes can have one or more child- and/or parentobjects that are also instantiated from those...
4
by: ro86 | last post by:
Hello everyone! I am a newbie to C++ (~1 Week experience) and I have a few months of experience with object-oriented languages (Objective-C). I am currently working just for fun on a particle...
6
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
12
by: Frank Rizzo | last post by:
I have a circular reference between 2 classes in the same project (i.e. each class refers to the other). The app runs fine and I am seeing no issues, which kind of surprised me. Are there any...
6
by: Stephen Robertson | last post by:
We are currently in a dead end with a circular reference issue using vb.net, and are hoping someone might help us resolve it. Idea... We have frmmain calling frmperson (dim f as new frmperson)...
3
by: Solution Seeker | last post by:
Hi All, I am here with a Query and need a Solution for it. The Query is as Follows, We have 3 Projects in a Solution - Say UI, CMN and PRD First One Deals with UI Forms Second One Deals...
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
8
by: nyhetsgrupper | last post by:
I have written a windows service and want to expose a web based user interface for this service. I then wrote a class library containing a ..net remoting server. The class library have a method...
3
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I Have a solution with about 50 projects and each project have References to 1 to n of the projects in the solution. I try go to a project and try to add a reference to another project and I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.