0

How come "new test()" allocates memory for an object of class test?

Instructions: Fill in the blanks to declare a class "test" with a "foo()" public member function. Declare a pointer "myPtr" to "test" and call "foo()" via the pointer. ----------------------------------- Code (with blanks filled in): class test{ public: void foo() {} }; test* myPtr = new test(); myPtr -> foo(); ---------------------------------- I'm not following the last two lines of code. Why test*? What is new test() and what does it do? Isn't "test" a class; how can it have parameters? Also, the arrow selection (->) and type of pointer (test*) strongly suggest that the part "new test()" is an object of class test, but I'm not understanding this. Please explain. I get that we're assigning memory in the heap and calling it test(), but what precisely is test()? And why do we use "test*" for the pointer initialization, and how can we use "->" to call the foo() function of the pointer? Thank you.

16th Jul 2020, 2:29 PM
Aarav Dodhia
Aarav Dodhia - avatar
4 Respostas
+ 1
test* myPtr = new test(); myPtr->foo(); new test(): 'new' keyword creates an instance of a class 'test' in the heap memory. test* myPtr: You will need a pointer to that instance in the heap to access it's methods and properties. 'test*' declares a pointer 'myPtr' of type test. myPtr->foo(): Here saying 'myPtr->' is the same as saying (*myPtr).foo(). It is called the arrow operator.
16th Jul 2020, 2:36 PM
Saphal Poudyal
Saphal Poudyal - avatar
+ 1
1. No the pointer is not the object itself. Actually , the object created in the heap is nameless. 2. No , the parentheses is not necessary. 3. As I said before the object created with new keyword is nameless.
17th Jul 2020, 3:51 AM
Saphal Poudyal
Saphal Poudyal - avatar
0
Thanks for your help! So when we say: test myTest; What is the compiler really thinking? Is it defining a pointer to myTest and replacing every instance of myTest in our program, with a dereference of its pointer? So for example, say it creates test *ptr = &myTest; Then it would be replacing every time we use myTest with (*ptr), or myTest.(something) with ptr->something? Is that what the compiler implicitly does? I'm not following when you say, "Object/instance of class test" is pretty much equivalent to "pointer to class test". The pointer is not itself an object, right? Does the pointer contain the [i]address[/i] of an object? But in terms of memory allocation, let's say the entire class test takes up some large amount of memory. Then if we're creating a pointer to this, we're creating a pointer to that large amount of memory, right? Where are we creating an object or specific instance of the class, as opposed to a pointer to the entire class itself? I'm also struggling with the syntax: new test(); Why the parentheses? test is a class, not a function, right? How can we write test and then an empty parameter list -- what are we even passing parameters to in the first place?! Also, where are we defining the name of the object, or even a quantitative measure of what the object really is? Earlier, we had "myTest", but now what is the object/instance? Is the object that "test()"? I'm not able to get a good handle on what the object is, what kind of memory it's located in, and what the pointer myPtr is really pointing to. Please help. Thank you so much!
17th Jul 2020, 3:20 AM
Aarav Dodhia
Aarav Dodhia - avatar
0
Thank you, that really helps. I see now that objects are really things that *point* to the class methods and attributes. The same applies with a regular pointer to a class - hence that is what we define when we say test *myPtr = new test(); Now I understand what you mean by, "both the object and the pointer will store the same function pointer" -- since they both are essentially the same thing. They both retrieve functions or properties from the class; hence they both point to the class methods. Is my understanding correct? The last difficulty is the syntax "new test();". I read your example (first of all, good job and thank you!). Everything makes sense, except the second part where you say, "Test *myptr = new Test(101);". There's a similar confusion I am facing here. I think I need to step back to understand what the "new" keyword does here. So say we define "new int;". That creates [i]space[/i] for an integer in the heap. Similarly, "new (datatype)" creates space for that datatype in the heap, right? In a similar regard, "new test" would create space for something of type test in the heap. But that "something of type test" is, by definition, an object of class test?? Is that why the space that we're allocating is actually the space for an [i]object[/i] of test? Regarding the constructor, we usually pass constructors like this: test myTest(...arguments...); OR test myTest; myTest(...arguments...); So we pass constructors as parentheses at the end of the object, right? That's because it's the [i]object's[/i] arguments/methods/attributes that are being referred to? But in the original example, we're saying "test()" when we pass constructors; however, "test" is not an object! That's where I am confused -- are we using the constructor on "new test" (which is an object of class test), or are we using it directly on "test" (which is a class itself)? Please answer the above. Thanks a lot - I really appreciate your help!
17th Jul 2020, 12:05 PM
Aarav Dodhia
Aarav Dodhia - avatar