Why do you need assignments?

I came across some code that boils down to the following:

enum BAR { /* enum values omitted */ } class Foo{ public: void set(const BAR& bar); private: uint32_t bits; }; void Foo::set(const BAR& bar) { (uint32_t&)bits = bits | bar; } 

I do not understand the meaning of c-style casting in a job in Foo :: set. Why do you need assignments? Am I crazy or does this have a purpose?

+6
c ++
source share
6 answers

In this case, I see no reason for the cast, as the thing you do is of the same type as the cast. In general, it can be used to force the use of a specific assignment operator.

Now I repeat my mantra: if your code contains throws, there may be something wrong with the code or design, and you should study both options to remove the throw.

+8
source share

I agree with Neil Butterworth, casting is not necessary in this case, and this is a definite "code smell."

+2
source share

It does nothing, as far as I can tell, even if the BAR is determined by values ​​outside the uint32 range. Sounds like a noise to me.

0
source share

I can’t say for sure, without knowing the background, but it looks like someone might have taken some existing C code and wrapped it in a C ++ class. The cast can be left from code c.

0
source share

I am not sure if this is legal. cast is not an lvalue ...

In any case, this seems pretty pointless.

0
source share

If you only want to assign a specific value (for example, the assignment check is performed as if it were changed to fit the cast, instead of just skipping something as if you had expanded rhs).

-one
source share

All Articles