C++ 具有不同签名的宏重载
是否可以重载可以使用相同宏名称执行不同运算符(、+=、-=、++、-)的宏 我想实现这样的目标:C++ 具有不同签名的宏重载,c++,gcc,macros,C++,Gcc,Macros,是否可以重载可以使用相同宏名称执行不同运算符(、+=、-=、++、-)的宏 我想实现这样的目标: int main() { LOG_STAT("hello") << "world"; LOG_STAT("hello") = 5; LOG_STAT("hello") += 10; } intmain(){ LOG_STAT(“hello”)为类创建操作符: Stat& Stat::operator += (int rhs) { number +
int main() {
LOG_STAT("hello") << "world";
LOG_STAT("hello") = 5;
LOG_STAT("hello") += 10;
}
intmain(){
LOG_STAT(“hello”)为类创建操作符:
Stat& Stat::operator += (int rhs)
{
number += rhs;
return *this;
}
Stat operator + (const Stat& lhs, int rhs)
{
Stat res(lhs);
res += rhs;
return res;
}
template <typename T>
Stat& operator << (Stat& stat, const T&value)
{
stat.stream << value;
return stat;
}
Stat&Stat::operator+=(int-rhs)
{
数字+=rhs;
归还*这个;
}
统计运算符+(常量统计和左侧,内部右侧)
{
统计资源(lhs);
res+=rhs;
返回res;
}
模板
Stat&operator什么是LOG\u Stat(“hello”)+=10;
应该是什么意思?尤其是LOG\u Stat(…)
是一个std::ostream
。宏是邪恶的。创建一个重载这些运算符的类,如果必须的话,可以执行您想要的操作。@BillLynch只是重载的另一个例子。例如,+=将增加stat类中的数字。在上面的示例中,当析构函数调用该值时,该值将为15。@NeilKirk ya,我想我想得越多,你就越对。我只是想知道这样的事情是否可能发生。宏发生了,至少在概念上是这样,在编译器理解运算符之前很久。所以,不,你不能对宏这样做。
Stat& Stat::operator += (int rhs)
{
number += rhs;
return *this;
}
Stat operator + (const Stat& lhs, int rhs)
{
Stat res(lhs);
res += rhs;
return res;
}
template <typename T>
Stat& operator << (Stat& stat, const T&value)
{
stat.stream << value;
return stat;
}
Stat("hello") << "world";
Stat("hello") = 5;
Stat("hello") += 10;