int a=b; and b=a; both beeing function calls?

T

ttl_idiot

I think this is a C++ question, but you might think its basic:

How can I in a c++ environment let

b when used as an r-value, as in

a = b; // where a is declared as int

mean that a function should be called, and at the same time let

b = a; // a is still declared as int

mean that another function should be called, with the argument a.

The first I can solve with
#define b f()
and the second I can solve with b being an object and overloading the
assignment operator.
But how can I do both at the same time?

(Please dont tell me to rewrite the code - that is, besides beeing
obvious, totally out of the question, due to reasons that is a little
hard to explain - but it has to do with using multiple compilers at the
same time, one of them beeing pure C.)

If this simply cant be done withing the C++ language, it would be
useful for me to know. Any comments please? Thank you.
 
N

Noah Roberts

I think this is a C++ question, but you might think its basic:

How can I in a c++ environment let

b when used as an r-value, as in

a = b; // where a is declared as int

mean that a function should be called, and at the same time let

b = a; // a is still declared as int


struct B
{
int b;
B(int x) : b(x) {}
operator int () { return b; }
};

B b(5);
int a = b;

a = 7;

b = a;
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

b when used as an r-value, as in
a = b; // where a is declared as int
mean that a function should be called, and at the same time let
b = a; // a is still declared as int
mean that another function should be called, with the argument a.

class B
{
// ....
public:
operator int () const
{
// Whatever you want for a= b
}
B & operator = (int a)
{
// Whatever you want for b= a
return * this;
}
// ....
};
 
T

ttl_idiot

class B
{
// ....
public:
operator int () const
{
// Whatever you want for a= b
}
B & operator = (int a)
{
// Whatever you want for b= a
return * this;
}
// ....
};

That is great! Thank you (to all replyers). Lets say x and y are
declared to be of the class B above, then I want

x = y;

to compile to a call to the first function (operator int etc) and the
result of that to be sent as an argument to the second function
(operator = etc). I assume this will not happen automatically? Could
this be done by hiding the simple just-copy-operator = ? or perhaps
some other way?

I would also want, for instance,

x &= 0x0F;

to compile to one call to the "operator int" -function, the result to
be anded with 0x0F and then sent as an argument to the "operator ="
-function. I assume this will not happen automatically either? I would
have to manually define all operators that I want get working in this
way, correct?

Thank you very much!
 
S

Simon G Best

Hello!

I think this is a C++ question, but you might think its basic:

How can I in a c++ environment let

b when used as an r-value, as in

a = b; // where a is declared as int

mean that a function should be called, and at the same time let

b = a; // a is still declared as int

mean that another function should be called, with the argument a.
....

(Please dont tell me to rewrite the code - that is, besides beeing
obvious, totally out of the question, due to reasons that is a little
hard to explain - but it has to do with using multiple compilers at the
same time, one of them beeing pure C.)

I would like to know why you're wanting to do such curious things.
Please do explain :)

Simon
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

That is great! Thank you (to all replyers). Lets say x and y are
declared to be of the class B above, then I want

x = y;

to compile to a call to the first function (operator int etc) and the
result of that to be sent as an argument to the second function
(operator = etc). I assume this will not happen automatically?

I don't understand the questions. Please post real code you try to compile.
 
T

ttl_idiot

I don't understand the questions. Please post real code you try to compile.

I am not yet implementing, just checking what could possibly be done,
so I have no code to post. Your solution works, both

a = b; // and
b = a; // will compile to what I want,

(remember a is a normal int and b is an object.)

but moreover I need for example

b = b;

to compile to the same thing as

{
int temp;
temp = b;
b = temp;
}

but it will not do that, will it? Can I hide the original = operator
from the compiler so it will have to use the conversions to and then
from int?

Sorry if I am not making myself too little unclear. Thank you!
 
T

ttl_idiot

I don't understand the questions. Please post real code you try to compile.

I am not yet implementing, just checking what could possibly be done,
so I have no code to post. Your solution works, both

a = b; // and
b = a; // will compile to what I want,

(remember a is a normal int and b is an object.)

but moreover I need for example

b = b;

to compile to the same thing as

{
int temp;
temp = b;
b = temp;
}

but it will not do that, will it? Can I hide the original = operator
from the compiler so it will have to use the conversions to and then
from int?

Sorry if I am not making myself too little unclear. Thank you!
 
T

ttl_idiot

I would like to know why you're wanting to do such curious things.
Please do explain :)

Fair enough. I work with a C compiler that runs on a PC but compiles
for a non-PC architecture, where some variables, with special names,
are placed at fixed places in memory. The exact locations of those
variables are specified in the C source code, with a special
compiler-specific syntax. But in reality they are not just variables,
but instead special hardware functions. So now to what I WANT to do.

I want to be able to compile the program in Visual C++ and run the
program on the PC, which of course lacks the special hardware
functions. Those hardware functions I want to emulate. But in order to
do that, the variable-like syntax a=b; etc must work in the VC++
environment. Somehow.
 
C

Clark S. Cox III

I am not yet implementing, just checking what could possibly be done,
so I have no code to post. Your solution works, both

a = b; // and
b = a; // will compile to what I want,

(remember a is a normal int and b is an object.)

but moreover I need for example

b = b;

to compile to the same thing as

{
int temp;
temp = b;
b = temp;
}

but it will not do that, will it? Can I hide the original = operator
from the compiler so it will have to use the conversions to and then
from int?

Just implement it yourself:
class B
{
// ....
public:
operator int() const
{
// Whatever you want for a= b
}

B &operator=(int a)
{
// Whatever you want for b= a
return * this;
}

B &operator=(const B &b)
{
return operator=(static_cast<int>(b));
}
// ....
};
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

a = b; // and
b = a; // will compile to what I want,

(remember a is a normal int and b is an object.)

but moreover I need for example

b = b;

to compile to the same thing as

{
int temp;
temp = b;
b = temp;
}

but it will not do that, will it?

It seems that you are looking for ways to make the compiler read your mind.
It does not do such things. You need to provide all operators you want. You
can also provide some conversions to be used automatically in some cases,
but that way can be confusing, and you may need to write a bunch of them to
avoid all possible ambiguities, and debugging can be a nightmare.
 

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,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top