Solved

Conversions in ref classes // c++ 2005

Posted on 2006-07-20
7
229 Views
Last Modified: 2010-04-24
What shall be done to compile following code and to apply following conversion:

ref class ttt
{
private:
      System::String^ MyString;
public:
      ttt(){}
      ttt(System::String^ InVal)
      {
            MyString = InVal;
      }
      void operator = (System::String^ InVal)
      {
            MyString = InVal;
      }      
};

int main(array<System::String ^> ^args)
{      
      System::String^ sysStr;
      sysStr = "system strring";
      
      ttt^ TEST = gcnew ttt();
      TEST = sysStr;                             // Not compiling - conversion needed
      
    return 0;
}
0
Comment
Question by:PLABB
  • 5
  • 2
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
Replace
TEST = sysStr;
with:
*TEST = sysStr;
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
TEST = sysStr;
This is attempt to assign String^ to ttt reference.

*TEST = sysStr;
In this case String^ is assigned to ttt instance, this is compiled because class ttt has operator= (String^).
0
 

Author Comment

by:PLABB
Comment Utility
but String^ is a also a reference. It should be possible to assign reference to reference shouldn't it
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
Assign reference to reference is another task. For example:

String^ s1 = gcnew String(L"1");
String^ s2 = gcnew String(L"2");

s2 = s1;

Now s2 points to s1, and previous instance of s2 is out of scope. In your case:

TEST = sysStr;

You try to assign string reference to ttt reference. These two types are not compatible, and this is not compiled: reference to string cannot point to ttt. However, when you write:

*TEST = sysStr;

reference to string is assigned to class instance. Since class has operator=(String^), this is compiled. Class knows what to do with String^ when it is assigned to class instance:
MyString = InVal;

Class assigns this string instance to it's own String^ variable. Notice that String^ is assigned to String^ - legal operation, and to to ttt^.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
Comment Utility
If you know C++, consider that ttt is unmanaged class which has operator=(char*), and TEST is ttt*. In this case:
TEST = "abc";
is not compiled, and
*TEST = "abc";
is compiled - exactly the same case.
0
 

Author Comment

by:PLABB
Comment Utility
One more question:
What is then System::String^ when we can assign:
System::string^ abc;
abc = "abc";
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
Good question. I will return to this later, now I need to go.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now