首页 \ 问答 \ 具有下限和上限的C ++数组定义?(C++ Array Definition with Lower and Upper Bound?)

具有下限和上限的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
更新时间:2023-03-26 11:03

最满意答案

因为,一般来说,这将是合理的建议。 如果您在基础实现中更改某些内容,则调用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任何对AbstractBaseClassBaseClass添加将在您当前的实现中被反序列化。

所以是的,你可以说当前的实现是一个设计错误,虽然我可能更可能称之为设计的错误实现。


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) constructor

public 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 the this(string fileName, string password) constructor, instead of base(SerializationInfo, StreamingContext) constructor. And that's the scenario that this warning is all about. The setup is not future proof, and any addition to either AbstractBaseClass or BaseClass 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.Empty不会创建新对象并在堆上分配它。 此外, EventArgs.Empty是Null对象模式的一个实例。 具有表示“无值”的对象以避免在使用它时检查null。 要在应该使用EventArgs或正确的类时添加更多内容,这里有一些关于事件设计的MSDN指南: 考虑使用System.EventArgs的派生类作为事件参数,除非您完全确定事件永远不需要将任何数据携带到事件处理方法,在这种情况下,您可以直接使用System.EventArgs类型。 如果定义一个事件,该事件采用Event ...
  • 有多种方法可以做到这一点,例如: 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 ...
  • 已经实现了几种不同的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 ( ...
  • 问题是$db不在范围内(因此它被初始化为null )。 如果你有错误报告到E_ALL你会看到关于尝试使用未初始化的变量$db的通知......你需要以某种方式将$db带入函数范围: 如果它是全球可变的: function checkCredentials($username, $password) { global $db; 如果它是一个成员变量(该函数实际上是一个对象中的方法): function checkCredentials($username, $password) { $q = ...
  • 因为,一般来说,这将是合理的建议。 如果您在基础实现中更改某些内容,则调用base将确保一切都能继续工作。 编译器与您没有相同的知识。 你知道没有必要调用base,编译器没有。 编译器确实知道你正在做的事情并不违法,所以它只是发出一个警告,告诉你有一些代码可能需要你注意(并且警告总是引起你的注意)。 如果这真的是你想要的,请禁止警告,但我个人会调用基础构造函数,就像编译器建议的那样。 你现在在做什么并没有错。 它是完全有效的C#代码,但编译器决定发出警告,因为它“知道”(为简单起见)关于如何通常实现ISer ...
  • 几点意见: 静态方法总是可以实现为不使用任何实例变量的实例方法,因此应该可以解决您的问题而无法覆盖静态方法。 我不明白为什么启发式板正在扩展板。 我认为使用一个像scoreBoard()这样的方法的Solver类更有意义。 2048不是一个极小极大的游戏,因为'对手'没有选择'min'选项,而是一个随机选项。 因此,使用的优化器是expectimax。 A couple of observations: A static method can always be implemented as an inst ...
  • 这可能是我有限的想象力和糟糕的设计技巧,但每当我遇到上述要求时,我都被迫做出某种妥协。 这就是为什么在OOP中也很难: UI将了解这些类型,并相应地显示每种类型。 这是有问题的部分。 据我所知,只有两种方法可以满足这一要求: 将所有“子类型”视为特例 。 如果使用OOD执行此操作,则意味着客户端(UI) 必须知道所有可用的子类型。 它可以以临时方式尝试向下转换,也可以利用访问者模式 ,但在任何一种情况下,它都违反了开放/封闭原则 ,因为您无法在不修改的情况下向系统添加新的“提供者” UI代码。 让所有“子类 ...
  • 您要做的是更改现有theBar对象的框架,而不是实例化新框架。 你可以这样做: - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration { CGRect f = CGRectMake(0, ...
  • 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=='- ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。