Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: template question



Permlink Replies: 2 - Last Post: Sep 22, 2014 2:18 PM Last Post By: chen zero
chen zero

Posts: 30
Registered: 5/26/00
template question
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 22, 2014 3:04 AM
Hello,
C++Builder XE, following code can not compile when class A1 is inherited from TObject,
however, when class A1 is not inherited from TObject, it can compile.


template <class T>
class A1 : public TObject {
protected:
	T* t;
public:
	A1() {
	}
	virtual __fastcall ~A1() {
	}
	virtual void f() {
		ShowMessage("A1.f()\n");
	}
};
 
template <class T>
class B1:public A1<T>
{
public:
	B1():A1<T>() {
	}
 
	virtual __fastcall ~B1() {
	}
 
	virtual void f() {
		ShowMessage("B1.f()\n");
	}
};
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	A1<int>* a1 = new A1<int>();
	a1->f();
	delete a1;
 
	B1<int>* b1 = new B1<int>();
	b1->f();
	delete b1;
}


the error msg is:
[BCC32 Error] Unit1.cpp(481): E2080 '_fastcall ~B1<int>::B1()' must be declared with one parameter

I just curious the position where the error msg occurred ---- B1::~B1()
if it's not allowed to create template inherited from TObject, why the error msg is not at A1::~A1()

Thanks!

Edited by: chen zero on Sep 22, 2014 3:17 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: template question
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 22, 2014 10:00 AM   in response to: chen zero in response to: chen zero
chen zero wrote:
C++Builder XE, following code can not compile when class A1 is inherited from
TObject, however, when class A1 is not inherited from TObject, it can compile.

Due to differences between C++ templates and Delphi Generics, the C++ compiler does not support creating a C++ template class that derives from a Delphi class. It only supports consuming a template class that is implemented in Delphi (is marked with __declspec(pascalimplementation)). See the documentation for some more details:

http://docwiki.embarcadero.com/RADStudio/XE/en/How_to_Handle_Delphi_Generics_in_C%2B%2B

--
Remy Lebeau (TeamB)
chen zero

Posts: 30
Registered: 5/26/00
Re: template question
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 22, 2014 2:18 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks, Remy,
the doc is very useful,
and I change the code to compound TObject as member of class A1.
good enough :)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02