Is this the correct way to use type specializations

F

fabian.lim

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;
}

};

//********************** END: BINARY VARIABLES
*************************
 
E

Erik Wikström

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

Kai-Uwe Bux

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
 
A

acehreli

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
 
F

fabian.lim

Hi all,

Thank you all for your kind comments. Thanks to Ali I learnt the new
concept of explicit. Thanks again all.
 

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
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top