Board index » cppbuilder » Classes and Inheritance

Classes and Inheritance


2003-08-06 01:37:18 AM
cppbuilder81
Hi
I'm still in the process of writing my component and need some more help. I
have a load of classes I made in the component kind of like this
My Component
-TMySettingsClass
-TMyLogsClass
-TMyFilesClass
The TMyFilesClass's base class is the TMyLogsClass and the TMyLogsClass's
base class is the TMySettingsClass. Which seems to be working great, from
TMyFilesClass I have access to all my settings in the TMySettingsClass
Now here's the question.
If I was to add a TMyNewClass class to the TMyFilesClass (initialize
within), which has TPersistent as is base class is there anyway from inside
the TMyNewClass I can still access the TMySettingsClass so I don't have to
have another copy of the settings floating about?
kind of like (code wise)
// ---------------
__fastcall TMySettingsClass::TMySettingsClass() : TPersistent()
{
}
// ---------------
__fastcall TMyLogsClass::TMyLogsClass() : TMySettingsClass()
{
}
// ---------------
__fastcall TMyFilesClass::TMyFilesClass() : TMyLogsClass()
{
TMyFilesClass MyNewClass = new TMyFilesClass(); // add new class
}
// ---------------
// ---------------
__fastcall TMyNewClass::TMyNewClass() : TPersistent()
{
// I'd like to be able to assess the info in TMySettingsClass()
// in this whole class like as if it had TMyFilesClass as its base
// class instead of TPersistent
}
// ---------------
I did originally have the TMyFilesClass as the base class for the
TMyNewClass but this would cause problems as I need to add 3 or 4 classes
like the TMyNewClass which in the end I'd like all to be working from the
one TMyFilesClass which has access to all the other classes that are
required instead of having to update the other TMySettingsClass for the
other TMyNewClass's(2 up to 4).
ps, sorry if this seems very badly explained, hopefully you get the jest of
what I'm trying to accomplish here ;)
Thanks again,
MwnN
 
 

Re:Classes and Inheritance

"man with no name" < XXXX@XXXXX.COM >wrote in message
Quote
I'm still in the process of writing my component and need some more help.
Questions regarding writing new components should be directed to the
.vcl.components, writing group instead.
Gambit
 

Re:Classes and Inheritance

Hi
I'm still in the process of writing my component and need some more help. I
have a load of classes I made in the component kind of like this
My Component
-TMySettingsClass
-TMyLogsClass
-TMyFilesClass
The TMyFilesClass's base class is the TMyLogsClass and the TMyLogsClass's
base class is the TMySettingsClass. Which seems to be working great, from
TMyFilesClass I have access to all my settings in the TMySettingsClass
Now here's the question.
If I was to add a TMyNewClass class to the TMyFilesClass (initialize
within), which has TPersistent as is base class is there anyway from inside
the TMyNewClass I can still access the TMySettingsClass so I don't have to
have another copy of the settings floating about?
kind of like (code wise)
// ---------------
__fastcall TMySettingsClass::TMySettingsClass() : TPersistent()
{
}
// ---------------
__fastcall TMyLogsClass::TMyLogsClass() : TMySettingsClass()
{
}
// ---------------
__fastcall TMyFilesClass::TMyFilesClass() : TMyLogsClass()
{
TMyFilesClass MyNewClass = new TMyFilesClass(); // add new class
}
// ---------------
// ---------------
__fastcall TMyNewClass::TMyNewClass() : TPersistent()
{
// I'd like to be able to assess the info in TMySettingsClass()
// in this whole class like as if it had TMyFilesClass as its base
// class instead of TPersistent
}
// ---------------
I did originally have the TMyFilesClass as the base class for the
TMyNewClass but this would cause problems as I need to add 3 or 4 classes
like the TMyNewClass which in the end I'd like all to be working from the
one TMyFilesClass which has access to all the other classes that are
required instead of having to update the other TMySettingsClass for the
other TMyNewClass's(2 up to 4).
ps. Sorry if this seems very badly explained, hopefully you get the jest of
what I'm trying to accomplish here ;)
Thanks again,
MwnN
pps . I originally posted in the cppbuilder.students group and was informed
to post here instead because of the nature of the post. Please don't view
this post as a cross post.
 

{smallsort}

Re:Classes and Inheritance

man with no name wrote:
Quote
Hi

[snip]
Please dont post to multiple news groups - only the most appropriate one.
Ash Berlin
 

Re:Classes and Inheritance

"man with no name" < XXXX@XXXXX.COM >wrote in message news:3f3034c6$ XXXX@XXXXX.COM ...
Quote
__fastcall TMyNewClass::TMyNewClass() : TPersistent()
{
// I'd like to be able to assess the info in TMySettingsClass()
// in this whole class like as if it had TMyFilesClass as its base
// class instead of TPersistent
}
When constructing your new class you should pass in a pointer to
the object that created it.
Implement the concept of an Owner:
class PACKAGE TMyNewClass : public TPersistent
{
private:
TPersistent* FOwner;
...
};
__fastcall TMyNewClass::TMyNewClass(TPersistent* Owner)
{
FOwner = Owner;
}
__fastcall TMyNewClass::SomeMethod()
{
TMySettingsClass* Settings = dynamic_cast<TMySettingClass*>(FOwner);
if( Settings )
{
// now you can access public stuff in the Settings object
// as long as the Owner was derived from TMySettings
}
}
Todd
 

Re:Classes and Inheritance

Hi Ash,
As I was informed from my first post that such a question "should
be directed to the .vcl.components, writing group instead." that is
what I did.
[snip from second post]
Quote
pps . I originally posted in the cppbuilder.students group and was
informed to post here instead because of the nature of the post.
Please don't view this post as a cross post.
[snip]
Weather or not I was writing a component I was asking about
inheritance in general, but as I am using it within a component I just
mentioned it, that is why I originally thought that the students group
would have been the most appropriate one. Sorry if you
misunderstood my intensions.
MwnN
 

Re:Classes and Inheritance

"Todd Brylski" < XXXX@XXXXX.COM >wrote in message
Quote
When constructing your new class you should pass in a pointer to
the object that created it.
Implement the concept of an Owner:
<snip>
__fastcall TMyNewClass::SomeMethod()
{
TMySettingsClass* Settings = dynamic_cast<TMySettingClass*>(FOwner);
if( Settings )
{
// now you can access public stuff in the Settings object
// as long as the Owner was derived from TMySettings
}
}

Why don't you pass a TMySettingClass* in TMyNewClass's c-tor? It's not
likely that TMyNewClass is expecting another type, is it?
Ralph