473,785 Members | 2,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading operator <<

Hello,

I'm trying to overload operator << for class CString, which has an
operator const char *. I thought the following code would do:

template <typename T>
inline std::basic_ostr eam<T> operator<<(
const std::basic_ostr eam<T>& p_Stream,
const CString& p_String)
{
p_Stream << static_cast<con st T*>(p_String) << std::flush;
return p_Stream;
}

But instead the operator calls itself recursively ad-infinitum. Can
someone explain why?
Bart.

Jul 23 '05 #1
4 1516
<ba***********@ gmail.com> schrieb im Newsbeitrag
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Hello,

I'm trying to overload operator << for class CString, which has an
operator const char *. I thought the following code would do:

template <typename T>
inline std::basic_ostr eam<T> operator<<(
const std::basic_ostr eam<T>& p_Stream,
const CString& p_String)
{
p_Stream << static_cast<con st T*>(p_String) << std::flush;
return p_Stream;
}

But instead the operator calls itself recursively ad-infinitum. Can
someone explain why?


I'm surprised that it calls anything at all. It shouldn't even compile:

1. p_Stream is a reference to a constant basic_ostream, but operator<<(char
const*) is not a const member.
2. Your operator<< returns a copy of p_Stream, but basic_ostreams are not
copy constructable.

Which compiler are you using?

Heinz
Jul 23 '05 #2
>> I'm surprised that it calls anything at all. It shouldn't even compile:

1. p_Stream is a reference to a constant basic_ostream, but
operator<<(char const*) is not a const member.
2. Your operator<< returns a copy of p_Stream, but basic_ostreams are
not copy constructable.

Which compiler are you using?


Oops. That was obviously my mistake, but VC++ 6.0 didn't complain one
bit. It happily called CString's implicit constructor and then operator
<< again.

Thanks.

Jul 23 '05 #3
Do you need to add &
at end of inline std::basic_ostr eam<T>
H.

Jul 23 '05 #4
>> Do you need to add &
at end of inline std::basic_ostr eam<T>


Yes. I need to pass a non const reference and return this reference. It
was a mistake on my part, but the compiler accepted it even though
there's no copy ctor for basic_ostream.

Jul 23 '05 #5

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

Similar topics

3
2035
by: Robert Wierschke | last post by:
Hi I want to overload the operator<< for a class Vector. class Vector { double x; double y; double z;
10
1660
by: pmatos | last post by:
Hi all, I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {} const std::string * getName() { return name; }
8
8381
by: jois.de.vivre | last post by:
Hi, I'm having some trouble overloading the << operator. I have the following, very simple code: #include <iostream> using namespace std; class test { private: int val;
3
1658
by: Suresh Tri | last post by:
Hi all, I was trying to overload '<' operator for (varchar,varchar). But in the function which handles the comparision I want to use the previous '<' operator.. but it is going into a recursion. My simplified code looks like : create or replace function orastringcmp (varchar, varchar) returns boolean as 'declare
6
2448
by: Andreas | last post by:
Hi, to output the private data of a class I want to overload the operator<<. The Output should be written in a string-variable. To do this i have written the attached code: But the program-compilation aborts with the following error message: StreamKoord.cpp: In function `int main()':
1
2134
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get the same error. Here is the code of the class template< class T > class Matrix { friend ostream &operator << <>( ostream &, const Matrix< T > & ); friend istream &operator >> <>( istream &, Matrix< T > & ); public: ...
6
1500
by: Peter v. N. | last post by:
Hi all, Maybe this has been asked a million times before. In that case I'm sorry for being to lazy to search the Internet or look it up in a decent C++ reference: I read in O'Reilly's C++ Pocket reference (see also http://www.oreilly.com/catalog/cpluspluspr/errata/ , btw not that bad), that the operator << has to be overloaded like this:
3
1730
by: johnmmcparland | last post by:
Hi all, I know it is possible to overload the operators and < in C++ but how can I do this. Assume I have a class Date with three int members, m_day, m_month and m_year. In the .cpp files I have defined the < and operators; bool Date::operator<(const Date& d) {
8
1729
by: Micko1 | last post by:
hi there, I have an issue when overloading <<. string operator << (room * &currentPlayerLocation); string room::operator << (room * &currentPlayerLocation) {
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9952
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.