| View previous topic :: View next topic |
| Author |
Message |
Tales Aguiar Guest
|
Posted: Wed Jun 22, 2005 8:23 pm Post subject: delphi interfaces with inheritance |
|
|
I create an simple interface and one delphi style class implementing this
interface.
A second class override one method of that interface but don't work if I
cast to interface type;
Why?
__interface INTERFACE_UUID("{05F1BBC5-4D65-490B-9A3D-16AFF41036AD}")
MyInterface: public IInterface
{
public:
virtual void Show() = 0;
};
typedef DelphiInterface<MyInterface> _di_MyInterface;
class Teste: public TInterfacedObject, public MyInterface
{
public:
virtual HRESULT __stdcall QueryInterface(const GUID& IID, void **Obj) {
return TInterfacedObject::QueryInterface(IID, (void *)Obj);
}
virtual ULONG __stdcall AddRef() {
return TInterfacedObject::_AddRef();
}
virtual ULONG __stdcall Release(){
return TInterfacedObject::_Release();
}
virtual void Show(){
ShowMessage("A");
}
};
class DELPHICLASS Teste2: public Teste
{
public:
virtual void Show(){ // not override MyInterface::Show ... why?
ShowMessage("B");
}
};
void main ()
{
_di_MyInterface intf = new Teste2;
intf->Show(); // the message box A is displayed
}
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Jun 23, 2005 12:04 am Post subject: Re: delphi interfaces with inheritance |
|
|
"Tales Aguiar" <tqueiroz (AT) engesis (DOT) com> wrote
| Quote: | virtual void Show() = 0;
|
Interface methods should always use the __stdcall convention.
| Quote: | typedef DelphiInterface<MyInterface> _di_MyInterface;
|
Your class is not a delphi interface to begin with, so you should not be
using the DelphiInterface class for it.
| Quote: | class DELPHICLASS Teste2: public Teste
|
Why are you using the DELPHICLASS macro if your class is not actually
implemented in Delphi to begin with? You should not be using the
DELPHICLASS macro unless the class is actually implemented in Delphi Pascal
code, which is not the case in your example.
Gambit
|
|
| Back to top |
|
 |
Tales Aguiar Guest
|
Posted: Thu Jun 23, 2005 2:48 pm Post subject: Re: delphi interfaces with inheritance |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote
| Quote: |
"Tales Aguiar" <tqueiroz (AT) engesis (DOT) com> wrote in message
news:42b9c8c5 (AT) newsgroups (DOT) borland.com...
virtual void Show() = 0;
Interface methods should always use the __stdcall convention.
typedef DelphiInterface<MyInterface> _di_MyInterface;
Your class is not a delphi interface to begin with, so you should not be
using the DelphiInterface class for it.
class DELPHICLASS Teste2: public Teste
Why are you using the DELPHICLASS macro if your class is not actually
implemented in Delphi to begin with? You should not be using the
DELPHICLASS macro unless the class is actually implemented in Delphi
Pascal
code, which is not the case in your example.
Gambit
|
Ok! I will try show the real problem more clear.
I have 3 classes:
The first is a pure virtual class with no data... This class have a method
Show. This class is called PureVirtualClass.
The second is a TObject derived class using the first class like an
interface. This class implement the Show method of first class. This class
is called BaseClass.
The last class is derived from the fisrt overriding Show method. This class
is called DerivedClass.
see the problem:
BaseClass * var1 = new DerivedClass();
PureVirtualClass * var2 = new DerivedClass();
var1->Show(); // Show Derived as espected
var2->Show(); // Show Base!!! Why?
Declaration of class:
class PureVirtualClass
{
public:
virtual void Show() = 0;
};
class BaseClass:
public TObject, // Must be inherited from TObject
public PureVirtualClass
{
public:
virtual void Show() { // Implement the method
ShowMessage("Base");
}
};
class DerivedClass:
public BaseClass
{
public:
virtual void Show() { // Override th method
ShowMessage("Derived");
}
};
|
|
| Back to top |
|
 |
Liz Albin Guest
|
Posted: Thu Jun 23, 2005 3:31 pm Post subject: Re: delphi interfaces with inheritance |
|
|
On Thu, 23 Jun 2005 11:48:07 -0300, Tales Aguiar wrote:
| Quote: | class BaseClass:
public TObject, // Must be inherited from TObject
public PureVirtualClass
{
public:
virtual void Show() { // Implement the method
ShowMessage("Base");
}
};
|
You're not allowed to use multiple inheritence with VCL classes, sorry.
What you can do is derive your pure class from TObject or, you can derive
it from TInterfacedObject (check the online help for details)
e.g.
class PureVirtualClass : public TObject
{
public:
virtual void Show() = 0;
};
class BaseClass: public PureVirtualClass
//public TObject, // Must be inherited from TObject
{
public:
virtual void Show() { // Implement the method
ShowMessage("Base");
}
};
class DerivedClass:
public BaseClass
{
public:
virtual void Show() { // Override th method
ShowMessage("Derived");
}
};
--
Good luck,
liz
|
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Thu Jun 23, 2005 3:44 pm Post subject: Re: delphi interfaces with inheritance |
|
|
Liz Albin <lizalbin (AT) yahooNotThis (DOT) com> writes:
| Quote: | On Thu, 23 Jun 2005 11:48:07 -0300, Tales Aguiar wrote:
class BaseClass:
public TObject, // Must be inherited from TObject
public PureVirtualClass
{
public:
virtual void Show() { // Implement the method
ShowMessage("Base");
}
};
You're not allowed to use multiple inheritence with VCL classes, sorry.
|
BCB6 introduced limited support for multiple inheritance with VCL
classes, provided that the other base classes had only pure virtual
(or static member) functions and no data. Still, cross-casting
through dynamic_cast does not work, which is a big omission, IMHO.
Prior to BCB6, no multiple inheritance with the VCL was allowed at
all.
--
Chris (TeamB);
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Jun 23, 2005 5:08 pm Post subject: Re: delphi interfaces with inheritance |
|
|
"Liz Albin" <lizalbin (AT) yahooNotThis (DOT) com> wrote
| Quote: | You're not allowed to use multiple inheritence
with VCL classes, sorry.
|
That feature was finally added in BCB6, but only for abstract interfaces,
which Tales is using.
Gambit
|
|
| Back to top |
|
 |
Tales Aguiar Guest
|
Posted: Thu Jun 23, 2005 6:14 pm Post subject: Re: delphi interfaces with inheritance |
|
|
Ok But:
1 - If my BaseClass derive from TObject --> DerivedClass casted to PureClass
don't work
2 - If my BaseClass derive from amother c++ class --> DerivedClass casted to
PureClass work
3 - If my BaseClass derive from amother c++ class with no virtual methods
and no data --> The compiler generate an error: F1004 Internal compiler
error at 0xc65566 with base 0xc10000
I use CBuilder 6 update 4.
:-(
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote
| Quote: |
"Liz Albin" <lizalbin (AT) yahooNotThis (DOT) com> wrote in message
news:19ldcijsnlxgh$.wh5zrp6fzx3y$.dlg (AT) 40tude (DOT) net...
You're not allowed to use multiple inheritence
with VCL classes, sorry.
That feature was finally added in BCB6, but only for abstract interfaces,
which Tales is using.
Gambit
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Jun 23, 2005 6:46 pm Post subject: Re: delphi interfaces with inheritance |
|
|
"Tales Aguiar" <tqueiroz (AT) engesis (DOT) com> wrote
| Quote: | 1 - If my BaseClass derive from TObject --> DerivedClass casted
to PureClass don't work
|
How exactly are you casting it? As Chris already mentioned, dynamic_cast
does not work for VCL classes with interfaces.
Gambit
|
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Thu Jun 23, 2005 7:01 pm Post subject: Re: delphi interfaces with inheritance |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote:
| Quote: | How exactly are you casting it? As Chris already mentioned, dynamic_cast
does not work for VCL classes with interfaces.
|
TObject isn't true C++ ...
Tales: the other question is - why do you need to derive from TObject?
_Do_ you need to derive from TObject at all?
Alan Bellingham
--
Me <url:mailto:alanb (AT) episys (DOT) com> <url:http://www.doughnut.demon.co.uk/>
ACCU - C, C++ and Java programming <url:http://accu.org/>
The 2004 Discworld Convention <url:http://dwcon.org/>
|
|
| Back to top |
|
 |
Tales Aguiar Guest
|
Posted: Thu Jun 23, 2005 7:18 pm Post subject: Re: delphi interfaces with inheritance |
|
|
| Quote: | How exactly are you casting it? As Chris already mentioned, dynamic_cast
does not work for VCL classes with interfaces.
TObject isn't true C++ ...
Tales: the other question is - why do you need to derive from TObject?
_Do_ you need to derive from TObject at all?
|
not exacly from TObject, but from TForm and TFrame. I use TObject to
simplify.
I have an TForm that implement an interface, inherited from IInterface, but
this form is only the base class for amother forms.
When I override one method of this interface does not work. Ok I can't use
dynamic_cast. Have amother way to extract the correct interface? Or build
it?
Tales
|
|
| Back to top |
|
 |
|