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 5 1645
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<typena me T>
CArchive& operator<<(CArc hive& 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?
Create a class RawBlock
class RawBlock
{
public:
void* Data;
size_t Size;
RawBlock(void* BlockData,size_ t
BlockSize):Data (BlockData),Siz e(BlockSize) {}
};
Then, you can do that:
ar << RawBlock(vp,siz eof(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,siz eof(XXX));
Second : A write method in your CArchive class:
ar.write(vp, sizeof(XXX))
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.
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 ?
" 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 :( This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 a 'std::basic_ostream<charT, traits>'.
For some reason, it does not work. The compiler always complains:
|
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 seen commercial programs
print output to the screen as well as to a log file.
depending on the user and other situations, i might want to turn off one of
the outputs or maybe even both outputs.
so, i want a single line with operator << function...
|
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 FUNCTION!!
|
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;
|
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>& lst );
| |
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( const std::string& Name, const float Value );
float Value( const std::string& Name ) const;
float Sum( const std::string& Name ) const;
|
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 T>
DataDest& operator<<(DataDest& d, const T& t)
{
t.writeTo(d);
|
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;
}
|
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.
#include <iostream>
template <typename T>
class TC
|
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,...
|
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...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |