Confused over specs and what I'm seeing. 
Author Message
 Confused over specs and what I'm seeing.

I'm reading the Visual C++ specs mcspec.doc for managed c++ extensions
and I came across the following statement:

"all instances of identical S string literals always point to the same
object, which is not true for String* objects that are constructed
from C++ string literals." on page 64 line 7.

I thought that was interesting and took it to mean that any time there
was a string literal with S preceding it that has the same string
literal value, it always refers to the same object. So, I figured
(probably naively), that if I did something like this:

using namespace System;

__gc class test
{
public:
        test(){}
        System::String* returnString()
        {
                return S"Hello World";
        }
private:

Quote:
};

// This is the entry point for this application
int _tmain(void)
{
        test *s = new test();
        System::String *blah = s->returnString();
        System::String *blah2 = s->returnString();
        blah2 = "Hello World2";
        System::Console::WriteLine(blah);
        System::Console::WriteLine(blah2);
    return 0;

Quote:
}

I would change both blah and blah2 since, at least as far as my
understanding was concerned, blah and blah2 should, then, point to the
same string object since my String* object was created from a non-C++
string literal.

But, the output is

Hello World
Hello World2

So that I don't miss an important concept here, can someone fill me in
on where my thinking is bad?

Thanks,
Ben



Mon, 27 Jun 2005 01:16:15 GMT  
 Confused over specs and what I'm seeing.

Quote:

> System::String *blah = s->returnString();
> System::String *blah2 = s->returnString();
> blah2 = "Hello World2";

> [SNIP]

> So that I don't miss an important concept here, can someone fill me in
> on where my thinking is bad?

You're not actually changing the value of the string (impossible to do),
you're changing the value the pointer.  Before, you had something like this:

   blah ---\
            \--->  S"Hello World"
            /--->
   blah2---/

Afterwards, you had this:

   blah ----> S"Hello World"
   blah2----> "Hello World2"

Remember that in pictures like this, the value of a pointer is represented
by an arrow.

Hope that helps!

--
Brandon Bray                                          Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.



Mon, 27 Jun 2005 02:04:15 GMT  
 Confused over specs and what I'm seeing.
Ah, I had a mental breakthrough (breakdown?). The strings values cannot be
changed, and like all non-value objects in the .Net world everything is on
the managed heap, and thus has a pointer to it is used, not the value, and
so on and so forth. So when I'm changing the "value" like that, I'm actually
changing what object I'm pointing to.

See, I was thinking that since I returned a string literal I was returning
the same object both times. Which,

if(blah==blah2)
    System::Console::WriteLine("Equal");

Shows is true. However, when I assign a new string, I create a new object,
and the variable no longer references the original object, but the new
one.You're drawing makes more sense this way:

What was:

[blah] & [blah2] -------->[S"Hello World";]

Now is:

[blah]---------------->[S"Hello World";]
[blah2]--------------->[L"Hello World2"; ]

Where everything in the []'s is an object, and the arrow is the pointer and
so forth. Ah. See, I think I new this but I just didn't make the connection
for some reason.

Thanks Brandon,
Ben



Quote:

> > System::String *blah = s->returnString();
> > System::String *blah2 = s->returnString();
> > blah2 = "Hello World2";

> > [SNIP]

> > So that I don't miss an important concept here, can someone fill me in
> > on where my thinking is bad?

> You're not actually changing the value of the string (impossible to do),
> you're changing the value the pointer.  Before, you had something like
this:

>    blah ---\
>             \--->  S"Hello World"
>             /--->
>    blah2---/

> Afterwards, you had this:

>    blah ----> S"Hello World"
>    blah2----> "Hello World2"

> Remember that in pictures like this, the value of a pointer is represented
> by an arrow.

> Hope that helps!

> --
> Brandon Bray                                          Visual C++ Compiler
> This posting is provided AS IS with no warranties, and confers no rights.



Mon, 27 Jun 2005 13:29:52 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Printf's format Specs

2. Confused between 0 and '\0'

3. Protype form I hav'nt seen before

4. Who's seen the standard?

5. making #define's to be seen across files

6. why my Control methods can't be seen by outside

7. Seeing pre=processor's work

8. The most frustrating windows bug i've seen

9. Start a command without seeing the console and get the exe's return code

10. Connection Manager sees GPS but can't find on any of the COM ports

11. pointer to struct I'm confused

 

 
Powered by phpBB® Forum Software