📄 00000000.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: Zeemon (塞下秋), 信区: Visual <BR>标 题: C++ Builder常见问题解答(3) <BR>发信站: BBS 水木清华站 (Wed Dec 3 19:58:47 1997) <BR> <BR>第三部分 C/C++ Language Issues <BR> <BR>1、Sizeof template parameter <BR> <BR>Question: <BR>BC++ 5.02 and C++Builder do not allow sizeof() to take <BR>a template parameter. How do I get around this? <BR> <BR>Answer: <BR>The behavior is a result of a bug in BC++ 5.02 and C++Builder. <BR>Borland is aware of the bug. In the interim, the following hack <BR>will get around the problem: <BR> <BR>template <class T> class foo { <BR> struct hack {T t; }; <BR> enum _sz {SIZE = sizeof(t) }; <BR>} <BR> <BR>2、Calling Pascal from C++Builder <BR> <BR>Question: <BR>How do I call Pascal routines and variables from C++ modules? <BR> <BR>Answer: <BR> An easy way to find out is to let C++Builder do it for you. <BR>Put the Object Pascal declarations you need to translate in the <BR>interface section of an Object {ascal unit. Now use the command <BR>line compiler DCC32.EXE with the options j, p, h, n, and v. <BR>Below is an example of called DCC32 to translate a file called <BR>MyUnit.pas. <BR> <BR> DCC32 -jphnv MyUnit.pas <BR> <BR>After the compliler finished you will have a header file call <BR>MyUnit.hpp, open it in an editor and will see the C++ <BR>declarations. <BR> <BR>3、Converting AnsiString to numeric variables <BR> <BR>Question: <BR> How do you convert AnsiStrings to and from numeric values in <BR>C++Builder? <BR> <BR>Answer: <BR> Use the built-in functions StrToInst() and StrToFloat convert <BR>to numeric values and IntToStr() and StrToFloat() for the <BR>reverse process. <BR> <BR> int intValue = StrToInt(Edit1->Text); //String to Integer double <BR> double doubleValue = StrToFloat(Edit1->Text);//String to Double <BR> <BR> Edit1->Text = IntToStr(IntValue); // Integer to String <BR> Edit1->Text = FloatToStr(doubleValue); // Double to String <BR> <BR>4、C++ and Pascal <BR> <BR>Question: <BR>I am C++ programmer and not familiar with pascal. Can you <BR>give me a few pointers that will help? <BR> <BR>Answer: <BR>Here are some pointers: <BR> - Function ( procedure ) calls don't require parenthesis. <BR> - When you see '.' in pascal, you probably need the '->' <BR> in C/C++. <BR> Form2.Show; (* Opascal *) <BR> Form2->Show(); // C++ <BR> - Don't forget assignment operator is a colon equals <BR> combination ':='. <BR> - Create and CreateNew are Object Pascal's constructors. <BR> - The inherited statement is used to tell the compiler <BR> not to call the function named in the current class, <BR> but to use the one in the next class higher in the <BR> hierarchy. If not, there continue searching up the <BR> hierarchy. This is the same as the super keyword in <BR> Java or Smalltalk. <BR> - Bar = class(Foo) in Opascal says a variable of <BR> type class Bar inheirits compatibliliy with class Foo. <BR> <BR> type <BR> Shape = class <BR> . . . <BR> end; <BR> <BR> Rectangle = class(Shape) <BR> . . . <BR> end; <BR> <BR> <BR> Square = class(Rectangle) <BR> . . . <BR> end; <BR> <BR> Circle = class(Shape) <BR> . . . <BR> end; <BR> <BR> In example above a value of Rectangle can be assigned to <BR>variables of type Rectangle, Shape, or TObject ( base of all <BR>VCL classes.) At runtime a variable of type Shape can reference <BR>an instance of Shape, Rectangle, Square, or Circle or any other <BR>instance or a class derived from Shape. <BR> <BR>5、Converting a String to Char* <BR> <BR>Question: <BR> I am trying to convert a String to char* but keep receiving <BR>the error: <BR> "Cannot cast from System::AnsiString to Char*". <BR> <BR>Below is my code. What am I doing wrong? <BR> String s; <BR> char* ch; <BR> s = "test"; <BR> ch = PChar(s); <BR> <BR>Answer: <BR>What you need to do is use the c_str() member function of <BR>AnsiString. Here are a couple examples: <BR> <BR> String s; <BR> const char* ch; <BR> s = "test"; <BR> ch = PChar(s.c_str()); <BR> <BR>You can avoid the PChar completely if you like. <BR> <BR> String s; <BR> const char* ch; <BR> s = "test"; <BR> ch = s.c_str(); <BR> <BR>6、How do I obtain a char * from an AnsiString? <BR> <BR>Question: <BR>I've got <BR> <BR>AnsiString foo; <BR> <BR>How do I pass this to a function which needs a char? <BR>(eg., strcpy, etc). <BR> <BR>Answer: <BR>foo->c_str(). <BR> <BR>The c_str() method of the AnsiString class returns <BR>a const char* which can be used to read, but not <BR>modify, the underlying string. <BR> <BR>7、Getting command-line arguments in BCB <BR> <BR>Question: <BR>How can I access the list of parameters passed at the command <BR>line with BCB? <BR> <BR>Answer: <BR>You can use the following code: <BR> <BR>for (int x=0; x <= ParamCount(); x++) <BR> ShowMessage(ParamStr(0)); <BR> <BR> <BR>ParamStr(0) will return the fully qualified name of the <BR>executable file. <BR> <BR>8、Byte alignment in C++Builder <BR> <BR>Question: <BR>How do I get byte alignment in a struct in C++Builder <BR> <BR>Answer: <BR>#pragma pack(1) <BR>struct foo{ /* put struct definition here */ }; <BR>#pragma pack() <BR> <BR>9、Catching exceptions in a thread <BR> <BR>Question: <BR>How can I catch a C++ exception in a thread. I have created a <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -