can operator << accept two parameters?

T

TOMERDR

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
 
R

Rolf Magnus

TOMERDR said:
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?
 
S

SuperKoko

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))
 
T

TOMERDR

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.
 
L

loufoque

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 ?
 
D

Diego Martins

" 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 :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top