how does temporary address come?

H

Hunter Hou

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
 
K

Karl Heinz Buchegger

Hunter said:
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].
 
J

John Harrison

Hunter Hou said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top