Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use the second constructor?

35 views
Skip to first unread message

fl

unread,
May 11, 2017, 2:22:26 PM5/11/17
to
Hi,

I have a few code to use the below template. I figure out the 3rd constructor
usage. How to use the 2nd constructor? Could you analyze it for me?


Thanks,

----------------------------
sc_biguint<3> atomw(5);
const sc_biguint<4> atomw1(2);

atomw=atomw1; // 3 third constructor
...........
template< int W >
class sc_biguint
: public sc_unsigned
{
public:

// constructors

sc_biguint()
: sc_unsigned( W )
{}

sc_biguint( const sc_biguint<W>& v ) // 2nd
: sc_unsigned( W )
{ *this = v; }

sc_biguint( const sc_unsigned& v ) // 3 third
: sc_unsigned( W )
{ *this = v; }

Alf P. Steinbach

unread,
May 11, 2017, 3:13:40 PM5/11/17
to
On 11-May-17 8:22 PM, fl wrote:
>
> I have a few code to use the below template. I figure out the 3rd constructor
> usage. How to use the 2nd constructor? Could you analyze it for me?

The second constructor is an ordinary copy constructor.


> ----------------------------
> sc_biguint<3> atomw(5);
> const sc_biguint<4> atomw1(2);
>
> atomw=atomw1; // 3 third constructor

That's an assignment.

Copy construction is more like

sc_biguint a_copy = atomw1;

or

sc_biguint a_copy{ atomw1 };


> ...........
> template< int W >
> class sc_biguint
> : public sc_unsigned
> {
> public:
>
> // constructors
>
> sc_biguint()
> : sc_unsigned( W )
> {}
>
> sc_biguint( const sc_biguint<W>& v ) // 2nd
> : sc_unsigned( W )
> { *this = v; }
>
> sc_biguint( const sc_unsigned& v ) // 3 third
> : sc_unsigned( W )
> { *this = v; }
>

Cheers & hth.,

- Alf

fl

unread,
May 12, 2017, 10:07:59 AM5/12/17
to
Thank you for your help. When I run below code, I find one calls assignment
while the other calls copy constructor. It is obvious it is caused by the
template parameter <W>. It is still a surprise to me. I thought both should
call copy constructor. Can you explain it to me?

Thanks,

//////////////////
const sc_biguint<4> atomwrj1(2);
sc_biguint<3> atomwrj0=atomwrj1; // Thus calls assignment constructor
sc_biguint<4> atomwrj2=atomwrj1; // This calls copy constructor

// copy constructor
sc_biguint( const sc_biguint<W>& v )
: sc_unsigned( W )
{ *this = v; }

// assignment constructor

Alf P. Steinbach

unread,
May 12, 2017, 8:43:19 PM5/12/17
to
On 12-May-17 4:07 PM, fl wrote:

> [snip] When I run below code, I find one calls assignment
> while the other calls copy constructor.

You mean, one declaration uses the converting constructor while the
other uses the copy constructor.


> It is obvious it is caused by the
> template parameter <W>. It is still a surprise to me. I thought both should
> call copy constructor. Can you explain it to me?

`sc_biguint<3>` is a different type than `sc_biguint<4>`. So an instance
of the first can't be used directly where a reference to the second is
required, or vice versa. This rules out use of the copy constructor in
one case: it can't be called with an argument that can't be converted to
the type of the formal argument.

However, these types share a common base class.


> Thanks,
>
> //////////////////
> const sc_biguint<4> atomwrj1(2);
> sc_biguint<3> atomwrj0=atomwrj1; // Thus calls assignment constructor
> sc_biguint<4> atomwrj2=atomwrj1; // This calls copy constructor
>
> // copy constructor
> sc_biguint( const sc_biguint<W>& v )
> : sc_unsigned( W )
> { *this = v; }
>
> // assignment constructor
> sc_biguint( const sc_unsigned& v )
> : sc_unsigned( W )
> { *this = v; }
>

0 new messages