Is this the correct way to use type specializations

Discussion in 'C++' started by fabian.lim@gmail.com, Sep 7, 2008.

  1. Guest

    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
    *************************
    , Sep 7, 2008
    #1
    1. Advertising

  2. On 2008-09-07 09:54, 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
    Erik Wikström, Sep 7, 2008
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Sep 7, 2008
    #3
  4. Alex Guest

    It's correct in syntax, but looks strange.
    Alex, Sep 7, 2008
    #4
  5. Guest

    On Sep 7, 12:54 am, 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
    , Sep 8, 2008
    #5
  6. Guest

    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, wrote:
    > On Sep 7, 12:54 am, 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
    , Sep 8, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tseng, Ling-hua
    Replies:
    0
    Views:
    435
    Tseng, Ling-hua
    Dec 8, 2003
  2. Tom Richardson
    Replies:
    1
    Views:
    323
    Victor Bazarov
    Dec 19, 2003
  3. Thomas Matthews

    Order of template specializations

    Thomas Matthews, Feb 26, 2004, in forum: C++
    Replies:
    0
    Views:
    333
    Thomas Matthews
    Feb 26, 2004
  4. CoolPint
    Replies:
    4
    Views:
    356
    CoolPint
    Sep 18, 2004
  5. christopher diggins
    Replies:
    4
    Views:
    399
    christopher diggins
    Dec 21, 2004
Loading...

Share This Page