T
Thomas Matthews
Hi,
I am converting my table and record classes into templates.
My issue is the syntax of declaring a friend class within
the template. I have searched the C++ FAQ Lite (web),
the C++ newsgroups, "Thinking In C++" to no avail.
Background
----------
My table is a collection of <integer, string> pairs, in
which the string is a fixed width that is specialized.
One specialization of the table may have a 32 length
string and another 64. The table is also a Singleton.
The record class is of the form <integer, string>.
The integer portion, the primary key, is hidden from
public usage, so that the class behaves like a string.
When the class is written as a field of a record, the
integer portion will be written out.
Part of the interface is for the table to be a friend
of the record. This allows the table to set the primary
key without giving access or knowledge of the primary
key to the general public.
The Code
---------
Here is my code:
// File Name_Id_Table.hpp
template <typename Record_Class,
const char * TABLE_NAME>
class Name_Id_Table
{
//...
public:
void load_from_table(Record_Class& rc)
{
rc.id = get_id_from_table();
rc.name = get_name_from_table();
}
};
// File Name_Id.hpp
#include <string>
using std::string
template <int MAX_STRING_WIDTH>
class Name_Id
{
// ...
/* The following line is what I need help with */
template <> friend class<Name_ID, ????> Name_Id_Table;
private:
int id;
string name;
};
// File main.cpp
#include "Name_Id_Table.hpp"
#include "Name_Id.hpp"
const char * TITLE_TABLE_NAME = "Titles";
/* Here is another issue I'm having problems with.
* I want to declare the types but am having syntax
* issues.
*/
typedef Name_Id<64> Title;
typedef Name_Id_Table<Title, TITLE_TABLE_NAME> Title_Table;
int main(void)
{
Title t;
Title_Table table;
table.load_from_table(t);
return EXIT_SUCCESS;
}
In the database, I will have three Name-ID tables:
Title, Author, Publisher. The string widths will
differ and they will have different table names.
Otherwise they have the same functionality. Each
table is a singleton; the Title table will not
contain author or publisher entries. Similarly
with Author and Publisher tables.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
I am converting my table and record classes into templates.
My issue is the syntax of declaring a friend class within
the template. I have searched the C++ FAQ Lite (web),
the C++ newsgroups, "Thinking In C++" to no avail.
Background
----------
My table is a collection of <integer, string> pairs, in
which the string is a fixed width that is specialized.
One specialization of the table may have a 32 length
string and another 64. The table is also a Singleton.
The record class is of the form <integer, string>.
The integer portion, the primary key, is hidden from
public usage, so that the class behaves like a string.
When the class is written as a field of a record, the
integer portion will be written out.
Part of the interface is for the table to be a friend
of the record. This allows the table to set the primary
key without giving access or knowledge of the primary
key to the general public.
The Code
---------
Here is my code:
// File Name_Id_Table.hpp
template <typename Record_Class,
const char * TABLE_NAME>
class Name_Id_Table
{
//...
public:
void load_from_table(Record_Class& rc)
{
rc.id = get_id_from_table();
rc.name = get_name_from_table();
}
};
// File Name_Id.hpp
#include <string>
using std::string
template <int MAX_STRING_WIDTH>
class Name_Id
{
// ...
/* The following line is what I need help with */
template <> friend class<Name_ID, ????> Name_Id_Table;
private:
int id;
string name;
};
// File main.cpp
#include "Name_Id_Table.hpp"
#include "Name_Id.hpp"
const char * TITLE_TABLE_NAME = "Titles";
/* Here is another issue I'm having problems with.
* I want to declare the types but am having syntax
* issues.
*/
typedef Name_Id<64> Title;
typedef Name_Id_Table<Title, TITLE_TABLE_NAME> Title_Table;
int main(void)
{
Title t;
Title_Table table;
table.load_from_table(t);
return EXIT_SUCCESS;
}
In the database, I will have three Name-ID tables:
Title, Author, Publisher. The string widths will
differ and they will have different table names.
Otherwise they have the same functionality. Each
table is a singleton; the Title table will not
contain author or publisher entries. Similarly
with Author and Publisher tables.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library