Questions tagged [member-initialization]

121 questions
3
votes
2 answers

Why can't access to enum here?

Here's my code/namespace: namespace myNamespace { enum MyType { ASD1, ASD2, ASD3 }; struct MyClass { MyType mMyType; MyClass(MyType myType = MyType::ASD1) : mMyType(myType) { } }; } Now if I try, within another struct,…
markzzz
  • 47,390
  • 120
  • 299
  • 507
3
votes
1 answer

How to fix derived class giving 'declaration has no storage class or type specifier' error?

I am trying to initialize a base class variable in a derived class, but instead, I am given an error: "this declaration has no storage class or type specifier" I have been tinkering with C++ inheritance and derived classes in Visual Studio. I made…
3
votes
1 answer

Which part of standard determines priority of in-place member vs initializer list?

I just made a sample, which from my personal point of view should not compile, or at least give a warning, but Visual Studio 2017 does not give any warnings. The sample is following: #include struct Foo { Foo(int i) { throw…
3
votes
4 answers

Disable default class member initialization before constructor code

In C++, any member of a class which is not constructed in a member initialization list is default constructed before the containing class's constructor is executed. However, this appears to be very wasteful if that member variable is just going to…
Marco Merlini
  • 875
  • 7
  • 29
3
votes
2 answers

Member initializer list. Order of arguments evaluation

Example of a code with a member initializer list. #include struct Throwable { Throwable() { throw "Exception!"; } }; struct A { A() : t(Throwable()), i(new int()) {} Throwable t; std::unique_ptr
3
votes
2 answers

Better way to organize initializer lists in constructors

Sometimes there is a need to do some processing on the class constructor arguments before initializing const members or those not having a default constructor. For instance, in Java I can do this (don't ask why, it's just an example): class A { …
Alex
  • 1,165
  • 2
  • 9
  • 27
3
votes
2 answers

Aggregate Member Initialization in C++14

Having this structure: struct A { struct B { int a = 21; int b; int c = 22; int d; int e = 23; }; B b1 = { 11, 12 }; B b2 = { 11, 12, 13 }; int x; }; And declaring: A a = { { 1, 2, 3, 4…
J L
  • 563
  • 3
  • 17
3
votes
2 answers

C++ - Mixing default member initializers and member initialization lists - bad idea?

This is partly a style question, partly a correctness question. Submit the following sample (a strip-down of a class which deals with a block of data that contains an embedded header): class Foo { public: Foo(size_t size) : scratch_(new…
Dana M
  • 198
  • 1
  • 11
3
votes
2 answers

Attempting to use vector's fill constructor in class member initialization fails. What is wrong?

When using std::vector's fill constructor (either form) with C++11's class member initialization feature, the following code fails to compile (under clang/llvm 3.6): #include class Foo { std::vector buf_(10); //compiler error! …
U007D
  • 5,772
  • 2
  • 38
  • 44
3
votes
3 answers

Patterns for resetting objects to initial state

Suppose I have an object with a member that is expensive to construct, and the need for a reset() function that resets it to its initial state: struct Example { // option 1: efficient, but need to duplicate initialization logic of reset() …
anderas
  • 5,744
  • 30
  • 49
3
votes
1 answer

D - Set default value for a struct member which is a multidimensional static array

I am using the D programming language. I want to have a struct containing a multidimensional static array of ints initially filled with a non-zero value (in my case, zero is a valid entry, and I want to initially mark all entries as invalid). As it…
Gassa
  • 8,546
  • 3
  • 29
  • 49
2
votes
3 answers

Complex statements in the member initialization part?

I have this: struct myClass{ multiset > values ; myClass(const char param1, const char param2) : values(less()) { } } ; I need to initialize the values member with a different functor depending on the…
GetFree
  • 40,278
  • 18
  • 77
  • 104
2
votes
2 answers

Initialize a lambda function in the body of a class constructor

I can compile the following code without any problem (using gcc 11.1.0): #include template class K { Func f; public: K(Func _f): f{_f} {}; void do_thing(int x) {f(x);}; }; int main() { auto f = [](int x)…
MaPo
  • 613
  • 4
  • 9
2
votes
1 answer

Why is object initialization different inside a struct?

I have created a class Point, here is the corresponding hpp file. #ifndef POINT #define POINT class Point { protected: int x; int y; public: Point(int x = 10, int y = 10); void movePoint(int moveX, int moveY); void…
2
votes
1 answer

Initialising class members - default values or member-init-list?

I'm a bit confused about when and how to initialize members of a class in a constructor. If I understand things correctly, you can accomplish this by either including default values in the class definition, a constructor member initialiser list, or…
1 2
3
8 9