🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by God_Of_Pain Mon, 09 Apr 2001 15:23
operator overloading syntax question
#57
Author: God_Of_Pain
Date: Mon, 09 Apr 2001 15:23
190 lines
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); 
}
Re: operator overloading syntax question
#58
Author: Roman Fietze
Date: Tue, 10 Apr 2001 06:36
66 lines
2350 bytes
Hello,

God_Of_Pain <silicontao_roy@technologist.com> writes:

> ...
> Any help would be great.
> ...

I would say that is somewhat overkill. First of all I would store the
port bits in one BYTE variable (typedef unsigned char BYTE; class
TPortMap ... private: BYTE PortBits; ...) inside your class. Setting
single bits could probably be done like this:

void TPortMap::SetBits(BYTE bits)
{
	PortBits |= bits;
	WritePortBits();
}

void TPortMap::SetBit(unsigned short i)
{
	PortBits |= (1U << i);
	WritePortBits();
}


Querying could be done like

BYTE TPortMap::GetBits() const
{
	return (PortBits);
}

bool TPortMap::GetBit(unsigned short i) const
{
	return (PortBits & (1U << i));
}


and you operators could be something like

bool operator[] (unsigned short i) const
{
	return (GetBit(i));
}


void operator= (BYTE bits)
{
	SetBits(bits);
}



What would be nice is something like

MyPort[5] = true;

but that's not doable this way.


Roman

-- 
Roman Fietze (Mail Code 6)     roman.fietze@de.heidelberg.com
Heidelberg Digital Finishing GmbH, Germany     DDF-T SWEC ESW
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads