 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jennifer-Ashley Guest
|
Posted: Fri Mar 30, 2007 7:30 pm Post subject: Comparing objects |
|
|
What is the C++Builder equivalent again of the Delphi statement comparing
two objects:
if OneObject is AnotherObject then. |
|
| Back to top |
|
 |
OBones Guest
|
Posted: Fri Mar 30, 2007 7:41 pm Post subject: Re: Comparing objects |
|
|
Jennifer-Ashley wrote:
| Quote: | What is the C++Builder equivalent again of the Delphi statement comparing
two objects:
if OneObject is AnotherObject then.
|
if (dynamic_cast<TSomeType*>(OneObject))
{
} |
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Fri Mar 30, 2007 9:57 pm Post subject: Re: Comparing objects |
|
|
OBones <obones_gfd_ (AT) _gfz_altern (DOT) org> writes:
| Quote: | Jennifer-Ashley wrote:
What is the C++Builder equivalent again of the Delphi statement
comparing two objects:
if OneObject is AnotherObject then.
if (dynamic_cast<TSomeType*>(OneObject))
{
}
|
Better:
if (TSomeType * obj = dynamic_cast<TSomeType*>(OneObject))
{
// use obj
}
This keeps obj confined to the scope where it could be valid, and
avoids having to do a 2nd cast.
--
Chris (TeamB); |
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Sat Mar 31, 2007 12:00 am Post subject: Re: Comparing objects |
|
|
| Quote: | if (dynamic_cast<TSomeType*>(OneObject))
{
}
Better:
if (TSomeType * obj = dynamic_cast<TSomeType*>(OneObject))
{
// use obj
}
This keeps obj confined to the scope where it could be valid,
and avoids having to do a 2nd cast.
|
In the first example the dynamic_cast return value is a pointer to a type of
TSomeType or is NULL.
In the second example the dynamic_cast return value is a pointer to a type
of TSomeType or is NULL and is then assigned to a temporary declared in the
boolean expression area of an 'if' statement.
Assuming a smart compiler the second example will be compiled as that of the
first. If the compiler's optimization is less smart the second example will
generate the same code plus more code and/or a storage location or register
assignment.
How is the second example better than the first example? Where does the
issue of "confined to the scope where it could be valid" arise?
.. Ed
| Quote: | Chris Uzdavinis wrote in message
news:86zm5u7jpz.fsf (AT) explicit (DOT) atdesk.com...
What is the C++Builder equivalent again of the Delphi statement
comparing two objects:
if OneObject is AnotherObject then.
if (dynamic_cast<TSomeType*>(OneObject))
{
}
Better:
if (TSomeType * obj = dynamic_cast<TSomeType*>(OneObject))
{
// use obj
}
This keeps obj confined to the scope where it could be valid,
and avoids having to do a 2nd cast. |
|
|
| Back to top |
|
 |
Duane Hebert Guest
|
Posted: Sat Mar 31, 2007 12:21 am Post subject: Re: Comparing objects |
|
|
"Ed Mulroy" <dont_email_me (AT) bitbuc (DOT) ket> wrote in message
news:460d5eac$1 (AT) newsgroups (DOT) borland.com...
| Quote: | if (dynamic_cast<TSomeType*>(OneObject))
{
}
Better:
if (TSomeType * obj = dynamic_cast<TSomeType*>(OneObject))
{
// use obj
}
This keeps obj confined to the scope where it could be valid,
and avoids having to do a 2nd cast.
In the first example the dynamic_cast return value is a pointer to a type
of
TSomeType or is NULL.
In the second example the dynamic_cast return value is a pointer to a type
of TSomeType or is NULL and is then assigned to a temporary declared in
the
boolean expression area of an 'if' statement.
Assuming a smart compiler the second example will be compiled as that of
the
first. If the compiler's optimization is less smart the second example
will
generate the same code plus more code and/or a storage location or
register
assignment.
How is the second example better than the first example? Where does the
issue of "confined to the scope where it could be valid" arise?
|
In the first example, how would you use the result of the dynamic cast? |
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Sat Mar 31, 2007 12:24 am Post subject: Re: Comparing objects |
|
|
"Ed Mulroy" <dont_email_me (AT) bitbuc (DOT) ket> wrote:
| Quote: | if (dynamic_cast<TSomeType*>(OneObject))
{
}
Better:
if (TSomeType * obj = dynamic_cast<TSomeType*>(OneObject))
{
// use obj
}
This keeps obj confined to the scope where it could be valid,
and avoids having to do a 2nd cast.
In the first example the dynamic_cast return value is a pointer to a type of
TSomeType or is NULL.
|
Yes
| Quote: | In the second example the dynamic_cast return value is a pointer to a type
of TSomeType or is NULL and is then assigned to a temporary declared in the
boolean expression area of an 'if' statement.
|
Yes
| Quote: | Assuming a smart compiler the second example will be compiled as that of the
first. If the compiler's optimization is less smart the second example will
generate the same code plus more code and/or a storage location or register
assignment.
|
An extra assignment ought to be all. For it to do more than store a
register would be abysmal code quality, and there's a pretty good chance
it won't need to do even that.
| Quote: | How is the second example better than the first example? Where does the
issue of "confined to the scope where it could be valid" arise?
|
Because the storage location will be used anyway.
The first form will be something like
if (dynamic_cast<TSomeType*>(OneObject))
{
TSomeType * obj = static_cast<TSomeType*>(OneObject);
obj->...
}
(I'm assuming that the downcast can be done statically.), whereas the
second is
if (TSomeType * obj = dynamic_cast<TSomeType*>(OneObject))
{
obj->...
}
which is simpler.
(The variable may conceivably not be used, but if so, why downcast at
all, then? It'd not pass my code reviews, on the basis of being
pointless.)
| Quote: | Where does the
issue of "confined to the scope where it could be valid" arise?
|
Imagine the user leaving the pointer declaration outside the scope
TSomeType * obj;
if (obj = dynamic_cast<TSomeType*>(OneObject))
{
obj->...
}
obj->... // ooops, it may be null
as opposed to
if (TSomeType * obj = dynamic_cast<TSomeType*>(OneObject))
{
obj->...
}
obj->... // compilation error
It's good coding practice not to have a variable visible outside its
scope of validity. That's not always possible, but it's still a good
idea when it is.
Alan Bellingham
--
Team Thai Kingdom
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html> netiquette |
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Sat Mar 31, 2007 12:41 am Post subject: Re: Comparing objects |
|
|
| Quote: | In the first example, how would you use the result of
the dynamic cast?
|
The question was how one tests that an object is of a given type. The
replies answer not this question but rather an assumed question. The answer
tells not of how one tests if an object is the same as another but rather
how one can test if the object is either of the other's type or is of a
derived type which can be interpreted as the other's because the other is of
a type in the chain of inheritance.
Nothing in the question mentioned using or creating another pointer to
object and in fact the existence of the initial object to be tested implies
that for many operations, once one verifies that it is a compatible type in
the inheritance chain, it may well be usable for the desired task without a
cast.
.. Ed
| Quote: | Duane Hebert wrote in message
news:460d6327$1 (AT) newsgroups (DOT) borland.com... |
|
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Sat Mar 31, 2007 12:47 am Post subject: Re: Comparing objects |
|
|
I saw it differently.
Restating the original question:
I have a variable, call it 'var', and I want to test if it is of a given
type.
No answer was given on how to do that and I am unsure of in C++ once can do
it.
The answer given as was to if it is either of that given type or of a type
that can be polymorphically (sp?) used as that type.
The implication is that the questioner already has a variable name that is
an object or a pointer to an object. He wishes to test if it can be used as
a certain type. If it can be, then he needs no more storage for it as he
already has it, it appeared complete with storage allocation before the code
associated with the question was encountered.
To use your phrase, the "scope of visibility" of the name began long before
the requested code performing the test occurs.
.. Ed
| Quote: | Alan Bellingham wrote in message
news:3coq03prbk3le1g0no3ogtflubtqnn158v (AT) 4ax (DOT) com...
if (dynamic_cast<TSomeType*>(OneObject))
{
}
Better:
if (TSomeType * obj = dynamic_cast<TSomeType*>(OneObject))
{
// use obj
}
This keeps obj confined to the scope where it could be valid,
and avoids having to do a 2nd cast.
In the first example the dynamic_cast return value is a pointer to a type
of
TSomeType or is NULL.
Yes
In the second example the dynamic_cast return value is a pointer to a type
of TSomeType or is NULL and is then assigned to a temporary declared in
the
boolean expression area of an 'if' statement.
Yes
Assuming a smart compiler the second example will be compiled as that of
the
first. If the compiler's optimization is less smart the second example
will
generate the same code plus more code and/or a storage location or
register
assignment.
An extra assignment ought to be all. For it to do more than store a
register would be abysmal code quality, and there's a pretty good chance
it won't need to do even that.
How is the second example better than the first example? Where does the
issue of "confined to the scope where it could be valid" arise?
Because the storage location will be used anyway.
The first form will be something like
if (dynamic_cast<TSomeType*>(OneObject))
{
TSomeType * obj = static_cast<TSomeType*>(OneObject);
obj->...
}
(I'm assuming that the downcast can be done statically.), whereas the
second is
if (TSomeType * obj = dynamic_cast<TSomeType*>(OneObject))
{
obj->...
}
which is simpler.
(The variable may conceivably not be used, but if so, why downcast at
all, then? It'd not pass my code reviews, on the basis of being
pointless.)
Where does the
issue of "confined to the scope where it could be valid" arise?
Imagine the user leaving the pointer declaration outside the scope
TSomeType * obj;
if (obj = dynamic_cast<TSomeType*>(OneObject))
{
obj->...
}
obj->... // ooops, it may be null
as opposed to
if (TSomeType * obj = dynamic_cast<TSomeType*>(OneObject))
{
obj->...
}
obj->... // compilation error
It's good coding practice not to have a variable visible outside its
scope of validity. That's not always possible, but it's still a good
idea when it is. |
|
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Sat Mar 31, 2007 1:01 am Post subject: Re: Comparing objects |
|
|
"Ed Mulroy" <dont_email_me (AT) bitbuc (DOT) ket> writes:
| Quote: | In the first example, how would you use the result of
the dynamic cast?
The question was how one tests that an object is of a given type. The
|
All right, I'll concede that technically the author didn't ask how to
USE the result as the target type. If answering the question was all
that was needed, then remembering the result is an unnessary
instruction.
But most of the time, code only does a type query becuase it actually
intends to use the object as the other type. Sure, cases exist where
the function's behavior depends on the dynamic type of an object but
doesn't actually use the dynamic type of the object, but those cases
are unusual.
| Quote: | replies answer not this question but rather an assumed question. The answer
|
Yep. Guilty as charged. Let's ask the original poster fill us in on
what the program will do with the result of the question. If the code
uses the dynamic_cast's result, you owe me a beer. If the code purely
wants to know the answer to the type query without actually using the resulting
type, I owe you a beer.
Fair enough?
Original Poster: Please let us know how you plan to use the result of
this type query. Thanks. (Please answer quickly, I'm thirsty.)
<g>
--
Chris (TeamB); |
|
| Back to top |
|
 |
Duane Hebert Guest
|
Posted: Sat Mar 31, 2007 1:26 am Post subject: Re: Comparing objects |
|
|
"Ed Mulroy" <dont_email_me (AT) bitbuc (DOT) ket> wrote in message
news:460d69a5 (AT) newsgroups (DOT) borland.com...
| Quote: | In the first example, how would you use the result of
the dynamic cast?
The question was how one tests that an object is of a given type. The
replies answer not this question but rather an assumed question. The
answer
tells not of how one tests if an object is the same as another but rather
how one can test if the object is either of the other's type or is of a
derived type which can be interpreted as the other's because the other is
of
a type in the chain of inheritance.
|
Well I was answering the part of your question which was basically
| Quote: | How is the second example better than the first example? Where does the
issue of "confined to the scope where it could be valid" arise?
|
especially about the scope bit.
| Quote: | Nothing in the question mentioned using or creating another pointer to
object and in fact the existence of the initial object to be tested
implies
that for many operations, once one verifies that it is a compatible type
in
the inheritance chain, it may well be usable for the desired task without
a
cast.
|
Not sure why you would use a dynamic cast to determine whether you
can use a type without a cast but I imagine that it could happen.
As for dynamic cast testing that an object is of a given type, we could
argue about that as well but here in Montreal it's 30 minutes to pub time. |
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Sat Mar 31, 2007 3:26 am Post subject: Re: Comparing objects |
|
|
"Ed Mulroy" <dont_email_me (AT) bitbuc (DOT) ket> wrote:
| Quote: | I saw it differently.
Restating the original question:
I have a variable, call it 'var', and I want to test if it is of a given
type.
|
You failed the underlying question. The unwritten one.
*WHY* does he want to know?
Just answering the written question isn't always the right thing to do.
Often we need to tease out what the underlying question actually is.
Yes, we could just answer the explicit question, but if we fail to
address the implicit question, then we fail to properly teach. I'm not
here to answer questions on a points per answer basis, where by making a
minimal answer I get more questions which mean I make more answers - I
prefer to try to understand why someone is doing something and, if
there's a higher level matter going on, to address that.
You usually do that too, I'm pretty sure. And I'm sure, at least as
often as we do. So my question to you is, why did you really object to
CU's initial answer? Because I'm not sure.
| Quote: | The implication is that the questioner already has a variable name that is
an object or a pointer to an object.
|
Pointer (or reference) - you can't dynamically cast actual objects.
| Quote: | He wishes to test if it can be used as
a certain type. If it can be, then he needs no more storage for it as he
already has it, it appeared complete with storage allocation before the code
associated with the question was encountered.
|
The point there is, how *is* he going to use it as that particular type?
You're objecting to storing the result of the cast, and yet, without
storing it, how is he to know what the result is? Setting a bool?
Recalculating later? Doing a static cast? I can't see anything which
isn't actually *worse*.
Alan Bellingham
--
Team Thai Kingdom
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html> netiquette |
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Sat Mar 31, 2007 8:10 am Post subject: Re: Comparing objects |
|
|
| Quote: | The point there is, how *is* he going to use it as that
particular type? You're objecting to storing the result of
the cast, and yet, without storing it, how is he to know
what the result is? Setting a bool?
|
The result is used because the code which uses it as that type is the target
of the 'if' statement, the code between the curly braces which follows it.
No need to store the result of the cast because if the test passes, the item
is usable as the desired object type and if not, that code never executes.
The item which is tested is already an instance name, reference or pointer
(allowing for trivial adjustment of the test line for whichever it is).
There need not be a new element declared to hold that which he already has.
The code between the curly braces need not have any effect on the return
value of the function or on the rest of the code in the function. It quite
easily could do something outside of the function such as make a native API
call to cause something to happen in the machine or such as posting a
message to somewhere else.
class SomeClass
{
public:
virtual int DoWhatever();
----blah blah blah----
};
:
class DerivedClass : public SomeClass
{
public:
----blah blah blah----
};
:
if (dynamic_cast<SomeType*>(&instance_or_reference_name))
{
instance_or_reference_name.DoWhatever();
}
else
{
DoSomethingElse();
}
.. Ed
| Quote: | Alan Bellingham wrote in message
news:9t2r03hnqgdl43chmiu1kugk81va0rmm74 (AT) 4ax (DOT) com...
I saw it differently.
Restating the original question:
I have a variable, call it 'var', and I want to test if it is of a given
type.
You failed the underlying question. The unwritten one.
*WHY* does he want to know?
Just answering the written question isn't always the right thing to do.
Often we need to tease out what the underlying question actually is.
Yes, we could just answer the explicit question, but if we fail to
address the implicit question, then we fail to properly teach. I'm not
here to answer questions on a points per answer basis, where by making a
minimal answer I get more questions which mean I make more answers - I
prefer to try to understand why someone is doing something and, if
there's a higher level matter going on, to address that.
You usually do that too, I'm pretty sure. And I'm sure, at least as
often as we do. So my question to you is, why did you really object to
CU's initial answer? Because I'm not sure.
The implication is that the questioner already has a variable name that is
an object or a pointer to an object.
Pointer (or reference) - you can't dynamically cast actual objects.
He wishes to test if it can be used
as
a certain type. If it can be, then he needs no more storage for it as he
already has it, it appeared complete with storage allocation before the
code
associated with the question was encountered.
The point there is, how *is* he going to use it as that particular type?
You're objecting to storing the result of the cast, and yet, without
storing it, how is he to know what the result is? Setting a bool?
Recalculating later? Doing a static cast? I can't see anything which
isn't actually *worse*. |
|
|
| Back to top |
|
 |
Ed Mulroy Guest
|
Posted: Sat Mar 31, 2007 8:10 am Post subject: Re: Comparing objects |
|
|
Ok Chris, a beer it is! <g>
.. Ed
| Quote: | Chris Uzdavinis wrote in message
news:86vegi7b65.fsf (AT) explicit (DOT) atdesk.com...
In the first example, how would you use the result of
the dynamic cast?
The question was how one tests that an object is of a given type. The
All right, I'll concede that technically the author didn't ask how to
USE the result as the target type. If answering the question was all
that was needed, then remembering the result is an unnessary
instruction.
But most of the time, code only does a type query becuase it actually
intends to use the object as the other type. Sure, cases exist where
the function's behavior depends on the dynamic type of an object but
doesn't actually use the dynamic type of the object, but those cases
are unusual.
replies answer not this question but rather an assumed question. The
answer
Yep. Guilty as charged. Let's ask the original poster fill us in on
what the program will do with the result of the question. If the code
uses the dynamic_cast's result, you owe me a beer. If the code purely
wants to know the answer to the type query without actually using the
resulting
type, I owe you a beer.
Fair enough?
Original Poster: Please let us know how you plan to use the result of
this type query. Thanks. (Please answer quickly, I'm thirsty.)
g |
|
|
| Back to top |
|
 |
Hendrik Schober Guest
|
Posted: Mon Apr 02, 2007 6:53 pm Post subject: Re: Comparing objects |
|
|
Duane Hebert <spoo (AT) flarn (DOT) com> wrote:
| Quote: | [...]
Not sure why you would use a dynamic cast to determine whether you
can use a type without a cast but I imagine that it could happen.
|
<nitpicking>
You could then 'static_cast'.
</nitpicking>
| Quote: | As for dynamic cast testing that an object is of a given type, we could
argue about that as well but here in Montreal it's 30 minutes to pub time.
|
<sigh style="Yossarian"/>
Schobi
--
SpamTrap (AT) gmx (DOT) de is never read
I'm HSchober at gmx dot org
"My hope is that if more people start reading books,
the world will become a better place."
froarulv in afp |
|
| Back to top |
|
 |
Torsten Franz Guest
|
Posted: Mon Apr 02, 2007 7:11 pm Post subject: Re: Comparing objects |
|
|
| Quote: | Imagine the user leaving the pointer declaration outside the scope
TSomeType * obj;
if (obj = dynamic_cast<TSomeType*>(OneObject))
{
obj->...
}
obj->... // ooops, it may be null
|
or even worse, a random memory location that is coincidentally valid...
(of course only when showing the program to the customer) |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|