Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I assign struct with a pointer?

Why does it even compile?

struct UE{
    UE(bool a = true) {  };
//  UE(){}; // if UE took no initial args and called below, gcc will complain.
};
class VA {
    protected:
            UE ue;
    public:
            VA();
};
VA::VA()
{
    ue = new UE(true); // ???why???
//  ue = new UE(); // will complain
}

I tried with gcc (GCC) 4.6.2. How can I assign a struct with a pointer?

like image 980
BetterWang Avatar asked Feb 11 '14 18:02

BetterWang


People also ask

How do you assign a pointer to a struct?

To allocate the memory for n number of struct person , we used, ptr = (struct person*) malloc(n * sizeof(struct person)); Then, we used the ptr pointer to access elements of person .

Can we have a pointer to a struct?

As we know Pointer is a variable that stores the address of another variable of data types like int or float. Similarly, we can have a Pointer to Structures, In which the pointer variable point to the address of the user-defined data types i.e. Structures.


3 Answers

You're hitting the fact that you didn't mark your constructor as explicit, and thus it can be used for implicit conversions.

new UE(true) returns a pointer. All pointers can be implicitly converted to bool, resulting in true if they are non-null. UE can be implicitly constructed from a bool. So in effect, the pointer returned by new is converted to bool, which is converted to UE using your constructor, and then the copy assignment operator of UE is called. Of course, the memory allocated by new is leaked.

The take-home message is: always mark your single-argument constructors as explicit unless you actually want them to be usable for implicit conversions. By "single-argument constructor," I mean one which can be called with a single argument. Either because it has one parameter, or it has more and all parameters after the first have default arguments.

like image 85
Angew is no longer proud of SO Avatar answered Sep 25 '22 16:09

Angew is no longer proud of SO


There is an implicit conversion from any pointer to bool. Hence this code is implicitly calling the single argument constructor after converting the pointer to a bool value

One way to fix this is to make the single parameter constructor explicit.

struct UE{     explicit UE(bool a = true) {  }; }; 

This will prevent the code from compiling unless the constructor is explicitly invoked

like image 44
JaredPar Avatar answered Sep 26 '22 16:09

JaredPar


If you mark you constructor explicit this won't work:

explicit UE( bool a = true) {  };

this is because there is an implicit conversion from the pointer to a bool, we can see this is allowed from draft C++ standard section 4.12 Boolean conversions says (emphasis mine):

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool.

like image 25
Shafik Yaghmour Avatar answered Sep 24 '22 16:09

Shafik Yaghmour