473,398 Members | 2,525 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,398 software developers and data experts.

A question about global operators vs. polymorphism

I just upgraded the compiler version we use in our product (we'd stuck with
a very old one for a long time) and it seems that things have moved forward
while we were off in the wilderness, and I'm just wanting to find out
whether the new compiler is doing the right thing or not here... So it may
turn out to be a product specific quirk (MS Visual Studio 2005), but I
wanted to find out what the strictly correct C++ language answer is first.

Basically we have our own very large framework. We have the usual streaming
classes (base classes for in and out streams, from which various specific
streaming classes are derived for files, memory, etc...) and the usual
templatized collection classes. We want our collections to be streamable,
where the instantiation element supports it, so we provide the usual global
(friend) streaming operators with each collection type, so that
streamability isn't forced on the instantiation element types, something
like:

template <class TTBinInStream&
operator>>(TBinInStream& strmToReadFrom, TFundStack<T>& colToStream)

template <class TTBinOutStream&
operator<<(TBinOutStream& strmToWriteTo, const TFundStack<T>& colToStream)

Where TBinInStream and TBinOutStream are the base classes for in and out
streams. This worked fine before, but now that we've upgraded the compiler,
it refuses to use these and so we end up with unresolved symbole link errors
anywhere that we stream a collection. I can move these operators into the
collection classes and make them members, and it works fine, but then of
course it forces streamability on all elements that collections are
instantiated for, which isn't feasible.

Has something occured in the C++ language spec in the intervening years that
would make this not resolve when one of the parameters is polymorphic? Or is
VC++ just being stupid? Or is there some new syntax required to deal with
this kind of thing?

BTW, just for funzies, I set up a test where I did the actual streaming
calls via the base stream classes and it still refuses to see these
operators, so maybe it's not related to the stream being polymorphic. But I
can't see any other reason why it would fail to see these operators and use
them.

I have to admit that I've been head down in product develoment for a long
time and haven't been following the language evolution for the last four
years or so, so I may be way behind the times here. I'm asking because we
decided to do this upgrade before going out with our 2.0 release and now
this is throwing a monkey wrench in the works and I'm a bit time constrained
to figure out whether to punt and go back to the old system for now or to
try to move forward. I've done a lot of searching, but for something like
this it's hard to find the right set of web search keywords to find someone
asking the same question, so I've not come up with the (probably many)
previously answered versions of this question so far.

-------------------------------------
Dean Roddey
Chairman/CTO, Charmed Quark Systems
www.charmedquark.com
Sep 24 '06 #1
3 1908
* Dean Roddey:
something like:
See the FAQ item about how to post a question about code that doesn't work.

Essentially, post a minimal but complete example that compiles.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 24 '06 #2
Given the nature of the question, no example that demonstrates the problem
is going to compile. And given that it's based on an enormous framework that
none of you have any access to, I'm not sure it would be very practical. But
here is a code outline that demonstrates the relevant parts:

//
// A templatized collection class with friend declarations for
// the streaming operators
//
template <class Tclass TFundStack : public TObject, public MDuplicable
{
public :
.......

protected :
friend TBinOutStream& operator<<
(
TBinOutStream& strmOut
, const TFundStack<T>& fcolToStream
);

friend TBinInStream& operator>>
(
TBinInStream& strmIn
, TFundStack<T>& fcolToStream
);

}

// In the same header, global streaming operators
template <class TTBinInStream&
operator>>(TBinInStream& strmToReadFrom, TFundStack<T>& colToStream)
{
}

template <class TTBinOutStream&
operator<<(TBinOutStream& strmToWriteTo, const TFundStack<T>& colToStream)
{
}

* TBinInStream and TBinOutStream are the base classes from which various
types of binary in/out streams are derived.

// In a test app, I'd create a stack, put something in it
TFundStack<tCIDLib::TCard4fcolStack(4);
fcolStack.Push(1);

// Then create an memory based output stream and stream the stack
TBinMBufOutStream strmTestOut(8192, 8192);
strmTest << fcolStack << kCIDLib::FlushIt;
The compiler doesn't find the global operators and I get an unresolved
symbol error on operator<<(TBinOutStream&, TFundStack<tCIDLib::TCard4>), so
it's not making the connection from the streaming of the stack to the
provided global streaming operator.

-------------------------------------
Dean Roddey
Chairman/CTO, Charmed Quark Systems
www.charmedquark.com

"Alf P. Steinbach" <al***@start.nowrote in message
news:4n************@individual.net...
>* Dean Roddey:
>something like:

See the FAQ item about how to post a question about code that doesn't
work.

Essentially, post a minimal but complete example that compiles.

Sep 24 '06 #3
I found the answer. I just happened to eventually luck onto an indentical
question being asked by someone else. Basically the fix is that the friend
declarations needed to the parameterized. So this:

friend TBinOutStream& operator<<
(
TBinOutStream& strmOut
, const TFundStack<T>& fcolToStream
);

needed to become:

friend TBinOutStream& operator<< <T>
(
TBinOutStream& strmOut
, const TFundStack<T>& fcolToStream
);

I'm not sure if that is a VC++'ism that really isn't required by the
language itself, or if it always was required and my old VC++ version just
wasn't enforcing it and therefore I never understood the necessity for
parameterizing the friend declaration.

-------------------------------------
Dean Roddey
Chairman/CTO, Charmed Quark Systems
www.charmedquark.com

"Alf P. Steinbach" <al***@start.nowrote in message
news:4n************@individual.net...
>* Dean Roddey:
>something like:

See the FAQ item about how to post a question about code that doesn't
work.

Essentially, post a minimal but complete example that compiles.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Sep 24 '06 #4

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

Similar topics

0
by: Joe Blow via DotNetMonster.com | last post by:
Hello, I have a design problem involving class instances as global variables. To give you my background, I've programmed lots in Java, and most of it has been large class structures. I'm...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
27
by: Chad | last post by:
The problem is: Write a recursive version of the function reverse(s), which reverses the string s in place. In "The C Answer Book", Second Edition, near the bottom of page 95, the authors say...
5
by: Shak | last post by:
Hi all. I was led to believe that static methods were not inherited by their subclasses (and since that makes sense, rightly so). However, a subclass I've written is using it's (abstract)...
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
1
by: dilabox | last post by:
Hello, I have overloaded the global new, delete, new and delete operators inside a "static library". The library uses private memory allocation routines that must not be accessible from other...
5
by: puzzlecracker | last post by:
I don't recall whether operators, which are members of the class, are intherited in subclasses? thanks
20
by: Aaron Gray | last post by:
There does not seem too be anyway to test if two jQuery references are the same element. Given :- ... <div id="1"></div .... Then :- alert( $("#1") == $("#1"))
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.