int a = b;和b = a;两个函数调用? [英] int a=b; and b=a; both beeing function calls?
问题描述
我认为这是一个C ++问题,但你可能会认为它是基本的:
我怎样才能在c ++环境中让
b当用作r值时,如
a = b; //其中a被声明为int
意味着应该调用一个函数,同时让
b = a; // a仍然被声明为int
意味着应该调用另一个函数,参数为a。
我能解决的第一个函数与
#define bf()
和第二个我可以用b作为对象解决并重载
赋值运算符。
但是我怎样才能同时做到这两件事?
(请不要告诉我重写代码 - 也就是说,除了蜜蜂之外
显而易见,完全没有问题,由于原因有点难以解释 - 但它与
同时使用多个编译器有关,其中一个是纯粹的C.)
如果不能用C ++语言完成,那么对我来说,这将是有用的。有什么意见吗?谢谢。
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.
推荐答案
ttl_id ... @ yahoo.com写道:
ttl_id...@yahoo.com wrote:
我认为这是一个C ++问题,但您可能认为它是基本的:
我如何在c ++环境中让
b当用作r值时,如
a = b; //其中a被声明为int
意味着应该调用一个函数,同时让
b = a; // a仍被声明为int
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;
struct B
{
int b;
B(int x) : b(x) {}
operator int () { return b; }
};
B b(5);
int a = b;
a = 7;
b = a;
tt ******* @ yahoo.com 写道:
tt*******@yahoo.com wrote:
b用作r值,如
a = b; //其中a被声明为int
表示应该调用一个函数,同时让
b = a; // a仍然被声明为int
意味着应该调用另一个函数,参数为a。
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 :
运算符int()const
{
//无论你想要什么= a
}
B& operator =(int a)
{
//无论你想要什么b = a
返回* this;
}
// ....
};
-
Salu2
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
class B
class B
{
// ....
public:
operator int()const
{
//无论你想要什么a = b
}
B& operator =(int a)
{
//无论你想要什么b = a
返回* this;
}
// ....
};
{
// ....
public:
operator int () const
{
// Whatever you want for a= b
}
B & operator = (int a)
{
// Whatever you want for b= a
return * this;
}
// ....
};
太棒了!谢谢(对所有回复者)。让我们说x和y是
声称属于上面的B类,然后我想要
x = y;
编译调用第一个函数(运算符int等)和
结果作为参数发送到第二个函数
(运算符) =等)。我认为这不会自动发生?这可以通过隐藏简单的just-copy-operator =来完成吗?或者也许
其他一些方式?
我还想要,例如,
x& = 0x0F ;
编译为对operator int的一次调用-function,
的结果用0x0F表示,然后作为参数发送到operator =
-function。我认为这也不会自动发生?我会
必须手动定义我想用这个
方式工作的所有运算符,对吗?
非常感谢你很多!
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!
这篇关于int a = b;和b = a;两个函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!