我重载运算符 << 如下:
std::ostream& operator<<(std::ostream& o, const MyClass& myobj);
Run Code Online (Sandbox Code Playgroud)
现在,我想要有两个版本的operator<<,一个会显示我的对象的简短描述,另一个会显示较长的版本。
例如,MyClass 可以包含有关客户端的信息。在短版本中,我将仅显示姓名,在长版本中,我将显示更多详细信息,例如生日、地址等。
有没有办法在 C++ 中做到这一点?
我知道我可以有一个接收流的 MyClass 方法,但它会被这样调用:
myObj.DisplayShort(cout)
Run Code Online (Sandbox Code Playgroud)
或者
myObj.DisplayLong(cout)
Run Code Online (Sandbox Code Playgroud)
但我想保留类似于通常形式的语法:
cout << myObj << endl;
Run Code Online (Sandbox Code Playgroud) 作为我的大学任务的一部分,我需要为有理数编写类,覆盖数学运算符,比较运算符等。但我还需要重载强制转换为short,int和long类型。这是我的班级的简化代码:
class RationalNumber {\n long long numerator, divider;\npublic:\n RationalNumber() : numerator(0), divider(1) {}\n RationalNumber(long long numerator, long long divider = 1) : numerator(numerator), divider(divider) {}\n \n // Let\'s take only one math operator\n RationalNumber operator*(const RationalNumber& other) {\n return { numerator * other.numerator, divider * other.divider };\n }\n\n // And one cast operator\n operator int() {\n return numerator / divider;\n }\n //...\n};\nRun Code Online (Sandbox Code Playgroud)\n问题是,如果我现在想将 RationalNumber 对象乘以int,我会收到一条错误消息,指出此操作不明确:
int main() {\n int …Run Code Online (Sandbox Code Playgroud) 我出于std::list教育目的重新实现。
我想知道是否有一种方法可以允许用户使用此语法进行初始化:
my::list l = {1, 2, 3, 4, 5};
Run Code Online (Sandbox Code Playgroud) 我有这两个重载方法定义:
string[] Test1(string data)
Run Code Online (Sandbox Code Playgroud)
和
string[] Test1(params string[] input)
Run Code Online (Sandbox Code Playgroud)
当我Test1("Single test data")意图调用第二个方法时,第一个方法被调用。
为什么编译器不像更明显的重载冲突场景那样标记这种情况?
我正在尝试使用两个const参数创建一个重写的运算符函数,但我无法弄清楚如何做到这一点.这是一个简单的例子:
class Number
{
Number()
{
value = 1;
};
inline Number operator + (const Number& n)
{
Number result;
result.value = value + n.value;
return result;
}
int value;
}
Run Code Online (Sandbox Code Playgroud)
我在这里尝试做的是将两个参数传递给另外的函数,这两个参数都是const并返回结果而不更改类中的任何内容:
const Number a = Number();
const Number b = Number();
Number c = a + b;
Run Code Online (Sandbox Code Playgroud)
这是可能的,我将如何做到这一点?
谢谢,
担
我有一大堆的是具有不同的含义很多的整数代码(我宁愿一个通用的解决方案,但对于一个具体的例子:某一天的 - 月月对比的最年与年等).我希望能够基于这些含义重载类构造函数.
例如
int a; // takes role A
int b; // takes role B
var A = new Foo(a); // should call one constructor
var B = new Foo(b); // should call another constructor
Run Code Online (Sandbox Code Playgroud)
现在很明显,这将无法工作,但如果我可以定义一个类型(而不仅仅是一个别名),int但是这个类型的名称是:
typedef int TypeA; // stealing the C syntax
typedef int TypeB;
Run Code Online (Sandbox Code Playgroud)
我可以做我需要的重载,让类型系统跟踪什么是什么.特别是这将允许我确保值不会混淆,例如,作为一年的函数返回的值不用作月中的日期.
有没有办法短的class或struct包装在C#这样做吗?
如果解决方案也适用于浮动和双打,那将是很好的.
指针为重载分辨率提出了一些特殊问题.
比如说,
void f(int* x) { ... }
void f(char* x) { ...}
int main()
{
f(0);
}
Run Code Online (Sandbox Code Playgroud)
调用f(0)有什么问题?如何修复f(0)的函数调用?
我有一个CheckDuration带有以下函数签名的重载实用程序方法.
private static Action<int> CheckDuration(Action action)
private static Action<int> CheckDuration<T>(Action<T> action, T arg)
Run Code Online (Sandbox Code Playgroud)
基本上CheckDuration在控制台上打印运行方法所需的时间.
现在,我想检查一个带2个参数的方法的持续时间.
所以我必须创建另一个重载CheckDuration以下方法签名.
private static Action<int> CheckDuration<T, U>(
Action<T, U> action, T arg1, U arg2)
Run Code Online (Sandbox Code Playgroud)
有没有办法更优雅地处理这个问题?
我在考虑类似的事情
private static Action<int> CheckDuration<params T>(
Action<params T> action, params T arg)
Run Code Online (Sandbox Code Playgroud)
,这显然不起作用.
[更新]我现在暂时打开这个问题,看看有没有人为这类问题想出办法.
我怎么写而不是??????? 选择适当的过载?
using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
class A {}
class B : A {}
class C : A {}
class Program
{
static void Main(string[] args)
{
var l1 = new List<C>();
var l2 = new List<C>();
Comparer<C>(l1, l2, ???????);
}
void Compare(C a, C b) { }
void Compare(B a, B b) {}
void Compare<T>(IList<T> a, IList<T> b, Action<T,T> comparator)
{
for (int i = 0; i < a.Count; i++)
comparator(a[i], b[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud) Hy,我知道这听起来是一个非常愚蠢的问题.
这是我发现的:
public static List<SomeDTO> GetData(Guid userId, int languageId)
{
// Do something here
}
public static List<int> GetData(Guid userId ,int iNumberOfItems)
{
var result = GetData(userID,0);
return (from r in result select c.id).Take(iNumberOfItems).ToList<int>();
}
Run Code Online (Sandbox Code Playgroud)
我得到一个编译错误:
ClassLibrary'已经定义了一个名为'GetData'的成员,它具有相同的参数类型
第二个只返回第一个函数的id.
我知道这不起作用.
我知道有两个返回List <>类型,但它们返回不同的类型.
有人可以解释一下为什么吗?
我怎么解决这个问题?
更新此问题在F#上解决了!