Questions tagged [delegating-constructor]

A delegating constructor is a C++11 feature that allows a class constructor to delegate to another of its constructors in its initializer-list, much like it can call base class constructors.

24 questions
36
votes
3 answers

How to handle constructors that must acquire multiple resources in an exception safe manner

I've got a non-trivial type that owns multiple resources. How do I construct it in an exception safe manner? For example, here is a demo class X that holds an array of A: #include "A.h" class X { unsigned size_ = 0; A* data_ =…
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
11
votes
4 answers

Do C++11 delegated ctors perform worse than C++03 ctors calling init functions?

[this question has been highly edited; pardon that, I've moved the edits into an answer below] From Wikipedia (subarticle included) on C++11: This [new delegating constructors feature] comes with a caveat: C++03 considers an object to be…
okovko
  • 1,851
  • 14
  • 27
10
votes
2 answers

Delegating constructor gives segmentation fault when using class field for argument

Actually the segmentation fault happens in another program I tried to compile which happens because of this behaviour. My question is: This is a bug or my fault? Reproducible in any way (even if the something field is private or protected) and…
Memos Electron
  • 660
  • 5
  • 26
8
votes
2 answers

Crash in constructor with using virtual inheritance and delegating constructors

struct D { virtual void m() const = 0; }; struct D1 : public virtual D { }; struct D2 : public virtual D { }; struct B : public D2 { B() { } B(int val) : B() { } void m() const { } }; struct A : public B, public D1 { A() :…
8
votes
1 answer

MSVC direct constructor call extension

In this response, tloveless pointed out that it's possible in MSVC to use this->foo::foo(42); for constructor delegation to directly call a constructor: #include struct foo { int m; foo(int p) : m(p) { std::cout <<…
dyp
  • 38,334
  • 13
  • 112
  • 177
7
votes
4 answers

How to forward overloaded constructor call to another constructor in C++/CLI

I know that there is no way to do this in pure C++, but I was wondering if it is possible to call a constructor from another constructor's initialization list in C++/CLI, same way one can do it in C#. Example: ref class Foo { Foo() {} Foo(int i)…
Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
6
votes
2 answers

Delegating copy constructor and const data initialization

I have a class A with lots of data members, some of which are constant. All data members have proper copy constructors, so I want to default a copy constructor of my class: class A { public: A() : a(1) {} A(const A& op) =…
Sergey
  • 7,985
  • 4
  • 48
  • 80
5
votes
3 answers

C++11 constructor delegation with aggregate initialization

Is it possible to invoke aggregate initialization within my own definition of the default ctor? GCC complains "error: constructor delegates to itself" with the below code: struct X { int x, y, z, p, q, r; X(): x{}, y{}, z{}, p{}, q{}, r{} { } …
5
votes
2 answers

argument's default value base on others

i want to achieve a class like this: class A{ int a; int b; int c; A():a(),b(),c(){}; A(int ia,int ib,int ic=ia+ib):a(ia),b(ib),c(ic){}; //this is what i need }; I want the ic's default value is caculated based on the ia and…
4
votes
1 answer

constructor delegates to itself in g++ & clang++

Consider following program. I accidentally made a mistake in it. struct T { int s; T() : T() { s=9; } }; int main() { T t; } The above code compiles & runs fine in some versions of g++ like g++ 4.8.1 (See live demo here ) &…
Destructor
  • 14,123
  • 11
  • 61
  • 126
3
votes
1 answer

C++ delegating constructor with initialization list. Which inits happen?

I've looked at a very similar question, but I'm not quite sure I understand the answer. If I delegate a constructor, which initializations from initialization lists occur? Example: MyClass::MyClass(int a, int b) : MyClass(a, b,…
turbulencetoo
  • 3,447
  • 1
  • 27
  • 50
2
votes
1 answer

C++ : can I do some processing before calling another constructor?

I have a class with two constructors. class Foo { Foo(B b) {... } Foo(int n) : Foo(buildBFromInt(n)) {} ?? } The first takes some object and I would like to have a second one that first creates the object from a simpler type. Is this possible…
ElefEnt
  • 2,027
  • 1
  • 16
  • 20
2
votes
3 answers

re-initialise using delegating constructor

I have this class X, using delegating constructor I want to change only the value i and j as 0. Is it possible to do so? class X{ public: X() = default; X(int ival, int jval) : i{ ival }, j{ jval } {} X(int new_i) : i{ new_i }, X(i,…
abhimanyuaryan
  • 3,882
  • 5
  • 41
  • 83
1
vote
1 answer

C++ Error checking before constructor delegation

I have a constructor that creates a matrix from size_t dimensions, I want to add support for int dimensions. However, before passing the ints to the size_t constructor I would like to make sure that they are positive. Matrix(vector vals,…
NoamV
  • 65
  • 5
1
vote
2 answers

Why partially initializing a class and then invoking delegating ctor fails?

Following code does not initialize struct string members to same value. #include #include struct S { std::string s1; std::string s2; S(std::string const& s) : s1{s}{} S(int i) : S([&]{ s2 =…
user1832484
  • 371
  • 3
  • 8
1
2