Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Conversions in ref classes // c++ 2005

Posted on 2006-07-20
7
Medium Priority
?
281 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
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

810 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