why dyna. allocation is allowed to pointers only?

Hello friends,

I have one single doubt today, I want to know why the new and delete expressions could only be used when the left hand side is a pointer ?
like this:

string *s = new string;

on the other hand, this isn't allowed:

string s = new string;

This is a part of the fundamentals of the language, I know, but I still need to have it clarified in the best way possible.

Thanks in advance,

Jose.
Last edited on
Because new string returns a pointer to a new string object.
When you write string s;, s will be constructed automatically.
Because a normal instance will get destroyed when out of scope. Pointers don't get "destroyed", only their identifier will be destroyed when they go out of scope (the way you refer to them, like, in your first example, s is the identifier) - the actual instance stays intact. That's the reason pointers are chosen; they refer not to an instance, but to the memory address of an instance.
so new string will return a pointer to a string, or in other words a pointer to an address that will eventually contain an string object ?
or in other words a pointer to an address that will eventually contain an string object ?


Yes. Although it's not "eventually". That address does contain a new string object when new returns.
and would that new string created by new initially be an empty string in this case?
Yes, unless you use a different constructor, i.e. new string("foo");
new calls the constructor of whatever it allocates

For string, the default constructor creates an empty string.

So yes.
Yes, it would be empty in the sense that it does not contain any useable data.
This is great,

One more thing, Am I to understand that the constructor for a type like this, string in this case returns string*. Is there a way to see this implementation ? (although I tend to believe not, because constructors have no return types, right?)
The constructor doesn't return anything. new returns the pointer and it's also new that allocates memory and calls the constructor.
Al right, this has been really instructive !

Thank you very much.

Consider this thread closed.
Yes, it would be empty in the sense that it does not contain any useable data.


I might be splitting hairs here, but this statement is somewhat misleading.

The whole point of a constructor is that it makes the object immediately usable.

It's just that the default constructor makes the string empty by default. By "empty" I mean it contains a string that is 0 characters in length.
I am doing an exercise on this very topic and have a follow-up question. I hope I get some replies despite the thread being flagged as solved.

So, using the example of the OP, string *s = new string yields a pointer called s to the address containing a string initialized to a default value. Whereas, string s = string yields a string called s initialized to the value 'string'.

My questions are, in the first case, does the object pointed to by s have a name? Can I refer to that object without using the pointer?
My questions are, in the first case, does the object pointed to by s have a name? Can I refer to that object without using the pointer?

No.

When using the term "name" loosely, you could say that its address is its name which you can use to refer to it.
But if you forget that name (=its address=the pointer), you no longer can, of course.
Topic archived. No new replies allowed.