| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Hi, I am a newbie to C++, however, I programmed quite alot in C before. This is my first attempt at OOP. In the following segment of my code, Im trying to implement (as an exercise) a binary variable class. I know this is extremely redundant but im learning so please bearwith me . Anyways, Notice that I have have two "=" operatoroverloads. The 1st case takes care of x=y, where both x and y are binary variables. The 2nd case takes care of x=1 or x=0. As you can see, both chunks of code are extremely redundant. So my question is, is there any better way to do this? //********************** START: BINARY VARIABLES ************************* class Binary { char bit; public: //************************************************** ****** // Constructors and Destructor Binary() { bit =0; }; ~Binary(){ }; //************************************************** ****** // Copiers //copy constructors Binary( const Binary& val) { bit = val.bit; }; Binary( const int& val) { bit = val; }; //int specialization //assignments Binary& operator=(const Binary& rhs) { bit = rhs.bit; return *this; } Binary& operator=(const int& rhs) { //int specialization bit = rhs; return *this; } }; //********************** END: BINARY VARIABLES ************************* |
|
#2
| |||
| |||
| On 2008-09-07 09:54, fabian.lim@gmail.com wrote: > Hi, > > I am a newbie to C++, however, I programmed quite alot in C before. > This is my first attempt at OOP. In the following segment of my code, > Im trying to implement (as an exercise) a binary variable class. I > know this is extremely redundant but im learning so please bear> with me . Anyways, Notice that I have have two "=" operator> overloads. The 1st case takes care of x=y, where both x and y are > binary variables. The 2nd case takes care of x=1 or x=0. As you can > see, both chunks of code are extremely redundant. So my question is, > is there any better way to do this? > > //********************** START: BINARY VARIABLES > ************************* > class Binary { > char bit; > public: > > //************************************************** ****** > // Constructors and Destructor > Binary() { bit =0; }; > ~Binary(){ }; > > //************************************************** ****** > // Copiers > //copy constructors > Binary( const Binary& val) { bit = val.bit; }; > Binary( const int& val) { bit = val; }; //int specialization > > //assignments > Binary& operator=(const Binary& rhs) { > bit = rhs.bit; > return *this; > } > Binary& operator=(const int& rhs) { //int specialization > bit = rhs; > return *this; > } > > }; Looks fine to me. If you look closely you see that there is not much redundancy at all, the only thing both the assignment operators have in common is the "return *this;" statement, and you'll find them in all assignment operators. If you do end up with a lot in duplication it might be worth to break it out into a private function with does the common parts and then call that function from those places that needs it. Just a question tough, if the class handles binary values you really should check the int value in the constructor and assignment operator so you do not end up storing a value like 34 instead of just 1 or 0. You might also want to us an initialisation list instead of assignments in the constructor (and drop the ; after the constructor bodies): Binary( const Binary& val) : bit(val.bit) { } Binary( const int& val) : bit(val > 0 ? 1 : 0) { } Binary& operator=(const int& rhs) { bit = rhs > 0 ? 1 : 0; return *this; } If you use something like this you will also reduce the amount of redundancy between the assignment operators. -- Erik Wikström |
|
#3
| |||
| |||
| fabian.lim@gmail.com wrote: > Hi, > > I am a newbie to C++, however, I programmed quite alot in C before. > This is my first attempt at OOP. In the following segment of my code, > Im trying to implement (as an exercise) a binary variable class. I > know this is extremely redundant but im learning so please bear> with me . Anyways, Notice that I have have two "=" operator> overloads. The 1st case takes care of x=y, where both x and y are > binary variables. The 2nd case takes care of x=1 or x=0. As you can > see, both chunks of code are extremely redundant. So my question is, > is there any better way to do this? > > //********************** START: BINARY VARIABLES > ************************* > class Binary { > char bit; bool bit; > public: > //************************************************** ****** > // Constructors and Destructor > Binary() { bit =0; }; > ~Binary(){ }; > > //************************************************** ****** > // Copiers > //copy constructors > Binary( const Binary& val) { bit = val.bit; }; > Binary( const int& val) { bit = val; }; //int specialization > > //assignments > Binary& operator=(const Binary& rhs) { > bit = rhs.bit; > return *this; > } > Binary& operator=(const int& rhs) { //int specialization > bit = rhs; > return *this; > } > > }; > > //********************** END: BINARY VARIABLES > ************************* Just a thought: instead of using Binary var; I would go with bool var; I do not see any reason to fix what is not broken. There are many aspects of C++ that need a little support. This isn't one of them. Best Kai-Uwe Bux |
|
#4
| |||
| |||
| It's correct in syntax, but looks strange. |
|
#5
| |||
| |||
| On Sep 7, 12:54 am, fabian....@gmail.com wrote: > Notice that I have have two "=" operator > overloads. The 1st case takes care of x=y, where both x and y are > binary variables. The 2nd case takes care of x=1 or x=0. As you can > see, both chunks of code are extremely redundant. So my question is, > is there any better way to do this? The better way is to not define most of those functions. I knowyou're learning, but I wanted to make sure that you didn't think that you had to write all those functions. > class Binary { > char bit; > public: > > //************************************************** ****** > // Constructors and Destructor > Binary() { bit =0; }; > ~Binary(){ }; Stray semicolons are illegal in some contexts. I am not sure whether the ones up there are, but they are at least unnecessary. > Binary( const Binary& val) { bit = val.bit; }; That's equivalent to what the compiler would generate. > Binary( const int& val) { bit = val; }; //int specialization I think "overload" is a better word in that context. > Binary& operator=(const Binary& rhs) { > bit = rhs.bit; > return *this; > } > Binary& operator=(const int& rhs) { //int specialization > bit = rhs; > return *this; > } That overload of operator= is unnecessary because your Binary(int) constructor is not 'explicit' (see the 'explicit' keyword). If you leave that constructor non-explicit, as is in your code, then assignment to an int would still work. How about this simple version which is functionally equivalent to yours: class Binary { bool bit; public: Binary(const int& val = 0) : bit(val) {} }; int main() { Binary b0; Binary b1(1); Binary b2 = b0; b1 = b2; b1 = 2; } In general though, there will be redundancy in the constructors, because there is no syntax to "call" one constructor from another. The solution is perhaps not to overload constructors too much or not to have very many members. Ali |
|
#6
| |||
| |||
| Hi all, Thank you all for your kind comments. Thanks to Ali I learnt the new concept of explicit. Thanks again all. On Sep 7, 6:01 pm, acehr...@gmail.com wrote: > On Sep 7, 12:54 am, fabian....@gmail.com wrote: > > > Notice that I have have two "=" operator > > overloads. The 1st case takes care of x=y, where both x and y are > > binary variables. The 2nd case takes care of x=1 or x=0. As you can > > see, both chunks of code are extremely redundant. So my question is, > > is there any better way to do this? > > The better way is to not define most of those functions. I know> you're learning, but I wanted to make sure that you didn't think that > you had to write all those functions. > > > class Binary { > > char bit; > > public: > > > //************************************************** ****** > > // Constructors and Destructor > > Binary() { bit =0; }; > > ~Binary(){ }; > > Stray semicolons are illegal in some contexts. I am not sure whether > the ones up there are, but they are at least unnecessary. > > > Binary( const Binary& val) { bit = val.bit; }; > > That's equivalent to what the compiler would generate. > > > Binary( const int& val) { bit = val; }; //int specialization > > I think "overload" is a better word in that context. > > > Binary& operator=(const Binary& rhs) { > > bit = rhs.bit; > > return *this; > > } > > Binary& operator=(const int& rhs) { //int specialization > > bit = rhs; > > return *this; > > } > > That overload of operator= is unnecessary because your Binary(int) > constructor is not 'explicit' (see the 'explicit' keyword). If you > leave that constructor non-explicit, as is in your code, then > assignment to an int would still work. > > How about this simple version which is functionally equivalent to > yours: > > class Binary > { > bool bit; > > public: > > Binary(const int& val = 0) > : > bit(val) > {} > > }; > > int main() > { > Binary b0; > Binary b1(1); > Binary b2 = b0; > > b1 = b2; > b1 = 2; > > } > > In general though, there will be redundancy in the constructors, > because there is no syntax to "call" one constructor from another. The > solution is perhaps not to overload constructors too much or not to > have very many members. > > Ali |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.