473,788 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Style (or otherwise?) question: creating temporary to invoke function

Let's say you're in some function , and you want to accomplish some task x
that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it? (This
is actually the sort of thing that might be
provided for with a static class function, but one is not provided in this
case - an instance must be created.)

void f()
{
A a;
a.x();

A* p = new A();
p->x();
delete p;

A().x();
}
Jul 22 '05 #1
23 1322
jeffc wrote:
Let's say you're in some function and you want to accomplish
some task x that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it?
(This is actually the sort of thing that might be provided for
with a static class function,
but one is not provided in this case - an instance must be created.)
void f(void) {
A a;
a.x(); // OK

A* p = new A();
p->x();
delete p; // Never!

A().x(); // Best
}


Leaving a reference lying around
to an object that should never be reference again
is an invitation for trouble.
If some other programmer (meaning you after a while) maintaining f()
doesn't realize that the object to which p points has been deleted
and decides to add code to modify the object through p,
The function may or may not exhibit the expected behavior
and the bug may be very difficult to detect and fix.

Jul 22 '05 #2
Ian
jeffc wrote:
Let's say you're in some function , and you want to accomplish some task x
that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it? (This
is actually the sort of thing that might be
provided for with a static class function, but one is not provided in this
case - an instance must be created.)

void f()
{
A a;
a.x();
Fine, a on stack.
A* p = new A();
p->x();
delete p;
Has to call new and allocate p from heap, OTT.
A().x();
Fine and concise!

Ian
}

Jul 22 '05 #3
"jeffc" <no****@nowhere .com> wrote in message
news:40******** @news1.prserv.n et...
Let's say you're in some function , and you want to accomplish some task x
that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it? (This is actually the sort of thing that might be
provided for with a static class function, but one is not provided in this
case - an instance must be created.)

void f()
{
A a;
a.x();

A* p = new A();
p->x();
delete p;

Do the second if for some reason you can't do the first.

A().x();

Do this paranoid stuff if for some reason you cannot do the previous two.


Ioannis Vranos

Jul 22 '05 #4
jeffc wrote:
Let's say you're in some function , and you want to accomplish some task x
that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it? (This
is actually the sort of thing that might be
provided for with a static class function, but one is not provided in this
case - an instance must be created.)

void f()
{
A a;
a.x();

A* p = new A();
p->x();
delete p;

A().x();
}


If I wanted to do something like this, I imagine I'd ask myself, "Should
A::x() be static?"

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #5

"Ioannis Vranos" <iv*@guesswh.at .emails.ru> wrote in message
news:c5******** **@ulysses.noc. ntua.gr...
"jeffc" <no****@nowhere .com> wrote in message
news:40******** @news1.prserv.n et...
Let's say you're in some function , and you want to accomplish some task x that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it?

(This
is actually the sort of thing that might be
provided for with a static class function, but one is not provided in this case - an instance must be created.)

void f()
{
A a;
a.x();

A* p = new A();
p->x();
delete p;


Do the second if for some reason you can't do the first.
A().x();


Do this paranoid stuff if for some reason you cannot do the previous two.


There was nothing paranoid about it - merely succinctness. Which is not to
say I prefer succinctness over clarity. In fact, in the specific situation
I'm looking at, there is a parameter list that's long (3 very long parameter
names) so that the statement will span multiple lines. Therefore I don't
think I'll use the last technique, because I think the actual function call
(which is an extremely short name) will get lost in the soup.
Jul 22 '05 #6

"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:8d******** **********@news read2.news.pas. earthlink.net.. .

If I wanted to do something like this, I imagine I'd ask myself, "Should
A::x() be static?"


You didn't read carefully :-)
Jul 22 '05 #7
jeffc wrote:
Let's say you're in some function , and you want to accomplish some task x
that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it? (This
is actually the sort of thing that might be
provided for with a static class function, but one is not provided in this
case - an instance must be created.)

void f()
{
A a;
a.x();

A* p = new A();
p->x();
delete p;

A().x();
}


If class 'A' doesn't have any internal state or you don't care about
that state, then

A().x();

is the best way to do it. Otherwise, use

A a;
a.x();

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #8
Kevin Goodsell wrote:
...
If I wanted to do something like this, I imagine I'd ask myself, "Should
A::x() be static?"
...


Yes, by of 'x' happens to be 'operator ()' then you are out of luck,
since 'operator ()' cannot be static.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #9
"jeffc" <no****@nowhere .com> wrote in message
news:40******** @news1.prserv.n et...

"Ioannis Vranos" <iv*@guesswh.at .emails.ru> wrote in message
news:c5******** **@ulysses.noc. ntua.gr...
"jeffc" <no****@nowhere .com> wrote in message
news:40******** @news1.prserv.n et...
Let's say you're in some function , and you want to accomplish some
task
x that is provided for you by a class, A.
Which of these do you prefer, and is there a technical reason for it? (This
is actually the sort of thing that might be
provided for with a static class function, but one is not provided in this case - an instance must be created.)

void f()
{
A a;
a.x();

A* p = new A();
p->x();
delete p;


Do the second if for some reason you can't do the first.
A().x();


Do this paranoid stuff if for some reason you cannot do the previous

two.
There was nothing paranoid about it - merely succinctness.
I did not mean to insult you, i spoke a bit freely. Let me rephrase: Do this
temporary object stuff ...

Which is not to
say I prefer succinctness over clarity. In fact, in the specific situation I'm looking at, there is a parameter list that's long (3 very long parameter names) so that the statement will span multiple lines. Therefore I don't
think I'll use the last technique, because I think the actual function call (which is an extremely short name) will get lost in the soup.

Well if the long function names bother you, you can do friend functions in
the style:
inline whatever_the_me thod_returns function1(A &x, whatever_parame ter y)
{
return x.the_long_vers ion_member_func tion(y);
}
// ...

A a;

function1(a, y);
Of course there are millions of things you can do like,

class capsule: public A
{
public:
// Your new short-name versions of functions accessing the old ones
// with *this
};
or
class capsule
{

public:
int some_func()
{
A a;

return a.x();
}
};

or

class capsule
{
A a;
// A *a;

public:
int some_func() { // ... }
// ...
};
// ...


Ioannis Vranos

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3835
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}): class C(object): pass C.__bases__ = bases dict = 0
1
2685
by: xtra | last post by:
Hi Folk I have written a module that allows you to type a bunch of commands in the immediate window, for quick access to information when you are creating VB code. Here it is, it may be helpful to you (I find it pretty fast and friendly).... Some of it is specific to my database (I left out many as well which were too specific), so you will have to rewrite it for yours, but you may find it useful.... Any questions, please ask. Typing...
8
2394
by: Todd Smith | last post by:
I'm wondering if anyone knows where I can learn a good programming method for creating windows apps. I'm just starting to learn windows programming with MSVC++ 6.0. I want to learn low level windows programming in C first. I don't want to learn any bad habits by hacking away at code until I get something to work. Specifically, I'd like to learn how to set up and plan out a C windows project and break it down into pieces of code that will...
31
2020
by: Christopher Benson-Manica | last post by:
How about your if/else if/else constructs? Being nitpicky like any good C programmer, I'm in the process of transforming code written like if( cond ) { ... } else if( some_other_cond ) { ... } else
144
6975
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force everybody else to write code the way he likes it, not for any rational reason, but simply for the...
83
15629
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include <stdio.h> #include <string.h> void main() { char *str1="abcd";
1
1837
by: Mladen Adamovic | last post by:
Why is memory safe that function allocate inner object and return its result? That inner object is destroyed when function come to the "return" statement. Is default copy contructor doing some job? Can somebody explain me what exactly is happening in the following code when it invoke boo myres=calculateSomething(12); #include <iostream> #include <cstdlib> #include <math.h>
49
2329
by: Luke Meyers | last post by:
Lately I find myself increasingly preferring the practice of going ever-so-slightly out of my way to avoid the use of the form of initialization which uses the '=' symbol, on the grounds that it can be mistaken too easily for assignment. I like the avoidance of mistakes. I like even more the frequent, subtle reinforcement (for myself and my colleagues) of the point that assignment and initialization are entirely different operations. ...
8
1881
by: kevin | last post by:
Hello! So I was reading O'Reilly's C++ in a Nutshell when I came accross something interesting: The class definition for basic_ostream contains numerous overloaded operator<< functions: template <class charT, class traits = char_traits<charT class basic_ostream : virtual public basic_ios<charT,traits>
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.