How to initialize int const *const in constructor?


Yongjian Wang

经验。我有一个

class foo{
    public:
      int const * const array;
      size_t const length;
}

构造后这些变量不应该有任何变化,包括任何成员方法,但每个人都应该可以访问这些值,因此它们应该是常量。

但是在构造函数中,我需要先确定长度,然后才能初始化数组,

此外,我需要调用一个函数来分配内存位置,而不仅仅是一个new,因为这个类是一个巨大的不透明数据结构的桥梁,并且内存是由那个人管理的。(考虑诸如 v8 之类的东西)。

我该如何初始化?

ps 让我们只调用 allocator void * bar(size_t),构造函数(也许)看起来像:

foo(size_t const len, int const *arr) {
    this->array = reinterpret_cast<int *> (bar(len));
    this->length = len;
    for(size_t i = 0; i < len; i++) array[i] = arr[i];
}
加利克

您需要像这样使用构造函数的成员初始值设定项列表

class Test {

    public:
    Test(size_t length): length(length), array(func_to_allocate(length)) {}

    size_t const length;
    int const * const array;
};

注意:构造函数的主体中没有任何内容,{}所有初始化都运行之前发生

Related


How to initialize int*const*const?

aluminum I need a 2d array with fixed width and height that can only change a single value stored in it. It is declared in the header and then initialized in the source file. What I found led me to try the following code snippet; unfortunately the question abo

How to properly initialize const int const* variables?

fluffy So I have a struct: typedef struct myStruct { const int *const array_ptr; } myStruct_s; I have an constarray int: const int constArray[SIZE1] = { [0] = 0, [1] = 1, [2] = 2, //... }; Now, I have an array constinitial

How to properly initialize const int const* variables?

fluffy So I have a struct: typedef struct myStruct { const int *const array_ptr; } myStruct_s; I have an constarray int: const int constArray[SIZE1] = { [0] = 0, [1] = 1, [2] = 2, //... }; Now, I have an array constinitial

Initialize const array in constructor

Hovnertan I am trying to achieve something like this struct A { A(const int (&arr)[5]) : arr_(arr) {} const int arr_[5]; } Obviously this doesn't work. My intention is to keep the arr_field constant. What is the best way to achieve this (c

Initialize const field in constructor

factory A const field in C++ must be initialized in an initialization list, which makes it trivial to compute interdependent values from constructor arguments. What's the best way to convert this Java code to c++? public class SomeObject { private final Stri

How to initialize const field in constructor in typescript?

Matthew Harwood I don't know if this has any value. I'm just curious if it's possible? question: If possible, what is the syntax for it Initialize const field in constructor in typescript? export class Gulpfile { private dist: string; private src: string;

Initialize const array in variadic constructor

Emil Kabirov I want to initialize const array from variadic parameters. But with this code, only the first value in the value array is initialized, the rest are zero. How can I fix this? I've never dealt with variadic parameters and I don't know how they basic

Initialize const array in variadic constructor

Emil Kabirov I want to initialize const array from variadic parameters. But with this code, only the first value in the value array is initialized, the rest are zero. How can I fix this? I've never dealt with variadic parameters and I don't know how they basic

Initialize const array in variadic constructor

Emil Kabirov I want to initialize const array from variadic parameters. But with this code, only the first value in the value array is initialized, the rest are zero. How can I fix this? I've never dealt with variadic parameters and I don't know how they basic

How to initialize and access const char* const*

folder I saw a C++ code with one of the structs defined as below, strangeconst char* const* struct TestStruct { const char* const* text; void* data; }; I need to use the above struct and return an object of it from a function. But for this how to ini

Initialize const vector in class constructor (C++)

space I'm trying to figure out how to initialize a const class member variable (in this case a vector) to an arbitrary value in the constructor of a class. Basically, in the class definition, I have: const vector < pair <float, float> > myVector; However, in

Initialize const c++ class without constructor

nigger I have a class that is used for unions and therefore cannot have a ( non-trivial ) constructor. I need to create a const instance of the class, is it possible to do so? which is: class Foo { // no constructors... private: int i; };

Initialize const vector in class constructor (C++)

space I'm trying to figure out how to initialize a const class member variable (in this case a vector) to an arbitrary value in the constructor of a class. Basically, in the class definition, I have: const vector < pair <float, float> > myVector; However, in

Initialize const members and use parent constructor

Benjamin Christopherson Is there a way to initialize const members in templates when inheriting Tand using using T::T? Here is an example #include <iostream> #include <ctime> #include <string> #include <memory> class A { protected: int wake_up_time_; publi

Initialize const variable in constructor after change

Jens I have a Client class which receives a std::string variable called emailAdress. Here is the code now, it works fine: private: const std::string email; Client::Client(std::string emailAddress) : email{emailAddress} { } Now I want to check if the ema

Initialize const c++ class without constructor

nigger I have a class that is used for unions and therefore cannot have a ( non-trivial ) constructor. I need to create a const instance of the class, is it possible to do so? which is: class Foo { // no constructors... private: int i; };

Initialize const variable in constructor after change

Jens I have a Client class which receives a std::string variable called emailAdress. Here is the code now, it works fine: private: const std::string email; Client::Client(std::string emailAddress) : email{emailAddress} { } Now I want to check if the ema

Initialize const vector in class constructor (C++)

space I'm trying to figure out how to initialize a const class member variable (in this case a vector) to an arbitrary value in the constructor of a class. Basically, in the class definition, I have: const vector < pair <float, float> > myVector; However, in

Initialize const vector in class constructor (C++)

space I'm trying to figure out how to initialize a const class member variable (in this case a vector) to some arbitrary value in the class constructor. Basically, in the class definition, I have: const vector < pair <float, float> > myVector; However, in the