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

can operator << accept two parameters?


Hi,
I was requested to write a code similar to this:

CArchive ar; //Not mfc CArchive.....
....
void * vp = &XXX; // any object.

ar << vp, sizeof(XXX);

The only solution i found is to implements both operator <<
and operator , on the CArchive object
when accepting void* the CArchive will store it in a temporary member
pointer
untill the operator , will be called

I dont conisider this solution to be good at all,and i am looking for
some other way

Thanks in advance

Apr 3 '06 #1
5 1628
TOMERDR wrote:

Hi,
I was requested to write a code similar to this:

CArchive ar; //Not mfc CArchive.....
...
void * vp = &XXX; // any object.

ar << vp, sizeof(XXX);

The only solution i found is to implements both operator <<
and operator , on the CArchive object
Why not

ar << vp << sizeof(XXX);

? Or how about letting operator << figure the size out and converting to
void* itself? Like:

template<typename T>
CArchive& operator<<(CArchive& lhs, const T& rhs)
{
void* vp = &rhs;
size_t size = sizeof(rhs);
// call some member function of ar that does The Right Thing(tm)
}
when accepting void* the CArchive will store it in a temporary member
pointer untill the operator , will be called

I dont conisider this solution to be good at all,and i am looking for
some other way


Well, does it have to be exactly that syntax?

Apr 3 '06 #2
Create a class RawBlock

class RawBlock
{
public:
void* Data;
size_t Size;
RawBlock(void* BlockData,size_t
BlockSize):Data(BlockData),Size(BlockSize) {}
};

Then, you can do that:
ar << RawBlock(vp,sizeof(XXX));

This syntax:
ar << vp, sizeof(XXX);
Would be very confusing. And doesn't work as expected:
ar << vp, sizeof(XXX) << 4;
Is equivalent to:
(ar << vp), (sizeof(XXX)<<4)

Logically, one could want (but still I think it is confusing...)
ar << (vp, sizeof(XXX));

But it is not possible to overload comma operator with only non-class
types parameters.
However it would be an abuse of operator overloading.

There are two sensible ways to do what you want:
First : the method I described:
ar << RawBlock(vp,sizeof(XXX));
Second : A write method in your CArchive class:
ar.write(vp, sizeof(XXX))

Apr 3 '06 #3

I thought about raw block also ,the problem is that this is an
assignment in a class i am taking
and i want to do exactly what i told.
I dont want to use my knowledge in templates and other stuff not
teached yet in the class.

Is there some why i can globaly overload operator , to create a raw
block from a void* and a size_t
in such way i can maintain the exact syntax which was requested.
Thanks in advance.

Apr 3 '06 #4
TOMERDR wrote :
Hi,
I was requested to write a code similar to this:

CArchive ar; //Not mfc CArchive.....
...
void * vp = &XXX; // any object.

ar << vp, sizeof(XXX);


Why not using XXX directly ?
Apr 3 '06 #5
" I dont want to use my knowledge in templates and other stuff not
teached yet in the class."

he must be suffering in a bad course teached by an inept teacher :(

Apr 4 '06 #6

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

Similar topics

4
by: Guenther Brunthaler | last post by:
Hi template specialists, I have a problem with the code listed below. What I wanted to do is defining an operator<< that is able to output a 'matrix<ELEMENT_T, INDEX_T>::subrange' object into...
2
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
5
by: cuneyt | last post by:
Hi all: I have defined the template template<class T> class CGroup { friend std::ostream & operator<<( std::ostream &lhs, const CGroup &rhs ); private: int nSize;
8
by: Ook | last post by:
This is my code in it's entireity: #include <iostream> using namespace std; template <typename T> class SortedList { public: friend ostream& operator<< (ostream& os, const SortedList<T>&...
3
by: Jim Langston | last post by:
I have a CSkill class which is rather complex as it is recursive. That is: class CSkill { public: CSkill( std::string Name, float Value ): Name_( Name ), Value_( Value ) {}; void Update(...
11
by: fungus | last post by:
I have some classes which have a "writeTo()" function but no operator<<. I want to fix it so that they all have an operator<< (for consistency). Can I do something like this: template <class...
2
by: soy.hohe | last post by:
Hi all I have a class StreamLogger which implements operator << in this way: template <class TStreamLogger& operator<<(const T& t) { <print the stuff using fstream, etc.> return *this; }
1
by: Stuart Golodetz | last post by:
Hi guys, I'm trying to making an instance of a templated operator<< for a templated class a friend of that class (see below), to allow it to access the class internals for output purposes. ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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.