由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一个C#似疑模板问题
相关主题
C++的exception大家常用吗?基于macro的meta programming真难懂
Re: L 电面 (转载)《新C++标准:C++0x 概述》英文文字版[PDF]
问一个C++ template的问题C++中parse string的问题
c++ define 一问c语言abort时怎么清理堆空间?
[合集] 谁能知道这是为什么?C++问题输入参数的检查应该归caller还是callee?
Help - C++ Debug Assertion Failed请教(C++)
问一个奇怪的问题。C++11里如何限制模板只针对特定类型定义?
问行C++代码用来用去还是Rust是最好的语言,代表今后的方向
相关话题的讨论汇总
话题: list话题: c++话题: newid话题: c#话题: oo
进入Programming版参与讨论
1 (共1页)
y*******n
发帖数: 195
1
问题有点小复杂,有四个类,基本思路是想用新加入程序的A类作为B、C类(B、C类在现
有程序中已存在)的基础类,在D类里用一个函数对A类成员,也就是B、C类成员进行操
作,从此免去在B、C类重复写类似的函数:
class A
{
//ID原来是B/C类成员
public int ID = 0;
}
class B:A
{
public string str_b = "";
}
class C:A
{
public string str_c = "";
}
class D
{
List list_b = new List();
List list_c = new List();
//本来是想用List代替List,但是如果用List就不能对class A的ID
//进行操作了
static public bool func_d1(ref int NewID, List
list_a)
{
NewID = list_a[0].ID;
return true;
}
public bool
r****r
发帖数: 115
2
最近我也碰到了这个问题 (covariance),http://blogs.msdn.com/rmbyers/archive/2
005/02/16/375079.aspx
反正我最后就是写了个函数来做cast,不知道有没有更好的方法

【在 y*******n 的大作中提到】
: 问题有点小复杂,有四个类,基本思路是想用新加入程序的A类作为B、C类(B、C类在现
: 有程序中已存在)的基础类,在D类里用一个函数对A类成员,也就是B、C类成员进行操
: 作,从此免去在B、C类重复写类似的函数:
: class A
: {
: //ID原来是B/C类成员
: public int ID = 0;
: }
: class B:A
: {

s******n
发帖数: 876
3
can you do
static public bool func_d1(ref int NewID, List list_a) where T:A
{
...
}
arguments"
Generic
l**t
发帖数: 64
4
C#怎么看起来这么复杂,感觉比C++还难搞

【在 y*******n 的大作中提到】
: 问题有点小复杂,有四个类,基本思路是想用新加入程序的A类作为B、C类(B、C类在现
: 有程序中已存在)的基础类,在D类里用一个函数对A类成员,也就是B、C类成员进行操
: 作,从此免去在B、C类重复写类似的函数:
: class A
: {
: //ID原来是B/C类成员
: public int ID = 0;
: }
: class B:A
: {

c*****m
发帖数: 16
5
不复杂吧,要说起来绝对还是C++复杂啊。。。
搂住这个问题其实比较明显吧,B、C是A的子类,你去把B的list传给用A做参数的方法
,当然提示不能做类型转换
我觉得吧,简单一点的话,要弄这个就在调用D的func之前new 一个A,把B.id赋给newA
.ID然后把A扔给那个方法就是了

在现
行操

【在 l**t 的大作中提到】
: C#怎么看起来这么复杂,感觉比C++还难搞
l**t
发帖数: 64
6
但是C++实现这个功能很简单直接
bool func_d1(int& NewID, List const& list_a)
{
NewID = list_a[0].ID;
return true;
}
但C#还得明确指明T的基类,搞个where T:A,语法看起来的确复杂

newA

【在 c*****m 的大作中提到】
: 不复杂吧,要说起来绝对还是C++复杂啊。。。
: 搂住这个问题其实比较明显吧,B、C是A的子类,你去把B的list传给用A做参数的方法
: ,当然提示不能做类型转换
: 我觉得吧,简单一点的话,要弄这个就在调用D的func之前new 一个A,把B.id赋给newA
: .ID然后把A扔给那个方法就是了
:
: 在现
: 行操

s******n
发帖数: 876
7
C++ template整个一宏替换, 跟C# generics比较不公平.
语言能够表达某种constraint, 是feature, 不一定是flaw.
程序员希望能写出这个constraint, 希望compiler/runtime能够替他检查,
希望能够帮助IDE更好地理解他的意图, 希望其他程序员能读懂他要干什么.
不能因为一个弱的type system看起来简单, 就认为用起来简单.

【在 l**t 的大作中提到】
: 但是C++实现这个功能很简单直接
: bool func_d1(int& NewID, List const& list_a)
: {
: NewID = list_a[0].ID;
: return true;
: }
: 但C#还得明确指明T的基类,搞个where T:A,语法看起来的确复杂
:
: newA

t****t
发帖数: 6806
8
C++9X的template是有点象宏替换, 虽然其实真正的C++模板库不是这样用的(会手动写
出constraint). C++0X的template constraint (concept)就看上去好得多.

【在 s******n 的大作中提到】
: C++ template整个一宏替换, 跟C# generics比较不公平.
: 语言能够表达某种constraint, 是feature, 不一定是flaw.
: 程序员希望能写出这个constraint, 希望compiler/runtime能够替他检查,
: 希望能够帮助IDE更好地理解他的意图, 希望其他程序员能读懂他要干什么.
: 不能因为一个弱的type system看起来简单, 就认为用起来简单.

p***m
发帖数: 387
9
nod~~
the "where" constraint is a very nice to have feature. it also makes c# a
more OO language and makes the code easier to read.
but, on the other hand, c++ will validate at compile time too...as long as
the template is instantiated.

【在 s******n 的大作中提到】
: C++ template整个一宏替换, 跟C# generics比较不公平.
: 语言能够表达某种constraint, 是feature, 不一定是flaw.
: 程序员希望能写出这个constraint, 希望compiler/runtime能够替他检查,
: 希望能够帮助IDE更好地理解他的意图, 希望其他程序员能读懂他要干什么.
: 不能因为一个弱的type system看起来简单, 就认为用起来简单.

c**t
发帖数: 2744
10
...
public static bool func_d1(ref int NewID, List list_a)
{
Type t = typeof(T);
if (t == typeof(A))
{
NewID = (list_a[0] as A).ID;
}
return true;
}
...

【在 y*******n 的大作中提到】
: 问题有点小复杂,有四个类,基本思路是想用新加入程序的A类作为B、C类(B、C类在现
: 有程序中已存在)的基础类,在D类里用一个函数对A类成员,也就是B、C类成员进行操
: 作,从此免去在B、C类重复写类似的函数:
: class A
: {
: //ID原来是B/C类成员
: public int ID = 0;
: }
: class B:A
: {

相关主题
Help - C++ Debug Assertion Failed基于macro的meta programming真难懂
问一个奇怪的问题。《新C++标准:C++0x 概述》英文文字版[PDF]
问行C++代码C++中parse string的问题
进入Programming版参与讨论
n*w
发帖数: 3393
11
.net 4.0?
l**t
发帖数: 64
12
如果这样说,C#本质上不一样是宏替换?
首先C++不是弱的type system,另外不要轻易这样揣测别人。
C++老标准中constrain都可以构造出来,当然更方便的是使用boost的type_traits库中
写好的各种traits;新标准则是直接在语法层面支持这些,写法更简洁。C#的泛型设计
本身就借鉴了C++。
写成 bool func_d1(int& NewID, List const& list_a) 可以说更generic,类型T
不一定非要从A继承,只要T存在可访问的成员“ID”即可。这种写法很直接简洁,编译
期间也能检查出来。至于非要加继承约束,可以加一行STATIC_ASSERT(is_base_of T>::value),哪个程员读不懂这个?

【在 s******n 的大作中提到】
: C++ template整个一宏替换, 跟C# generics比较不公平.
: 语言能够表达某种constraint, 是feature, 不一定是flaw.
: 程序员希望能写出这个constraint, 希望compiler/runtime能够替他检查,
: 希望能够帮助IDE更好地理解他的意图, 希望其他程序员能读懂他要干什么.
: 不能因为一个弱的type system看起来简单, 就认为用起来简单.

l**t
发帖数: 64
13
怎么引入了constrain就能使得语言更OO?
更易读应该改成更易查找错误,泛型的不易读主要是类型推演造成的,constrain的主
要作用是便于编译器查找错误,避免出现类似于C++编译器报错位置十万八千里的情况。

【在 p***m 的大作中提到】
: nod~~
: the "where" constraint is a very nice to have feature. it also makes c# a
: more OO language and makes the code easier to read.
: but, on the other hand, c++ will validate at compile time too...as long as
: the template is instantiated.

y*******n
发帖数: 195
14
谢谢大家的讨论,问题已经解决,包子也已经发给了raider(原因:沙发和提供了MSDN
上延伸讨论), shuiguan(原因:解决方案一), cogt(原因:解决方案二)。
p***m
发帖数: 387
15
i already said, both compilers for C++ and C# will check syntax errors (for
the case of C++, when the template is instantiated, compiler will surely
find errors and see if the member functions and variables in use exist)
in C#, you could say it is for the compiler to find errors....but from
software development point of view, when you have the following code:
class C where T : A
it explicitely says: class C is a template of T, and T *IS* A. this is OO. it
guards you from logic errors not syn

【在 l**t 的大作中提到】
: 怎么引入了constrain就能使得语言更OO?
: 更易读应该改成更易查找错误,泛型的不易读主要是类型推演造成的,constrain的主
: 要作用是便于编译器查找错误,避免出现类似于C++编译器报错位置十万八千里的情况。

l**t
发帖数: 64
16
1. C++模板实例化前也会进行一些必要的syntax checking,并不是都留在实例化时才
检查
2. constrain与OO是2个概念。你所举的仅是一种情况。constrain一方面易于编译器定
位错误,这个很重要,另一方面说利于用户阅读的作用,这个不必太夸大了,其实跟注
释的作用区别不大,优点在于二合一了。另如果在注释里写T is derived from A,
where A provides method f(), 按你的逻辑,岂不是更OO?
3. C++里也有constrain。取决于你的需求,如果仅操作规定范畴内的类型,可以按需
求增加constrain。最后如果出现你说的情况,只能说模板库的设计者需求分析有问题
,将一个窄范围使用的模板错误设计为通用范围

for
. it
have
business*

【在 p***m 的大作中提到】
: i already said, both compilers for C++ and C# will check syntax errors (for
: the case of C++, when the template is instantiated, compiler will surely
: find errors and see if the member functions and variables in use exist)
: in C#, you could say it is for the compiler to find errors....but from
: software development point of view, when you have the following code:
: class C where T : A
: it explicitely says: class C is a template of T, and T *IS* A. this is OO. it
: guards you from logic errors not syn

p***m
发帖数: 387
17

in C#, here A is almost always an interface, such as ICar, IAnimal, which
explains itself very well (of what it is and what it can do) already. here
user could be an end-user who just runs the program; he could also be
another programmer who will use this piece of code in his own code.
the foundamental idea of OO is for people easy to code, easy to read, easy
to expand, and easy to share duties in a big project. so "利于用户阅读" is
very very important. otherwise, why we need OO? we could just use

【在 l**t 的大作中提到】
: 1. C++模板实例化前也会进行一些必要的syntax checking,并不是都留在实例化时才
: 检查
: 2. constrain与OO是2个概念。你所举的仅是一种情况。constrain一方面易于编译器定
: 位错误,这个很重要,另一方面说利于用户阅读的作用,这个不必太夸大了,其实跟注
: 释的作用区别不大,优点在于二合一了。另如果在注释里写T is derived from A,
: where A provides method f(), 按你的逻辑,岂不是更OO?
: 3. C++里也有constrain。取决于你的需求,如果仅操作规定范畴内的类型,可以按需
: 求增加constrain。最后如果出现你说的情况,只能说模板库的设计者需求分析有问题
: ,将一个窄范围使用的模板错误设计为通用范围
:

l**t
发帖数: 64
18
1. 你把问题扯远了,我们argure的是C# 加入 constrain 使得语言更加OO了。另外OO
的基本核心思想是封装,你所说的这些优点难道是OO特有的,难道过程式语言、函式语
言就没有,linux kernel算大工程不?虽然OO好用,但没有必要捧到天上去
2. 正如我所说过的,造成你所谓的*bussiness* error 还是syntax error,根本原因
在于库的设计者出错了。你前面的举的例子无非在说明constrain的作用,是模板库的
设计者应该考虑的问题。讨论这个也偏题了

辑,
assembly do the same thing too.
they

【在 p***m 的大作中提到】
:
: in C#, here A is almost always an interface, such as ICar, IAnimal, which
: explains itself very well (of what it is and what it can do) already. here
: user could be an end-user who just runs the program; he could also be
: another programmer who will use this piece of code in his own code.
: the foundamental idea of OO is for people easy to code, easy to read, easy
: to expand, and easy to share duties in a big project. so "利于用户阅读" is
: very very important. otherwise, why we need OO? we could just use

s******n
发帖数: 876
19
well if we want to be more generic,
#define func_d1(a, b) a=b[0].ID

T
,

【在 l**t 的大作中提到】
: 如果这样说,C#本质上不一样是宏替换?
: 首先C++不是弱的type system,另外不要轻易这样揣测别人。
: C++老标准中constrain都可以构造出来,当然更方便的是使用boost的type_traits库中
: 写好的各种traits;新标准则是直接在语法层面支持这些,写法更简洁。C#的泛型设计
: 本身就借鉴了C++。
: 写成 bool func_d1(int& NewID, List const& list_a) 可以说更generic,类型T
: 不一定非要从A继承,只要T存在可访问的成员“ID”即可。这种写法很直接简洁,编译
: 期间也能检查出来。至于非要加继承约束,可以加一行STATIC_ASSERT(is_base_of: T>::value),哪个程员读不懂这个?

l**t
发帖数: 64
20
要举例也应该这样举:bool func_d1(int& a, LIST_T const& b) {a=b[0].ID;...}
似乎你对C++模板与宏展开等同认识了
如果使用宏展开,对于 func_d1(n,b)+1 等都存在问题,且宏是对全局范围,不易控制。
C++模板可以完成类型的推演(借助于偏特化等特性),比如实现is_base_of等(参
看boost.typetraits),宏太低级,根本不可能有这样的功能

【在 s******n 的大作中提到】
: well if we want to be more generic,
: #define func_d1(a, b) a=b[0].ID
:
: T
: ,

1 (共1页)
进入Programming版参与讨论
相关主题
用来用去还是Rust是最好的语言,代表今后的方向[合集] 谁能知道这是为什么?C++问题
请教一个算法题关于shortest path的Help - C++ Debug Assertion Failed
使用assert应遵循什么原则?问一个奇怪的问题。
太监打小报告是有历史的。问行C++代码
C++的exception大家常用吗?基于macro的meta programming真难懂
Re: L 电面 (转载)《新C++标准:C++0x 概述》英文文字版[PDF]
问一个C++ template的问题C++中parse string的问题
c++ define 一问c语言abort时怎么清理堆空间?
相关话题的讨论汇总
话题: list话题: c++话题: newid话题: c#话题: oo