Questions tagged [inherited-constructors]
22 questions
9
votes
1 answer
Inheriting constructors and brace-or-equal initializers
I don't understand why you can't compile a class which has both a member (not default constructible) with a brace-or-equal initializer and an inherited constructor. g++ says :
test.cpp:22:15: error: use of deleted function…

maxbc
- 949
- 1
- 8
- 18
8
votes
2 answers
Inherited constructors, default constructor and visibility
As stated by [namespace.udecl]/18:
[...] A using-declaration that names a constructor does not create a synonym; instead, the additional constructors are accessible if they would be accessible when used to construct an object of the corresponding…

skypjack
- 49,335
- 19
- 95
- 187
6
votes
2 answers
Can copy/move constructors be inherited by using-declaration in c++17?
struct B {
B(int) {}
B(B const&) {}
};
struct D: B {
using B::B;
};
int main(void) {
B b(5);
D d(b); // error
return 0;
}
c++14 explicitly excludes copy/move constructors from inherited constructors in 12.9 [class.inhctor]/p3.
For…

wanghan02
- 1,227
- 7
- 14
6
votes
2 answers
Should args to inherited constructors be copied when invoking the base ctor or not?
For the following program:
#include
struct Foo
{
Foo() { std::cout << "Foo()\n"; }
Foo(const Foo&) { std::cout << "Foo(const Foo&)\n"; }
~Foo() { std::cout << "~Foo()\n"; }
};
struct A
{
A(Foo) {}
};
struct B : A
{
…

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055
6
votes
3 answers
Need an example showing that default constructor is not inherited
I know that default constructor is not inherited, as stated in n3337.
And there is an example there:
struct B2 {
B2(int = 13, int = 42);
};
struct D2 : B2 {
using B2::B2;
};
With quite good explanation:
The candidate set of inherited…

PiotrNycz
- 23,099
- 7
- 66
- 112
5
votes
4 answers
c++0x inherited constructor in templates
Here is class foo:
template
struct foo
{
foo()
{
t = nullptr;
}
foo(T* p, bool flag)
{
t = p;
}
private:
T* t;
};
Here is class bar:
template
struct bar: public foo
{
…

Jarlaxle
- 871
- 10
- 18
3
votes
4 answers
How to declare constructors in base classes so that sub-classes can use them without declaring them?
I want a subclass to use its parent's constructors. But it seems I always need to define them again in the subclass in order for that to work, like so:
public SubClass(int x, int y) : base (x, y) {
//no code here
}
So I'm wondering if I'm not…

Alessandro Ituarte
- 95
- 1
- 7
3
votes
1 answer
What are the access specifiers of inherited (-> "using") base class ctors / operators in derived class?
In the following code you can see that I'm inheriting the base class ctors into the derived class under the "private" access specifier. My initial thought would be that these would adapt to the access specifiers that I have given (here "private")…

glades
- 3,778
- 1
- 12
- 34
3
votes
0 answers
Constructor with default arguments can not be called when inherited
Please take a look at the code:
struct X {};
struct Foo {
Foo (int n = {}, int p = {}) {}
};
struct Boo : Foo {
using Foo::Foo;
Boo (X n) {}
};
And usage:
Boo boo1;
Error message (GCC 5.2.1):
error: no matching function for call to…

Artur Pyszczuk
- 1,920
- 1
- 16
- 23
2
votes
2 answers
How do we call Base class Constructor from Derived Class using delegating Constructors
I know and I have read many threads to call a base class constructor from a Derived class, But I wanted to implement it using Delegating constructors
Here is my code
The error states
"A delegating constructor cannot have other…

Shivang Mathur
- 49
- 5
2
votes
1 answer
clang doesn't see base class constructors pulled in via typedef
The following code
#include
#include
template
struct V : public std::vector
{
using Impl = std::vector;
using typename Impl::vector; // the constructors
};
int main()
{
std::string empty;
…

Bulletmagnet
- 5,665
- 2
- 26
- 56
2
votes
2 answers
Overloading inherited constructors
Given the following base class:
class Base {
int a, b;
public:
Base(int a, int b=42): a(a), b(b) { }
};
And a class that is derived from base:
class Derived: public Base {
using Base::Base; // inherit Base constructors
bool…

smac89
- 39,374
- 15
- 132
- 179
2
votes
1 answer
Inherited constructor and stl containers error
I am playing around with inherited constructors, however I have troubles understanding why gcc complains when I try to inherit from std::string.
I know it's not best practice and it should be avoided at all costs, so before shouting at me for that,…

dau_sama
- 4,247
- 2
- 23
- 30
2
votes
1 answer
In what ways does C++ inheritance order affect a constructor?
If I define a structure that inherits from multiple other structures, how does the order that I list them in affect something like this:
struct D: C,B{
D(): B(), C(){...}
};
Simple question, but thanks in advance!

Wes Field
- 3,291
- 6
- 23
- 26
1
vote
2 answers
Printing an attribute of a instance in a list, only if it exist
I want to print a class attribute, part of a list of instances, only if this attribute exist
i created a class name Guitarist, that includes a name attribute, and otherItems (which usually is a dictionary for the user to add additional data such as…

Yoni Tzafrir
- 53
- 5