Questions tagged [member-initialization]

121 questions
2
votes
4 answers

Assignment operation in member initializer lists

I have the following Stack class. class Stack{ public: int size; int* x; Stack() : size(10), x(new int[10]) {} Stack(const Stack& s) : x(new int[size=s.size]) {} }; Notice the assignment in the copy constructor. The code works,…
2
votes
1 answer

C++: Initialize array of objects with parameters within another class

I'm trying to create a class that will have array of objects of another class as its member. This "lower" class constructor demands a parameter (no default c-tor), and I'm not sure how to do this. In .hpp class ProcessingElement : public…
Rorschach
  • 734
  • 2
  • 7
  • 22
2
votes
0 answers

Are the use of member functions within initialiser lists good or bad practice?

If I have a class which will initialise a member value in such a way that it is not trivial enough to be performed directly within the initialiser list, what is the correct way of initialising that member variable? class Rectangle { private: …
2
votes
1 answer

How the member's initial value like 'contents(ht * wd, c)' works in constructor function in C++ Primer?

I have read the book C++ Primer. In section 7.3.1: there is a constructor of Screen class: class Screen { public: typedef std::string::size_type pos; Screen() = default; Screen(pos ht, pos wd, char c): height(ht), width(wd), …
zhenguoli
  • 2,268
  • 1
  • 14
  • 31
2
votes
1 answer

Constant variable initializing working only with member initialization list

Consider the code snippet class Test{ const int i; public: // Test(int x):i(x){} This works Test(int x){ i=x; } //this doesn't work Why does the inline member initialization list work while the normal initialization doesn't work?
2
votes
1 answer

Modifying a member initializer list

I have some code which effectively reduces to #include class A { std::vector m_sizes; public: A(std::initializer_list const& sizes) : m_sizes(sizes) {} }; class B { A m_a; public: B(int size_front, int size_back,…
Brett Ryland
  • 1,045
  • 1
  • 9
  • 17
2
votes
2 answers

Order of member initialization list

After simplifying my code for many times, I found the following cause the problem. class B { public: B(const int x) :_x(x) {} const int _x; }; class C { public: C(const B& b) : _b(b), _b2(_b._x) {} B _b2; …
hamster on wheels
  • 2,771
  • 17
  • 50
2
votes
1 answer

Mersenne Twister seed as a member variable

I want to know how to keep a mersenne random number generator as a member variable and use it in the same class. I wrote the class as below and it works perfectly, but I do not like the that std::mt19937 is initialized. I would like to know whether…
Tharindu Kumara
  • 4,398
  • 2
  • 28
  • 46
2
votes
3 answers

How to pass pointer of a class member in a member initialization list?

I have a class named HighWaterDetector: class HighWaterDetector { public: HighWaterDetector(Device* device); Device * devicePtr; Output * output1Ptr; CloudMsgParser * cloudMsgParserPtr; Output output1; NCD2Relay ncd2Relay; …
Felix
  • 51
  • 2
  • 10
2
votes
2 answers

Nested Class. error: expected parameter declarator - for inner class instance

I started learning about nested classes in C++, I tried a quick code which I pasted here to see how nested classes work. But the compilation end with some errors which I can't figure out. File: check.cpp class Outside{ public: class…
jblixr
  • 1,345
  • 13
  • 34
2
votes
1 answer

What is wrong with this Ember.js handlebars if statement?

Hi i have an ember app with a handlebars if else statement that always shows the if part and never the else I have read everything i can in the ember documentation here and here and searched the net countless times:( The code for my controller and…
2
votes
1 answer

Is there a non-repetitive way to allow the programmer to choose between copy and move semantics for member initialization?

I want to be able to initialize each field of a class either using move semantics or copy semantics. The constructors will all use essentially the same code for construction, like this: LogRecord::LogRecord(const Logger &logger, LogLevel level,…
RPFeltz
  • 1,049
  • 2
  • 12
  • 21
2
votes
1 answer

Dealing with hard to avoid large numbers of const members in a C++ class

This is a followup question on my previous question: Initialize const members using complex function in C++ class In short, I have a program that has a class Grid that contains the properties of a 3D grid. I would like the properties of this grid…
Chiel
  • 6,006
  • 2
  • 32
  • 57
2
votes
1 answer

String Array Expecting Declaration?

I'm hitting the following issue in this simple code: Public Class BookStoreDatabase Public publicationArray(0 To 3) As String publicationArray(0) = "Stories to Scare People With" End Class The bit "publicationArray(0) etc" is telling me that a…
Brocktoon
  • 63
  • 1
  • 7
2
votes
1 answer

Clang++ non-static data member initialization error? C++11

I can't seem to figure out what Clang is saying or whether it's right as G++-4.7 seems to compile it fine. The error comes from trying to initialize std::uniform_int_distribution with curly braces for a non-static member. The following fails…
norcalli
  • 1,215
  • 2
  • 11
  • 22
1 2 3
8 9