By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Jan 2 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a

ttl_id...@yahoo.com wrote:
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;

Jan 2 '07 #2

P: n/a
tt*******@yahoo.com wrote:
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;
}
// ....
};

--
Salu2
Jan 2 '07 #3

P: n/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;
}
// ....
};
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!

Jan 3 '07 #4

P: n/a
Hello!

tt*******@yahoo.com wrote:
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

--
What happens if I mention Leader Kibo in my .signature?
Jan 3 '07 #5

P: n/a
tt*******@yahoo.com wrote:
>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?
I don't understand the questions. Please post real code you try to compile.

--
Salu2
Jan 3 '07 #6

P: n/a
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!

Jan 3 '07 #7

P: n/a
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!

Jan 3 '07 #8

P: n/a
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.

Jan 3 '07 #9

P: n/a
tt*******@yahoo.com wrote:
>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?
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));
}
// ....
};

--
Clark S. Cox III
cl*******@gmail.com
Jan 3 '07 #10

P: n/a
tt*******@yahoo.com wrote:
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.

--
Salu2
Jan 3 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.