BCB6有这样一个Bug
一个组件的属性名称为Port,并且其设置方法名称为SetPort时,编译没有问题,但是如果使用这个组件时,对这个属性的引用,编译器会将SetPort方法翻译为SetPortA,从而导致连接器不能正常连接,尽管可以通过在该组件的头文件中加入
#define SetPortA SetPort
来让程序可以正常的编译连接,但总觉得不爽!!
没有以上define语句会有如下错误
[Linker Error] Unresolved external '__fastcall TComBug::SetPortA(unsigned short)' referenced from H:\PROGRAM FILES\BORLAND\CBUILDER6\PROJECTS\BUGTEST\UNIT1.OBJ
整个测试代码如下,可以将ComBug.h和ComBug.cpp加入任意一个BPK工程中,创建一个空的Application工程加入TComBug,下列代码会产生上述连接错误
ComBug1->Port = 0;
代码如下:
Code In ComBug.h
////////////////
#ifndef ComBugH
#define ComBugH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
//---------------------------------------------------------------------------
class PACKAGE TComBug : public TComponent
{
private:
unsigned short FPort;
void __fastcall SetPort(unsigned short value);
protected:
public:
__fastcall TComBug(TComponent* Owner);
__published:
__property unsigned short Port = { read=FPort, write=SetPort };
};
//---------------------------------------------------------------------------
#endif
Code in ComBug.cpp
/////////////
//---------------------------------------------------------------------------
#include <basepch.h>
#pragma hdrstop
#include "ComBug.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TComBug *)
{
new TComBug(NULL);
}
//---------------------------------------------------------------------------
__fastcall TComBug::TComBug(TComponent* Owner)
: TComponent(Owner)
{
}
//---------------------------------------------------------------------------
namespace Combug
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TComBug)};
RegisterComponents("MyInterFace", classes, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TComBug::SetPort(unsigned short value)
{
if(FPort != value)
{
FPort = value;
}
}
Code in Application that Use this Component
////////////////
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "ComBug"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ComBug1->Port = 10;
}
//---------------------------------------------------------------------------