Solved

Conversions in ref classes // c++ 2005

Posted on 2006-07-20
7
273 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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