473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how does temporary address come?

Hello,

I have some code to implement combination of composite and command patterns.

Here is the problem:

======

class Number {

public:
void dubble( int& value ) { value *= 2; }
};
class Command {

public:
virtual void execute( int& ) = 0;
};

class SimpleCommand : public Command {

typedef void ( Number::*Action ) ( int& );
Number* receiver;
Action action;

public:
SimpleCommand( Number* rec, Action act ) : receiver( rec ), action(
act ){}
void execute( int& num ) {
(receiver->*action) ( num );
}
};

......

int main() {

Number object;
Command* commands[ 3 ];
commands[ 0 ] = &SimpleCommand( &object, &Number::dubble ); <<<<<<<<<<<
........
}
When I compiled this program, <<<< line always said
"warning: taking address of temporary"

I didn't figure out why there's a temporary address.

Any help will be appreciated.

Thanks
hunter
Jul 22 '05 #1
2 1306

"Hunter Hou" <hy***@lucent.com> wrote in message
news:cd********@netnews.proxy.lucent.com...
Hello,

I have some code to implement combination of composite and command patterns.
Here is the problem:

======

class Number {

public:
void dubble( int& value ) { value *= 2; }
};
class Command {

public:
virtual void execute( int& ) = 0;
};

class SimpleCommand : public Command {

typedef void ( Number::*Action ) ( int& );
Number* receiver;
Action action;

public:
SimpleCommand( Number* rec, Action act ) : receiver( rec ), action(
act ){}
void execute( int& num ) {
(receiver->*action) ( num );
}
};

.....

int main() {

Number object;
Command* commands[ 3 ];
commands[ 0 ] = &SimpleCommand( &object, &Number::dubble ); <<<<<<<<<<< .......
}
When I compiled this program, <<<< line always said
"warning: taking address of temporary"

I didn't figure out why there's a temporary address.

Any help will be appreciated.

Thanks
hunter


SimpleCommand( &object, &Number::dubble ) is a temporary.

Rewrite like this

Number object;
SimpleCommand cmd1(&object, &Number::dubble );
Command* commands[ 3 ];
commands[ 0 ] = &cmd1;

or like this

Number object;
Command* commands[ 3 ];
commands[ 0 ] = new SimpleCommand( &object, &Number::dubble );

john
Jul 22 '05 #2
Hunter Hou wrote:

Number object;
Command* commands[ 3 ];
commands[ 0 ] = &SimpleCommand( &object, &Number::dubble ); <<<<<<<<<<<
.......
}

When I compiled this program, <<<< line always said
"warning: taking address of temporary"

I didn't figure out why there's a temporary address. commands[ 0 ] = &SimpleCommand( &object, &Number::dubble ); <<<<<<<<<<<


This
^ ^
| |
+---------------------------------------+

creates a temporary SimpleCommand object.
You are taking the address of it and store it in commands[0].
After that the temporary is destroyed and you are left with
a useless address in commands[0].

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

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

Similar topics

2
by: Ariel | last post by:
When passing an object by value to some function foo(), the copy constructor gets called and inside it i see that the address of the temporary obect is X. but when entering to foo() i see that the...
2
by: Raterus | last post by:
Hi, I've got a crazy idea, but I think I can get it to work, I just need some help. I want to maintain a datarow across browser sessions. I have a screen where a user will submit some...
17
by: Henning Hasemann | last post by:
I have a function which gets the adress of an object as argument. It does some comparsion with the object's contents and then returns. no Reference or pointer to the object is stored or will be...
3
by: Chris | last post by:
All I am cross-posting, as I'm not sure if this is an issue for the data layer or the application layer. If this is unacceptable, can someone let me know so that I don't do this in future. ...
11
pbmods
by: pbmods | last post by:
A somewhat obscure hack has emerged recently that is an offshoot of the now-infamous XSS. It is known as Cross-Site Request Forgery, or XSRF for short. XSRF is a form of temporary identity theft...
17
by: Klaas Vantournhout | last post by:
Hi all, I was wondering if it is possible if you can check in a function if one of the arguments is temporary. What I mean is the following. A is a class, foo is a function returning a class...
1
by: popwhub | last post by:
I am having problems printing some reports from my application that are generated from temporary tables. The application has an Access FE and a SQL Server BE. The current method uses a temporary...
2
by: anon.asdf | last post by:
Hello! 1) =============================== When trying to define an array of std::string ... func( (std::string ) { std::string("ab"), std::string("cd"), std::string("ef") } , 3 ); ...
4
by: James Kanze | last post by:
On Nov 18, 5:50 pm, Pete Becker <p...@versatilecoding.comwrote: Has this changed in the latest draft. According to my copy of the standard (version 1998---out of date, I know), "The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.