Board index » cppbuilder » Assign or = for internal component pointers

Assign or = for internal component pointers

I have a component CA. One of the properties of CA is a pointer to
component CB. In handling this property, do I:

1) initialize the pointer to CB to 0 and handle setting this property by
using a C++ assignment ( - ) to another created instance of this component.

or

2) Initialize this pointer by dynamically creating a default instance of
CB and handle setting this property by overriding the
TPersistent::Assign for CB and using Assign to get all the values of the
assigned CB component.

ie.,

class PACKAGE CB : public TComponent
{
// various methods, properties, and/or events
public:
__fastcall CB(TComponent* Owner): TComponent(Owner) {// Some code}
virtual void __fastcall Assign(TPersistent* Source){// Some code}

Quote
};

class PACKAGE CA : public TComponent
{
private:
CB * fCB;
void __fastcall SetCB(CB *);
public:
__fastcall CA(TComponent* Owner);
published:
__property CB * CBProperty = {read = fCB, write = SetCB};

Quote
};

do this: Solution 1)
__fastcall CA::CA(TComponent* Owner) : TComponent(Owner),fCB(0){}
void __fastcall CA::SetCB(CB * aCB) { fCB = aCB }

or this: Solution 2)
__fastcall CA::CA(TComponent* Owner) : TComponent(Owner),fCB(new CB(this)){}
void __fastcall CA::SetCB(CB * aCB) {fCB->Assign(aCB)}

Does the answer make any difference if my internal component is a
pointer to a TCollection derived class, with its own TCollectionItem
derived class (substitute "public TCollection") for the base class of CB  ?

In my current code I am using Solution 1) for all cases except for the
TCollection one where I am told I must use Solution 2).

 

Re:Assign or = for internal component pointers


Is CB going to be it's own component that the user can drop on the form
independantly along side CA, or is CB really only supposed to be used as an
internal class for CA for it to store data in?  This makes a difference.

Gambit

Quote
"Edward Diener" <eddie...@tropicsoft.com> wrote in message

news:3BD73A6D.20604@tropicsoft.com...
Quote
> I have a component CA. One of the properties of CA is a pointer to
> component CB. In handling this property, do I:

> 1) initialize the pointer to CB to 0 and handle setting this property by
> using a C++ assignment ( - ) to another created instance of this
component.

> or

> 2) Initialize this pointer by dynamically creating a default instance of
> CB and handle setting this property by overriding the
> TPersistent::Assign for CB and using Assign to get all the values of the
> assigned CB component.

Re:Assign or = for internal component pointers


I think you have answered my initial question with your question. My
guess is that if CB is it's own component, linking to it uses the =
method in the setter while if CB is just an internal component, linking
to it uses the Assign method in its setter.

Is the latter case an actual linking to an internal VCL class or am I
embedding a VCL class in my component ? If it is actually embedding a
VCL class ( or component ) in my component, how does the IDE know that I
want the internal class to be represented via the internal class's
published properties in the Object Inspector. Both are using the
notation __property SomeVCLClass * aProperty = etc. How does the IDE
know that aProperty is actually a pointer to another component as
opposed to an embedded internal component. Is this the right syntax for
an embeddable component, __property SomeVCLClass aProperty = etc. ?

Sorry for the confusion. Maybe you can straighten me out on all the
differences.

Quote
Remy Lebeau wrote:
> Is CB going to be it's own component that the user can drop on the form
> independantly along side CA, or is CB really only supposed to be used as an
> internal class for CA for it to store data in?  This makes a difference.

> Gambit

> "Edward Diener" <eddie...@tropicsoft.com> wrote in message
> news:3BD73A6D.20604@tropicsoft.com...

>>I have a component CA. One of the properties of CA is a pointer to
>>component CB. In handling this property, do I:

>>1) initialize the pointer to CB to 0 and handle setting this property by
>>using a C++ assignment ( - ) to another created instance of this

> component.

>>or

>>2) Initialize this pointer by dynamically creating a default instance of
>>CB and handle setting this property by overriding the
>>TPersistent::Assign for CB and using Assign to get all the values of the
>>assigned CB component.

Re:Assign or = for internal component pointers


Quote
"Edward Diener" <eddie...@tropicsoft.com> wrote in message

news:3BD75CD2.6070301@tropicsoft.com...

Quote
> I think you have answered my initial question with your question. My
> guess is that if CB is it's own component, linking to it uses the =
> method in the setter while if CB is just an internal component, linking
> to it uses the Assign method in its setter.

Correct.  Make sure that if you do make CB into it's own component, that
your code in CA always tests it's CB pointer for NULL before trying to use
it, as you're now relying on user-interaction to assign CB to CA, and they
user may not have yet when you try to access CB.

Quote
> Is the latter case an actual linking to an internal VCL class or am I
> embedding a VCL class in my component ?

If CB is an internal class to CA, than it's it's own object.  When you
Assign() new data to it, you're not linking to anything.  You're telling CB
to make a new copy of the original data.  Even if the original data is than
destroyed afterwards, CB will still contain it, because it made a separate
copy of it.

Quote
> Both are using the notation __property SomeVCLClass * aProperty = etc. How
> does the IDE know that aProperty is actually a pointer to another
component as
> opposed to an embedded internal component.

It doesn't need to.  All it cares about is that you declared the property as
being of type SomeVCLClass*, and that's how it'll treat it as.

And also, if you're going to make CB an internal class, then don't derive it
from TComponent, derive it from TPersistent instead.  Only use TComponent as
a base class if you intend to make it into a separate component that's
installable on the Component Palette and dropped onto the form at design
time.

Gambit

Re:Assign or = for internal component pointers


Quote
Remy Lebeau wrote:
> "Edward Diener" <eddie...@tropicsoft.com> wrote in message
> news:3BD75CD2.6070301@tropicsoft.com...

>>I think you have answered my initial question with your question. My
>>guess is that if CB is it's own component, linking to it uses the =
>>method in the setter while if CB is just an internal component, linking
>>to it uses the Assign method in its setter.

> Correct.  Make sure that if you do make CB into it's own component, that
> your code in CA always tests it's CB pointer for NULL before trying to use
> it, as you're now relying on user-interaction to assign CB to CA, and they
> user may not have yet when you try to access CB.

>>Is the latter case an actual linking to an internal VCL class or am I
>>embedding a VCL class in my component ?

> If CB is an internal class to CA, than it's it's own object.  When you
> Assign() new data to it, you're not linking to anything.  You're telling CB
> to make a new copy of the original data.  Even if the original data is than
> destroyed afterwards, CB will still contain it, because it made a separate
> copy of it.

>>Both are using the notation __property SomeVCLClass * aProperty = etc. How
>>does the IDE know that aProperty is actually a pointer to another

> component as

>>opposed to an embedded internal component.

> It doesn't need to.  All it cares about is that you declared the property as
> being of type SomeVCLClass*, and that's how it'll treat it as.

Here is where you may have lost me. In one case the IDE treats my
pointer as a pointer to a separate component and in another case it
treats my pointer as a VCL class whose properties it will display. My
question was, how does it know which way to treat it ? The answer I have
gathered from your explanation is: if the pointer points to a class that
eventually derives from TComponent ( is a component ), it is treated as
a separate component, while if the pointer points to a class that
eventually derived from TPersistent but is not a component it is treated
as a VCL class whose properties it will display. Is this essentially the
way the IDE knows what my pointer actaully refers to ? If so, this is
the first time I have understood the difference, and I thank you kindly.
I just wish that this had been explained in the books I have read about
creating VCL components, or perhaps I have missed this explanation
somewhere.

- Show quoted text -

Quote
> And also, if you're going to make CB an internal class, then don't derive it
> from TComponent, derive it from TPersistent instead.  Only use TComponent as
> a base class if you intend to make it into a separate component that's
> installable on the Component Palette and dropped onto the form at design
> time.

Re:Assign or = for internal component pointers


Quote
"Edward Diener" <eddie...@tropicsoft.com> wrote in message

news:3BD783B8.1000107@tropicsoft.com...

Quote
> Here is where you may have lost me. In one case the IDE treats my
> pointer as a pointer to a separate component and in another case it
> treats my pointer as a VCL class whose properties it will display. My
> question was, how does it know which way to treat it ?

Because it knows that if you're treating CB as a pointer to an external
component, it will also treat it as an external component.  Both CA and CB
receive their own RTTI (runtime type information).

But if you use CB as an internal object, it's an integral part of CA and
they are treated as a single object.  CB becomes part of CA's RTTI, as it's
part of CA, not a separate object standing on it's own.

Quote
> The answer I have gathered from your explanation is: if the pointer points
to
> a class that eventually derives from TComponent ( is a component ), it is
> treated as a separate component, while if the pointer points to a class
that
> eventually derived from TPersistent but is not a component it is treated
> as a VCL class whose properties it will display. Is this essentially the
> way the IDE knows what my pointer actaully refers to ?

No, because TComponent is also derived from TPersistent, so that invalidates
your assumption.

Gambit

Re:Assign or = for internal component pointers


Quote
Remy Lebeau wrote:
> "Edward Diener" <eddie...@tropicsoft.com> wrote in message
> news:3BD783B8.1000107@tropicsoft.com...

>>Here is where you may have lost me. In one case the IDE treats my
>>pointer as a pointer to a separate component and in another case it
>>treats my pointer as a VCL class whose properties it will display. My
>>question was, how does it know which way to treat it ?

> Because it knows that if you're treating CB as a pointer to an external
> component, it will also treat it as an external component.  Both CA and CB
> receive their own RTTI (runtime type information).

> But if you use CB as an internal object, it's an integral part of CA and
> they are treated as a single object.  CB becomes part of CA's RTTI, as it's
> part of CA, not a separate object standing on it's own.

A component is an object. If I have a pointer to a TCollection based
object, the IDE treats it as an internal object.

- Show quoted text -

Quote

>>The answer I have gathered from your explanation is: if the pointer points

> to

>>a class that eventually derives from TComponent ( is a component ), it is
>>treated as a separate component, while if the pointer points to a class

> that

>>eventually derived from TPersistent but is not a component it is treated
>>as a VCL class whose properties it will display. Is this essentially the
>>way the IDE knows what my pointer actaully refers to ?

> No, because TComponent is also derived from TPersistent, so that invalidates
> your assumption.

That is not what I said. "while if the pointer points to a class
that eventually derived from TPersistent but is not a component, it is
treated as a VCL class whose properties it will display."

Other Threads