Article View: att.lang.c++
Article #57operator overloading syntax question
From: God_Of_Pain
Date: Mon, 09 Apr 2001 15:23
Date: Mon, 09 Apr 2001 15:23
190 lines
5815 bytes
5815 bytes
I have not done any operator overloading before, I have what I think is a
good start but there are a few things that don't seem to work. I was
hoping someone that is good with operator overloading could help me with
this. I could not find an documentation that explained the syntax of this
so I have written this by guessing and trial and error. This is what I have
so far, it should run on any C++ system.
What I am trying to do is mask 8 bool bits to a byte. I have an object
class of Port that contains 8 bits or bools and a byte. When a bool is
written to it should change the value of the byte. When the byte is written
to it should change the value of all 8 bools. When the port object instance
is read it should query the state of all 8 bits and form a byte and return
that value. Most of this works except when I write to PIN6 or PIN7 in the
object instance Writing to PIN0 through PIN5 work, and I can read the value
back from them. printf("PORTA = %d\n",PORTA); just prints "PORTA = 0", that
is wrong so I and not getting the byte value back. After I write to PIN6 or
PIN7 the printf seems to print a memory address. Why? Shouldn't
TPortMap::operator BYTE() return the byte value. Or is that not called when
I read the object instance? If not, how can I?
Any help would be great.
Thanks.
// Here is the code I am tring to make work
#include <stdio.h>
#define BYTE unsigned short int
class TPortMap
{
public:
BYTE PortValue;
bool PIN7,PIN6,PIN5,PIN4,PIN3,PIN2,PIN1,PIN0;
TPortMap &operator=(const BYTE NewValue);
TPortMap &operator++(int);
TPortMap &operator+(const int Incroment);
TPortMap &operator+=(const int Incroment);
TPortMap &operator+=(const TPortMap& );
operator BYTE();
void SetPort();
void SetBits(BYTE NewBits);
};
void TPortMap::SetBits(BYTE NewBits)
{
BYTE BitMask;
BitMask = NewBits;
PIN0 = BitMask & 0x01;
BitMask >>= 1;
PIN1 = BitMask & 0x01;
BitMask >>= 1;
PIN2 = BitMask & 0x01;
BitMask >>= 1;
PIN3 = BitMask & 0x01;
BitMask >>= 1;
PIN4 = BitMask & 0x01;
BitMask >>= 1;
PIN5 = BitMask & 0x01;
BitMask >>= 1;
PIN6 = BitMask & 0x01;
BitMask >>= 1;
PIN7 = BitMask & 0x01;
SetPort();
}
void TPortMap::SetPort()
{
BYTE BitMask;
BitMask = PIN7;
BitMask <<= 1;
BitMask += PIN6;
BitMask <<= 1;
BitMask += PIN5;
BitMask <<= 1;
BitMask += PIN4;
BitMask <<= 1;
BitMask += PIN3;
BitMask <<= 1;
BitMask += PIN2;
BitMask <<= 1;
BitMask += PIN1;
BitMask <<= 1;
BitMask += PIN0;
PortValue = BitMask;
}
TPortMap::operator BYTE()
{
SetPort();
printf("here\n");
return(PortValue);
}
TPortMap &TPortMap::operator+(const int Incroment)
{
BYTE BitMaskA, BitMaskB;
SetPort();
BitMaskA = PortValue;
BitMaskB = Incroment;
SetBits(BitMaskA + BitMaskB);
}
TPortMap &TPortMap::operator=(const BYTE NewValue)
{
BYTE BitMaskB;
SetPort();
BitMaskB = NewValue;
SetBits(BitMaskB);
}
TPortMap &TPortMap::operator+=(const TPortMap& Incroment)
{
BYTE BitMaskA, BitMaskB;
SetPort();
BitMaskA = PortValue;
BitMaskB = Incroment.PortValue;
SetBits(BitMaskA + BitMaskB);
}
TPortMap &TPortMap::operator+=(const int Incroment)
{
BYTE BitMaskA, BitMaskB;
SetPort();
BitMaskA = PortValue;
BitMaskB = Incroment;
SetBits(BitMaskA + BitMaskB);
}
TPortMap &TPortMap::operator++(int)
{
BYTE BitMaskA, BitMaskB;
SetPort();
BitMaskA = PortValue;
BitMaskB = 1;
SetBits(BitMaskA + BitMaskB);
}
const char version[]="0.0.2";
int main(int argc, char **argv)
{
int TempReturn;
TPortMap PORTA;
BYTE AByteValue;
PORTA = 4;
printf("PORTA = %d\n",PORTA);
PORTA.PIN5 = true;
// I changed PIN5 but I can't make the reading of PORTA call the
// overloaded operator BYTE(), I don't know how to make that happen.
printf("PORTA = %d\n",PORTA);
PORTA.PIN6 = true;
// This here setting PIN6 really screws up the app, when the number goes
// to print, it will be really wacked.
printf("PORTA = %d\n",PORTA);
PORTA++;
PORTA.PIN3 = 1;
printf("PORTA = %d\n",PORTA);
PORTA = PORTA + 6;
printf("PORTA = %d\n",PORTA);
if(PORTA.PIN7 == 0)
{
printf("PIN7 is off\n");
}
AByteValue = PORTA;
PORTA += (PORTA * 2);
PORTA += 5;
return(TempReturn);
}
Technical Details
Message-ID:
<otkA6.8297$%B2.1110673@news1.telusplanet.net>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!news.maxwell.syr.edu!sunqbc.risq.qc.ca!newsfeed.telusplanet.net!news1.telusplanet.net.POSTED!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fff06,94ced0dda0a89aaa,start X-Google-Attributes: gidfff06,public X-Google-Thread: 109fba,94ced0dda0a89aaa,start X-Google-Attributes: gid109fba,public X-Google-Thread: 10d4ba,94ced0dda0a89aaa,start X-Google-Attributes: gid10d4ba,public X-Google-ArrivalTime: 2001-04-09 08:24:06 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!news.maxwell.syr.edu!sunqbc.risq.qc.ca!newsfeed.telusplanet.net!news1.telusplanet.net.POSTED!not-for-mail From: God_Of_Pain <silicontao_roy@technologist.com> Subject: operator overloading syntax question Newsgroups: att.lang.c++,comp.lang.c++,comp.os.linux.embedded Followup-To: att.lang.c++ Reply-To: silicontao_roy@technologist.com Lines: 190 Organization: SiliconTao.com User-Agent: KNode/0.4beta4 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit Message-ID: <otkA6.8297$%B2.1110673@news1.telusplanet.net> Date: Mon, 09 Apr 2001 15:23:00 GMT NNTP-Posting-Host: 142.59.65.190 X-Trace: news1.telusplanet.net 986829780 142.59.65.190 (Mon, 09 Apr 2001 09:23:00 MDT) NNTP-Posting-Date: Mon, 09 Apr 2001 09:23:00 MDT