初始化块C ++&爪哇 [英] Initializing blocks C++ & Java

查看:74
本文介绍了初始化块C ++&爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我读过一些文章说,在Java中有一种叫做初始化块的东西.加载Class或创建实例后,我们可以在其中执行一些初始化分配.

Hi all,

I read in some articles that there is something called Initializing blocks in java ; where we can perform some initializing assignements When the Class is loaded or an instance is created.

class InitDemo
{
    static int y;
    int x;
     
     {
       y=10;
       x = 0;
     }
}



我问C ++中有这样的范例吗?

谢谢.



I am asking is there is such paradigme in C++ ?

Thank you.

推荐答案

我不会称其为范例";这只是一个很小的语法功能.看来StackOverflow已经为您提供了答案;请检查一下.

我需要在此答案中添加一些内容.首先,建议在C ++初始化语句中初始化实例(非静态)字段,该语句仅适用于构造函数,并且是重要的傻瓜语法功能.查看x(0)如何初始化x:

I would no call it a "paradigm"; this is just a tiny syntactic feature. It looks like StackOverflow already gave you an answer; please check it.

I need to add something to this answer. First, instance (non-static) fields are recommended to be initialized in C++ initialization statement, which applies only to constructors and is important fool-proof syntactic feature. See how x(0) initializes x:

class InitDemo {
    static int y;
    int x;
public:
    InitDemo() : x(0) { }
};

// this line cannot be in a header file:
int InitDemo::y = 10;



问题是静态字段的初始化.当然,您可以在构造函数中对其进行初始化,但其结果将不是您想要的.考虑:



The problem is initialization of a static field. Of course, you could initialize it in the constructor, but the result of it would not be what you want. Consider:

class InitDemo {
    static int y;
    int x;
public:
    InitDemo() : x(0) {
        y = 10; // WARNING! Don't repeat at home (unless you know exactly what are you doing :-)
    }
};



它至少可以工作一次,但是想像一下,如果您想在此类的所有实例之间累积一些值,会发生什么情况.在这种情况下,每个构造函数都会将y的修改后的值重置为10,从而有效地取消以前的计算结果.

所以你怎么看?与Java相比看起来丑陋?在.NET中,非常方便的静态构造函数非常适合此目的.我个人认为C ++是一种荒谬的古老语言(尽管它取得了明显的成功),但是有太多的C ++爱好者愿意为这样的话而scratch目结舌……我已经准备好接受不赞成投票…:-)

—SA



It works at least once, but imagine what happens if you want to accumulate some value across all the instances of this class. In this case, each constructor would reset the modified value of y to 10, effectively cancelling the result of previous calculations.

So, what do you think? Looks ugly compared to Java? And in .NET, there are very handy static constructors perfect for the purpose. I personally consider C++ as a ridiculously archaic language (despite its apparent success), but there are too many C++ fans ready to scratch out my eyes for such words… I''m prepared for down-votes… :-)

—SA


在/a头文件中:
In the/a header file:
class InitDemo
{
  static int y; //Declaration of static member variable ''y''
  int x; //Declaration of member variable ''x''
  
  InitDemo(); //Declaration of constructor
};



在/a cpp文件中:



in the/a cpp file:

int InitDemo::y = 1; //Initialization of static member variable ''y''

InitDemo::InitDemo() //Implementation of constructor
{
  x = 2; //initialization of member variable ''x''
}





Does this answer your question?


好吧,基于另一个论坛,我想这样的概念在C ++中不存在.

学习新事物很不错:)
Ok, based on another forum, i guess that such concept does''not exist in C++.

It''s nice to learn new things :)


这篇关于初始化块C ++&爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆