具有下限和上限的C ++数组定义?(C++ Array Definition with Lower and Upper Bound?)
我女儿的第12个标准C ++教科书说
数组的符号可以(也)如下给出:数组名称[下限L,上限U]
这对我来说是一个惊喜。 我知道Pascal有这个符号,但是C ++? 以前从未见过。 我在她的规定编译器(古代Turbo C ++ 4.5)中编写了一个快速程序,并且不支持它。 在Stanley Lippman的书中也没有找到这种语法。 互联网搜索没有吐出这个。 或者,也许我没有正确搜索?
那么,这是一个有效的声明吗?
My daughter's 12th standard C++ textbook says that
the notation of an array can (also) be given as follows: Array name [lower bound L, upper bound U]
This was a surprise for me. I know Pascal has this notation, but C++? Had never seen this earlier. I wrote a quick program in her prescribed compiler (the ancient Turbo C++ 4.5), and that does not support it. Did not find this syntax in Stanley Lippman's book either. Internet search did not throw up this. Or maybe I didn't search correctly?
So, is it a valid declaration?
原文:https://stackoverflow.com/questions/27297501
最满意答案
因为,一般来说,这将是合理的建议。 如果您在基础实现中更改某些内容,则调用base将确保一切都能继续工作。
编译器与您没有相同的知识。 你知道没有必要调用base,编译器没有。 编译器确实知道你正在做的事情并不违法,所以它只是发出一个警告,告诉你有一些代码可能需要你注意(并且警告总是引起你的注意)。
如果这真的是你想要的,请禁止警告,但我个人会调用基础构造函数,就像编译器建议的那样。
你现在在做什么并没有错。 它是完全有效的C#代码,但编译器决定发出警告,因为它“知道”(为简单起见)关于如何通常实现
ISerializable
与序列化构造函数的组合。假设在下一次迭代中,
BaseClass
添加了自己的一些属性,即Foo属性,并假设我们希望该属性(de-)序列化。 您可能会在BaseClass(SerializationInfo,StreamingContext)构造函数中实现反序列化所需的代码public class BaseClass : AbstractBaseClass, ISerializable { public int Foo { get; set; } protected BaseClass(SerializationInfo info, StreamingContext context) : base(info, context) { // ... Foo = info.GetInt32("Foo"); // ... } }
在当前的设置中,当反序列化
FinalClass
,BaseClass的Foo属性不会反序列化,因为您决定调用this(string fileName, string password)
构造函数,而不是base(SerializationInfo, StreamingContext)
构造函数。 这就是这个警告的全部情况。 该设置不是未来证明,并且在将来也应该在构造函数中反序列BaseClass
任何对AbstractBaseClass
或BaseClass
添加将在您当前的实现中不被反序列化。所以是的,你可以说当前的实现是一个设计错误,虽然我可能更可能称之为设计的错误实现。
Because, in general, that would be sound advice. Should you ever change something in the base implementation, calling base would ensure that everything will keep working.
The compiler doesn't have the same knowledge as you. You know it's not necessary to call base, the compiler doesn't. The compiler does know that it's not illegal what you're doing, so it just emits a warning, telling you that there's some code that might require your attention (and warnings always should get your attention).
If this is really what you want, suppress the warning, but I personally would call the base constructor, just like the compiler suggests.
What you are doing now is not wrong perse. It's perfectly valid C# code, but the compiler decided to issue a warning because it "knows" (for simplicity's sake) about how
ISerializable
in combination with a serialization constructor is usually implemented.Let's say in a next iteration
BaseClass
adds some property of its own, the Foo property, and assume that we want that property (de-)serialized. You would probably implement the necessary code for deserialization in the BaseClass(SerializationInfo, StreamingContext) constructorpublic class BaseClass : AbstractBaseClass, ISerializable { public int Foo { get; set; } protected BaseClass(SerializationInfo info, StreamingContext context) : base(info, context) { // ... Foo = info.GetInt32("Foo"); // ... } }
In your current setup, when deserializing
FinalClass
, the Foo property of BaseClass would not be deserialized, since you decided to call thethis(string fileName, string password)
constructor, instead ofbase(SerializationInfo, StreamingContext)
constructor. And that's the scenario that this warning is all about. The setup is not future proof, and any addition to eitherAbstractBaseClass
orBaseClass
in the future that should also be deserialized in the constructor will in your current implementation not be deserialized.So yes, you could say that the current implementation is a design mistake, although I would probably more likely call it a wrong implementation of the design.
相关问答
更多-
正确使用EventArgs(Proper usage of EventArgs)[2022-02-14]
使用EventArgs.Empty不会创建新对象并在堆上分配它。 此外, EventArgs.Empty是Null对象模式的一个实例。 具有表示“无值”的对象以避免在使用它时检查null。 要在应该使用EventArgs或正确的类时添加更多内容,这里有一些关于事件设计的MSDN指南: 考虑使用System.EventArgs的派生类作为事件参数,除非您完全确定事件永远不需要将任何数据携带到事件处理方法,在这种情况下,您可以直接使用System.EventArgs类型。 如果定义一个事件,该事件采用Event ... -
多对多 - 正确使用(Many to many - proper usage)[2023-09-28]
有多种方法可以做到这一点,例如: class UserAppForm(ModelForm): class Meta: model = User fields = ['apps'] 在您的视图中,您可以配置路由,以便您可以: 获取要选择的用户的ID 如果找不到ID,则引发错误(而不是创建新ID) 用户初始化表单 例如: def user_add_app(request, pk): user = get_object_or_404(User, pk=pk) if requ ... -
适当的原型继承(Proper Prototypal Inheritance)[2022-12-05]
已经实现了几种不同的JavaScript继承方式,这最终是我在为一个浏览器RPG构建一个JavaScript游戏引擎时所用到的: 玩家基类: function Player(name, type, gender, experience, avatar){ this.name = name; this.type = type; this.gender = gender; this.experience = experience; this.avatar = avatar ... -
有一件事,你可以使用多重继承for mixins 。 您可以使用mixins为在另一个类(要混合的类)中定义的类添加功能。 一个特别关于C ++中mixin的链接: MixinsForCeePlusPlus One thing that you can use multiple inheritance for is for mixins. You can use mixins to add functionality to a class that is defined in another class ( ...
-
正确使用MySQLi(Proper usage of MySQLi)[2022-02-04]
问题是$db不在范围内(因此它被初始化为null )。 如果你有错误报告到E_ALL你会看到关于尝试使用未初始化的变量$db的通知......你需要以某种方式将$db带入函数范围: 如果它是全球可变的: function checkCredentials($username, $password) { global $db; 如果它是一个成员变量(该函数实际上是一个对象中的方法): function checkCredentials($username, $password) { $q = ... -
因为,一般来说,这将是合理的建议。 如果您在基础实现中更改某些内容,则调用base将确保一切都能继续工作。 编译器与您没有相同的知识。 你知道没有必要调用base,编译器没有。 编译器确实知道你正在做的事情并不违法,所以它只是发出一个警告,告诉你有一些代码可能需要你注意(并且警告总是引起你的注意)。 如果这真的是你想要的,请禁止警告,但我个人会调用基础构造函数,就像编译器建议的那样。 你现在在做什么并没有错。 它是完全有效的C#代码,但编译器决定发出警告,因为它“知道”(为简单起见)关于如何通常实现ISer ...
-
适当的Java继承结构(Proper Java inheritance structure)[2024-02-01]
几点意见: 静态方法总是可以实现为不使用任何实例变量的实例方法,因此应该可以解决您的问题而无法覆盖静态方法。 我不明白为什么启发式板正在扩展板。 我认为使用一个像scoreBoard()这样的方法的Solver类更有意义。 2048不是一个极小极大的游戏,因为'对手'没有选择'min'选项,而是一个随机选项。 因此,使用的优化器是expectimax。 A couple of observations: A static method can always be implemented as an inst ... -
这可能是我有限的想象力和糟糕的设计技巧,但每当我遇到上述要求时,我都被迫做出某种妥协。 这就是为什么在OOP中也很难: UI将了解这些类型,并相应地显示每种类型。 这是有问题的部分。 据我所知,只有两种方法可以满足这一要求: 将所有“子类型”视为特例 。 如果使用OOD执行此操作,则意味着客户端(UI) 必须知道所有可用的子类型。 它可以以临时方式尝试向下转换,也可以利用访问者模式 ,但在任何一种情况下,它都违反了开放/封闭原则 ,因为您无法在不修改的情况下向系统添加新的“提供者” UI代码。 让所有“子类 ...
-
iOS willRotateToInterfaceOrientation正确使用(iOS willRotateToInterfaceOrientation proper usage)[2022-10-17]
您要做的是更改现有theBar对象的框架,而不是实例化新框架。 你可以这样做: - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration { CGRect f = CGRectMake(0, ... -
正确使用all()函数(The proper usage of all () function)[2022-06-08]
all需要一个可迭代的,所以让我们给它一个: >>> all(c=='-' for c in '-------') True >>> all(c=='-' for c in '------x') False all(...)永远是True或False ,绝不是"-" ,这就是为什么你的例子无法工作的原因。 all requires an iterable, so let's give it one: >>> all(c=='-' for c in '-------') True >>> all(c=='- ...