Solved

Conversions in ref classes // c++ 2005

Posted on 2006-07-20
7
248 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
ID: 17145746
Replace
TEST = sysStr;
with:
*TEST = sysStr;
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17145768
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
ID: 17145852
but String^ is a also a reference. It should be possible to assign reference to reference shouldn't it
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 48

Expert Comment

by:AlexFM
ID: 17145913
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
ID: 17145928
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
ID: 17146056
One more question:
What is then System::String^ when we can assign:
System::string^ abc;
abc = "abc";
0
 
LVL 48

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

11 Experts available now in Live!

Get 1:1 Help Now