?
Solved

Conversions in ref classes // c++ 2005

Posted on 2006-07-20
7
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified 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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

770 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