473,378 Members | 1,380 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,378 software developers and data experts.

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

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
10 1350

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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas | last post by:
Hi all, I want to execute a new program in a c++ program without beeing blocked. After the child process is created both processes should run idependend. My method at the moment: execute(...
3
by: Janross | last post by:
I'm having trouble with a query that's prohibitively slow. On my free-standing office computer it's fine (well, 2-4 seconds), but on the client's network, it takes at least 5 minutes to run. ...
5
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
9
by: Daniel Walzenbach | last post by:
Hi I am faced with the following problem: I have a page (let’s call this page page1.aspx) containing some TextBoxes and a hyperlink which opens another page (let’s call this page page2.aspx)...
6
by: Dasn | last post by:
Hi, there. 'lines' is a large list of strings each of which is seperated by '\t' I wanna split each string into a list. For speed, using map() instead of 'for' loop. 'map(str.split, lines)'...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
2
by: thomas.lidebrandt | last post by:
Hello I'm using wxPython to consruct a GUI and want to change a button label and event handler to change after the button have been pressed. The only thing I can think of is a global variable...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.