简体   繁体   English

类数组初始化

[英]Class array initialization

Good morning. 早上好。 Question is about initialisation of object. 问题是关于对象的初始化。 Its easy to do like this: 这样做很容易:

class MyClass {
  char* data;
public:
  inline MyClass(char* s) {
    data = s;
  };    
}

MyClass Obj = "foo";

But is it possible to make initialization syntax(with the same actions) like this? 但是是否可以像这样使初始化语法(具有相同的动作)?

MyClass Obj[] = "foo";

I need brackets for compatibility with other compiler :/ 我需要方括号以与其他编译器兼容:/

Have you tried using the initialization list using {} ? 您是否尝试使用{}使用初始化列表?

MyClass Obj[] = {"foo"};

or 要么

MyClass Obj[] = {"foo", "bar"};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM