Board index » cppbuilder » use/efficiency of a cast vs constructor

use/efficiency of a cast vs constructor


2006-06-22 12:31:41 AM
cppbuilder47
Hi all,
The answer to this question may depend on the class being used, but I'll use
AnsiString in this example:
Given:
char sometext[] = "some text";
...are the following exactly equivelent?
1) AnsiString(sometext);
2) (AnsiString)sometext;
If not, what is the functional and/or efficiency difference between them?
I suspect that the cast (2) causes the compiler to create a temporary object
that is constructed as if (1) were used, but I'd like some confirmation on
that.
Thanks,
- Dennis
 
 

Re:use/efficiency of a cast vs constructor

"Dennis Jones" < XXXX@XXXXX.COM >wrote:
Quote

what is the [...] efficiency difference between them?
I would expect the char array to generate fewer instructions
but I can't state any facts - I just think it.
Quote
I suspect that the cast (2) causes the compiler to create a
temporary object that is constructed as if (1) were used,
That's correct.
~ JD
 

Re:use/efficiency of a cast vs constructor

Dennis Jones wrote:
Quote
...are the following exactly equivelent?

1) AnsiString(sometext);

2) (AnsiString)sometext;

If not, what is the functional and/or efficiency difference between them?
char sometext[] = "1 2 3";
// For all of the following:
// AnsiString::AnsiString(char const *)
AnsiString s1 = sometext;
AnsiString s2 = AnsiString(sometext);
AnsiString s3 = (AnsiString) sometext;
AnsiString s4, s5, s6;
// For all of the following
// AnsiString::AnsiString(char const *)
// AnsiString::operator=(AnsiString const &)
s4 = sometext;
s5 = AnsiString(sometext);
s6 = (AnsiString) sometext;
All conversions from char const * to AnsiString is done via AnsiString
constructors. Temp objects are skipped in initalization, as permitted in
8.5/14. If the compiler chose to, it would be permitted to create temp
objects for the s2 and s3 initializations, but not for s1.
As AnsiString don't have assignment operators for char const *, only for
AnsiString const &, all the assignments need to create a temp AnsiString
first, and then assign. If AnsiString however *did* have assignment
operators for char const * the picture would be different, as
"s4=sometext;" would avoid the temp object.
Quote
I suspect that the cast (2) causes the compiler to create a temporary object
that is constructed as if (1) were used, but I'd like some confirmation on
that.
There's no difference for this particular case. It will vary with which
constructors and assignment operators are present, and with whether the
compiler uses the optimization possibilities the standard permits.
--Erik
 

{smallsort}

Re:use/efficiency of a cast vs constructor

The differences between them may vary depending upon your compiler options,
particularly the ones related to optimization.
I remember back when I was in school and forced to take a Philosophy class.
They spoke of how philosophers back in the days of Aristotle used to sit for
a day debating the subject of how many teeth were in their mouth while not
one counted the number of teeth.
We now know that it is better to work from fact than opionion. Look at what
the compiler does to see the answers to your questions.
Type that code into a test app, start in the de{*word*81} and when you get to
the line, look in the CPU window to see the exact generated code.
Alternatively compile it from the command line with the -S option and look
in the generated *.ASM file to see the generated code.
. Ed
Quote
Dennis Jones wrote in message
news:4499746c$ XXXX@XXXXX.COM ...

The answer to this question may depend on the class being
used, but I'll use AnsiString in this example:

Given:
char sometext[] = "some text";

...are the following exactly equivelent?
1) AnsiString(sometext);
2) (AnsiString)sometext;

If not, what is the functional and/or efficiency difference between
them?

I suspect that the cast (2) causes the compiler to create a
temporary object that is constructed as if (1) were used, but I'd
like some confirmation on that.
 

Re:use/efficiency of a cast vs constructor

"Ed Mulroy" < XXXX@XXXXX.COM >wrote in message
Quote
The differences between them may vary depending upon your compiler
options,
particularly the ones related to optimization.

I remember back when I was in school and forced to take a Philosophy
class.
They spoke of how philosophers back in the days of Aristotle used to sit
for
a day debating the subject of how many teeth were in their mouth while not
one counted the number of teeth.
Ed, I take it you're not a philosopher? :-)
- Dennis
 

Re:use/efficiency of a cast vs constructor

Quote
Ed, I take it you're not a philosopher? :-)
Saw no future in it. Look anywhere you want, the newspaper, monster,
hotjobs, wherever. See if you can find job listings that call for a
philosopher.
I am almost the antithesis, an engineer but if I had it to do it over again,
I'd probably go into law.
. Ed
Quote
Dennis Jones wrote in message
news:4499bd01$ XXXX@XXXXX.COM ...
 

Re:use/efficiency of a cast vs constructor

At 23:05:59, 21.06.2006, Ed Mulroy wrote:
Quote
They spoke of how philosophers back in the days of Aristotle
used to sit for a day debating the subject of how many teeth were in
their mouth while not one counted the number of teeth.
Ah, silly approach. Note that not all philosophers are that disconnected
to reality, though (especially if one of those teeth starts to hurt). <g>
--
Rudy Velthuis [TeamB] rvelthuis.de/
"Whenever I climb I am followed by a dog called 'Ego'."
- Friedrich Nietzsche (1844-1900)
 

Re:use/efficiency of a cast vs constructor

Ed Mulroy wrote:
Quote
Saw no future in it. Look anywhere you want, the newspaper, monster,
hotjobs, wherever. See if you can find job listings that call for a
philosopher.
Advert: Philosopher required to re-educate children about ethics and
morality...
As predicted in 1931 by Olaf Stapledon in Last and First Men, this bit is
about the next 10,000 years (paraphrased by me from memory) :-
"An Americanized planet followed shortly, and the mainstay ethics involved a
permanently distracted existence where trains of thought and subjects like
philosophy were banned/conditioned out worldwide. The society panned out as a
shallow sham where the only thing they revered was flying. They would put
newborn children on the wings of a flying plane, and if it hung on, it
survived, otherwise it would be killed. To the people of this unthinking
society, this seemed like a natural selection procedure."
We're almost there now. For more info see www.jacobsm.com/books.htm
--
?
Mark Jacobs
DK Computing
www.dkcomputing.co.uk
 

Re:use/efficiency of a cast vs constructor

Of you could think of the old science fiction short story about a war in
which both sides have fought to where neither can proceed further (ala WW1
in the trenches). The whole population is now mentally militarized. The
general says he can win if he has certain types of people to work on the
problem and advertises for some philosophers. Then he waits, and waits, and
....
Quote
...where the only thing they revered was flying ...
The "only thing" part is a problem, but flying is worth getting into.
However don't just get your licence for powered, also get approved for
sailplanes. Sailplanes are to powered aircraft as sailboats are to powered
boats. You go out in your motorboat and go to the other shore and back and
you're done. You go out in your sailboat and you've interesting sailing all
day. Think of dancing around Mount Blanc.
www.stemme.de/daten/e/index.html
. Ed
Quote
Mark Jacobs wrote in message
news: XXXX@XXXXX.COM ...

Advert: Philosopher required to re-educate children about ethics and
morality...

As predicted in 1931 by Olaf Stapledon in Last and First Men, this bit is
about the next 10,000 years (paraphrased by me from memory) :-
"An Americanized planet followed shortly, and the mainstay ethics involved
a permanently distracted existence where trains of thought and subjects
like philosophy were banned/conditioned out worldwide. The society panned
out as a shallow sham where the only thing they revered was flying. They
would put newborn children on the wings of a flying plane, and if it hung
on, it survived, otherwise it would be killed. To the people of this
unthinking society, this seemed like a natural selection procedure."

We're almost there now. For more info see
www.jacobsm.com/books.htm
 

Re:use/efficiency of a cast vs constructor

Bad link, sorry.
Try this instead:
www.stemme.de/daten/e/produkte/s10/video.htm
. Ed
Quote
Ed Mulroy wrote in message
news:449be451$ XXXX@XXXXX.COM ...