MtRock.cs Paradise 16

Qingshuan’s Programming Blog

Archive for January, 2008

[C++/.NET] When to use a reference

Posted by linuxprogram on January 28, 2008

Q:
When should I return a reference from a function?

A: Basically there are two situations:

  • The information being returned is a large enough object that returning a reference is more efficient than returning a copy. Just as it can be more efficient to pass large objects to functions by reference, it also can be more efficient to return large objects from functions by reference. Reference-return protocol eliminates the necessity of copying the object to a temporary location prior to returning.
  • The type of the function must be an l-value. Reference-return types can also be useful when the function must evaluate to an l-value. Most overloaded operators fall into this category, particularly the assignment operator.

Quoted from: http://www.codeguru.com/forum/showthread.php?t=316925

Posted in C/C++/C# | Leave a Comment »

[C++/.NET] Convert TCHAR* to System::String ^ AND Convert System::String ^ to TCHAR*

Posted by linuxprogram on January 25, 2008

In order to convert TCHAR* to System::String, use this:  

 System::String ^ tchar2StringRef( TCHAR * p){

    System::String ^ str;

    str = gcnew System::String(p);

    return str;

}

 

Or use a macro:

     #define TCHARPTR_TO_STRINGREF(ch)    gcnew System::String(p)

 In order to convert System::String ^ to TCHAR *, use this:

TCHAR * StringRef2Tchar(System::String ^ theString){

    pin_ptr<const TCHAR> str = PtrToStringChars(theString);

return str;

}

 

Sometimes, the above just won’t work (And I at this moment don’t know why), then we can use this:

 

wchar_t strUni[] = L”This is an Unicode string”;

String^ str = gcnew String (strUni);

Intptr p = Marshal::StringToHGlobalUni(str);

wchar_t* pUni = static_cast(whar_t*>(p.ToPointer());

wprintf(L”%s\r\n”, pUni);

Marshal::FreeHGlobal(p);

Posted in C/C++/C# | Leave a Comment »

[C++/.NET] unresolved token / unresolved external symbol

Posted by linuxprogram on January 25, 2008

Error    1    error LNK2028: unresolved token (0A000028) “public: int __thiscall UMess::Point::getX(void)” (?getX@Point@UMess@@$$FQAEHXZ) referenced in function “public: void __thiscall UClass::RefParamMethod(class UMess::Point)” (?RefParamMethod@UClass@@$$FQAEXVPoint@UMess@@@Z)    UClasses.obj    

Error    2    error LNK2019: unresolved external symbol “public: int __thiscall UMess::Point::getX(void)” (?getX@Point@UMess@@$$FQAEHXZ) referenced in function “public: void __thiscall UClass::RefParamMethod(class UMess::Point)” (?RefParamMethod@UClass@@$$FQAEXVPoint@UMess@@@Z)    UClasses.obj    

Error    3    fatal error LNK1120: 2 unresolved externals    C:\My Documents\Visual Studio 2005\Projects\TestCWrapper\Debug\TestCWrapper.dll    1

 

You might think, what the fak are all these!!!???

Yes, these are the payback for my carelessness. When you encounter some errors like:unresolved token”, “unresolved external symbol”, don’t panic. Calm down and check you code very carefully. If you know your code very well or you code doesn’t contain many lines, you are very likely to find out that you used something declared but NOT implemented in your code, and there was nowhere else for the compiler to find that “SOMeThINg”.

Well, to solve it, GO AND IMPLEMENT YOUR SYMBOL!! Come On! Why are you still here staring the blog?!! GO! GO! GO!!

Posted in C/C++/C# | 5 Comments »

[C++/.NET] How to wrap a returned reference

Posted by linuxprogram on January 25, 2008

For a function like:

    class A {};

    class B{

        virtual A & Method () = 0;

    }

To Wrap this Native Code, I did the following:

    public ref class A abstract{};

    public ref class B abstract{

        virtual A & Method () = 0; //    error C3699: ‘&’ : cannot use this indirection on type ‘A’

    }

But now the code block generates Error 3699. What should I do in order to wrap a function that returns a reference?

(yet to be solved)

Posted in C/C++/C# | Leave a Comment »

[C++/.NET] In ref class abstract keyword goes after

Posted by linuxprogram on January 24, 2008

To explicitly declare a ref class abstract, use the keyword “abstract” after the class name:

public ref class AbstractRefClass abstract {

void method() = 0;

}

But for a old syntax class using no “ref”, “abstract” goes before “class”:

abstract class OldClass{

void method() = 0;

}

Posted in C/C++/C# | Leave a Comment »

[C++] Header files gotcha

Posted by linuxprogram on January 23, 2008

When including header files, keep in mind that literally the header file content will placed at exactly the point where we include them.

So if the header file lacks a “;” at the end, a compiling error might be caused, just because the “;” is needed in including files (.cpp).

Posted in C/C++/C# | Leave a Comment »