Why doesn't C ++ new return a pointer to the object it creates?

I figured out the following code, according to my knowledge, when I declare an object using new, it constructs an object of a specific type and returns a pointer to that object. But here, when I create a student object using the new one, it doesn't return a pointer to that object. Also, when I call the new student (s1) " student (student * s) " instead of giving an error like a student to student type conversion *

#include <bits/stdc++.h>

using namespace std;

class student {
    public:
        int cool;

        student(student* s){ // this constructor is taking pointer to s whereas i am calling it by value below
            this->cool=s->cool;
        }
        student(){
            this->cool=1;
        }
};
void blah(student s){
    printf("cool:%d\n",s.cool);
}

int main(){

    student s1=new student(); // here new is not returning student*
    s1.cool=2;
    student s2=new student(s1); // here we are passing s1 which of type student but constructor (student*) is called why ???
    blah(s2);
}

      

After the output, I get no error:

cool: 2

+3


source to share


3 answers


You are leaking memory.

This constructor:

student(student* s)

      

Used for consistency of transformations as it is unlabeled explicit

. So what's going on here:

student s1=new student();

      

It's a heap - assign a new object student

. The expression new

is evaluated by a pointer. When the compiler looks at the assignment, it knows that the assignment won't work because it s1

is student

, and you assign to it student *

. So he looks for a way to convert it and finds the constructor I mentioned above.



What you are doing is equivalent to:

student s1 = student(new student());

      

Since you never specified a delete

pointer, the heap allocation was leaked, but otherwise your program will run correctly from your point of view.

If you mark the conversion constructor like this:

explicit student(student* s)

      

Then the compiler will not automatically use this constructor for conversions, requiring an explicit call of some kind, and the line student s1=new student();

will indeed cause a compile-time error allowing it student s1 = student(new student());

to work (but of course it will still cause a memory leak).

+7


source


new

returns a pointer to student

. Of course it is!

What happens in the first case is that the string student s1 = new student()

calls the constructor student(student*)

to construct s1

.

In the second case, the string student s2 = new student(s1)

first calls the default copy constructor and then calls the constructor student(student*)

to build s2

.



In both cases, a memory leak will occur because you are not calling delete

to match your new

s.

This behavior explains why it is very important to use explicit

single parameter before constructors, unless you really want this kind of implicit conversion (and even if you think it is, you probably won't).

+2


source


I don't know where you got the weird idea from that new

doesn't return a pointer. The code you submitted does not in any way endorse this claim. This initialization

student s1 = new student();

      

creates a default built object student

with new

. The pointer returned new

is then passed to the constructor student::student(student *)

as it should.

Second initialization

student s2 = new student(s1);

      

creates an object student

built to copy, c new

. The pointer returned new

is then passed to the constructor student::student(student *)

, as it should.

Why you expected the "no type conversion from student

to student *

" error in the second case is not clear. Your class student

has an implicit copy constructor declared and defined by the compiler. This is the constructor used in initialization student(s1)

. There is no need to convert student

to student *

and there is no error.

In both cases, it student::student(student *)

is called with a pointer returned new

. It seems that for some reason you think that in the first case student::student(student *)

it is not called. But this is a completely unfounded belief. It is called.

+1


source







All Articles