In C++, when you create a new class object using the 'new' keyword and directly assign it to a object pointer, is the object not associated with any direct name?
-
For eg, when we say 'obj *b = new obj()' is the pointer b the only way to refer to the new object ?
-
Answer:
Yes, it has no name. But neither does anything else, except for the lexical constructs. When you declare something otherwise, such as obj b(); it just associates a lexical name with some storage with the object in it, whereas new makes a pointer to the object. You can do this, however: obj b(); obj *b1 = &b; so now you have a location and a pointer. The location could be in a lot of places. It could be a global variable and be in global storage. It could be an automatic variable and be a location on the stack. Or it could be a static local variable, in which case it would reside in the global space but the lexical binding would make it look as if it were in the function. How names get associated with data in a program really gets quite complex. It's not really useful to say that the object has a name. It's more useful to say that the name is bound to a storage location where the object or its pointer are stored. I think that every student who starts making interpreters or compilers goes through a similar process of confusion, and you can see traces of this confusion in the history of programming languages. The "right" way to do this isn't obvious, and it took decades to work it out. What happens now is that languages go one way or the other. In some languages, such as C++, names are purely lexical. In C++, in particular, there are oodles of ways to name what you want. That way, you can know everything at compile time, which is one school. Other languages, such as Objective C and Java, go the other way. There's lexical stuff (more in Java than Objective C), but at run time, there are actually searches for names. This has worse performance, and it can produce subtle bugs, but there is a high degree of magic in reflection and introspection that is considered desirable by another school. In any event, it's only with the second approach that one can really talk about named objects in a running program, and C++ isn't one of the languages that does it.
Eric Pepke at Quora Visit the source
Other answers
Like Eric Pepke says, the "name" of something isn't a straightforward concept. An object can have zero or more names. A reference is just another name for the same object. Although this is oversimplified, I like to think of references as hard links, and pointers as soft links. The real story is far more complicated http://en.cppreference.com/w/cpp/language/value_category Aside from runtime values, types also have names.,and namespaces have names. But an object created with "new" cannot have a name, at least as the language understands it, because names are assigned at compile time. This is the reason virtual destructors exist. The compiler does not know how to delete an unnamed object. A pointer to an object may only point to a subtype, which may not even be add the address returned by new. (Say it points to a virtual base class of the object). Virtual dtor calls the correct destructor and "returns" under the hood the address of the object , I.e. the result of dynamic_cast<void*>
Lance Diduck
When you declare an object directly, a name is associated with a value. When you declare a dynamically allocated object, the name is that of the pointer. If you want a dynamically allocated object that has a direct name like a normal stack object, declare it using a reference. obj a(); // This is on the stack obj &b = *(new obj()) // This is on the heap // Treat a and b equally with the same syntax... // You cannot reuse the name b within this scope, just like a // Finally get rid of b delete (&b)
Vivek Nagarajan
Related Q & A:
- How to dynamically create a PHP class name on the fly?Best solution by stackoverflow.com
- how to create a new syntax in java?Best solution by Stack Overflow
- How to create a new blog?Best solution by Yahoo! Answers
- How do I create a new Yahoo screen name if I already have one?Best solution by Yahoo! Answers
- How do I create a new Yahoo address name?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.