Topic: Typedefs and Overloading template functions
Author: tottinge@csci.csc.com (Tim Ottinger)
Date: 1996/08/10 Raw View
In article <m3ivavlhdh.fsf@gabi-soft.fr>, kanze@gabi-soft.fr (J. Kanze) wrote:
>tottinge@csci.csc.com (Tim Ottinger) writes:
>
>> struct CustomerKey{
>> // ...
>> typedef Customer MyObject;
>> // ...
>> };
>>
>> template<class IDClass>
>> IDClass::myObject&* getObject(IDClass x)
> ^^
>
>Is this a typo? If not, what is it supposed to mean. The function
>should return either a pointer, or a reference, or a reference to a
>pointer. On the other hand, there's no such thing as a pointer to a
>reference.
Yes, this is a typo. It was not supposed to have an & in it.
I must have landed a finger squarely between the 7 and 8 to
do that.
Of course, it should be one or the other, or a return by value.
Either case is okay with me. For now, consider it * as I'd
intended.
+----------------------------------------------------------------------------+
| Tim Ottinger, Sr Tech tottinge@csci.csc.com |
| CSC Communications Industry Services 217-351-8508x2420 |
| TRIS Division -- Cellular Billing and Support Fax 217-351-2640 |
+----------------------------------------------------------------------------+
| Requirements have always been the least stable part of any project I have |
| worked on. -- Robert Martin via Usenet 12/02/95 |
+----------------------------------------------------------------------------+
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1996/08/07 Raw View
tottinge@csci.csc.com (Tim Ottinger) writes:
> struct CustomerKey{
> // ...
> typedef Customer MyObject;
> // ...
> };
>
> template<class IDClass>
> IDClass::myObject&* getObject(IDClass x)
^^
Is this a typo? If not, what is it supposed to mean. The function
should return either a pointer, or a reference, or a reference to a
pointer. On the other hand, there's no such thing as a pointer to a
reference.
> {
> // ...
> }
>
> This way, to get an object from a key is a matter of:
>
> CustomerKey x;
> // ...
> MyCustomer* cptr = getObject(x);
Other than the above typo, I believe that this should work. There's
plenty of similar stuff in the standard library, and I've used similar
stuff a lot myself.
Such things do tend to stress the template instantiation mechanism, so
be prepared to run up against compiler bugs. (My examples won't
compile/link with g++, for example, nor with anything but the very
latest version of Sun CC.)
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: tottinge@csci.csc.com (Tim Ottinger)
Date: 1996/08/05 Raw View
[Moderator's note: This article is crossposted to comp.std.c++ and
comp.lang.c++.moderated, and followups are directed by default to
comp.std.c++. If you respond to this article, please make sure that
you send your response to the appropriate group(s). mha]
This is a 'relational database extended into c++' thing, but it grew into
a question I'd like to have a good answer to. I'll give background a bit
first, then the problem. Then I'll sit back and await your judgement:
What I'd really love to be able to do is have more-or-less opaque objects
which serve as identifiers for other objects and nothing else. These are
essentially "keys" and "foreign keys".
struct CustomerKey{
// ...
long customerNumber;
};
This is easy enough, of course. These can be embedded in lots-o-classes:
class CableDataSubscription {
// ...
CustomerKey myCust;
EquipmentKey myEquip;
// ...
};
Then I thought it would be "cool" to have a very simple function which pulled
data out of a type-specific cache (i.e. all Customers are cached together, all
Equipment cached together, etc). This way, my caching is simple, and I still
don't have to have all types descending from a common base (which would
not be good in this world).
I conceived of a cute ploy using typedefs and template functions, but I can't
find a compiler which likes the idea. I actually crash the compilers with
this, so I will do it another way, but I thought maybe you would like tell me
why this can't be done:
struct CustomerKey{
// ...
typedef Customer MyObject;
// ...
};
template<class IDClass>
IDClass::myObject&* getObject(IDClass x)
{
// ...
}
This way, to get an object from a key is a matter of:
CustomerKey x;
// ...
MyCustomer* cptr = getObject(x);
It's not that I can't work around it, but it seemed like such a good idea, I'm
not sure why it's not possible. Maybe you can tell me. Should it be?
+----------------------------------------------------------------------------+
| Tim Ottinger, Sr Tech tottinge@csci.csc.com |
| CSC Communications Industry Services 217-351-8508x2420 |
| TRIS Division -- Cellular Billing and Support Fax 217-351-2640 |
+----------------------------------------------------------------------------+
| Requirements have always been the least stable part of any project I have |
| worked on. -- Robert Martin via Usenet 12/02/95 |
+----------------------------------------------------------------------------+
- ---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
- --
Matt Austern
SGI: MTI Compilers Group
austern@isolde.mti.sgi.com
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]