Concatenating C++ iterator ranges into a const vector member variable at construction time

后端 未结 5 1355
情歌与酒
情歌与酒 2020-12-19 06:42

I have a class X, which I provide a snippet of here:

class X {
  public:
    template 
    X(Iter begin, Iter end) : mVec(begin, end) {}         


        
5条回答
  •  攒了一身酷
    2020-12-19 07:13

    1) I would like to preserve the const on mVec, so that it is considered constant throughout the other methods of X.

    • This is a curious use of const on a member variable. And it defies good design. By definition, construction is a process which requires the object to change.

    • As for your requirement to keep the object non-modifiable -- use proper encapsulation. You should use const-member functions to expose any functionality based on your mVec for the clients of your class.

    2) I would like to avoid unnecessary copies if at all possible. That is, one solution is to have a static method that constructs a non-const temporary to range 1, inserts range 2 and returns it, and then define the concatenating constructor to

    You should be looking at move-constructors and r-value references in general (a promised goal of C++0x). Read this article.

提交回复
热议问题