Constructor call to pure virtual class is deleted
up vote
-3
down vote
favorite
I'm trying to make a UI, and I want objects to inherit properties. The way I do this is I have a templated class Widget
which takes a variable amount of types that it inherits from (see code below).
However, when I try to make a class that inherits from Widget<Resizeable>
I get an error: The default constructor of Widget<Resizeable>
is deleted.
Could someone explain why this happens? Widget
and Resizeable
are both abstract, so why is the constructor being called?
Widget:
template<typename... B>
class Widget : public B...
{
public:
virtual void Move(Vector position) = 0;
virtual void Show(bool show) = 0;
template<typename T>
bool HasBehavior()
{
return std::is_base_of<T, std::remove_reference<decltype(*this)>::type>::value;
}
protected:
// protected members
};
Resizeable:
class Resizeble
{
public:
virtual void Resize(Vector size) = 0;
Vector Size() const noexcept
{
return size_;
}
protected:
Vector size_;
};
Rectangle:
class Rectangle : public Widget<Resizeble>
{
public:
Rectangle(Vector position, Vector size, Color color) : color_(color)
{
}
private:
Color color_;
};
SOLUTION:
Implement constructors for all base classes, and use virtual inheritance so that the most derived class (in this case, Rectangle
) can call the constructors of all parent classes.
c++ class visual-c++ constructor virtual
|
show 3 more comments
up vote
-3
down vote
favorite
I'm trying to make a UI, and I want objects to inherit properties. The way I do this is I have a templated class Widget
which takes a variable amount of types that it inherits from (see code below).
However, when I try to make a class that inherits from Widget<Resizeable>
I get an error: The default constructor of Widget<Resizeable>
is deleted.
Could someone explain why this happens? Widget
and Resizeable
are both abstract, so why is the constructor being called?
Widget:
template<typename... B>
class Widget : public B...
{
public:
virtual void Move(Vector position) = 0;
virtual void Show(bool show) = 0;
template<typename T>
bool HasBehavior()
{
return std::is_base_of<T, std::remove_reference<decltype(*this)>::type>::value;
}
protected:
// protected members
};
Resizeable:
class Resizeble
{
public:
virtual void Resize(Vector size) = 0;
Vector Size() const noexcept
{
return size_;
}
protected:
Vector size_;
};
Rectangle:
class Rectangle : public Widget<Resizeble>
{
public:
Rectangle(Vector position, Vector size, Color color) : color_(color)
{
}
private:
Color color_;
};
SOLUTION:
Implement constructors for all base classes, and use virtual inheritance so that the most derived class (in this case, Rectangle
) can call the constructors of all parent classes.
c++ class visual-c++ constructor virtual
1
Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
– Some programmer dude
Nov 15 at 1:47
2
If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
– Paul Sanders
Nov 15 at 1:54
1
What'sVector
? Does it have a default constructor?
– songyuanyao
Nov 15 at 1:58
1
The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What isVector
?
– AnT
Nov 15 at 2:01
2
Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
– Pete Becker
Nov 15 at 2:21
|
show 3 more comments
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I'm trying to make a UI, and I want objects to inherit properties. The way I do this is I have a templated class Widget
which takes a variable amount of types that it inherits from (see code below).
However, when I try to make a class that inherits from Widget<Resizeable>
I get an error: The default constructor of Widget<Resizeable>
is deleted.
Could someone explain why this happens? Widget
and Resizeable
are both abstract, so why is the constructor being called?
Widget:
template<typename... B>
class Widget : public B...
{
public:
virtual void Move(Vector position) = 0;
virtual void Show(bool show) = 0;
template<typename T>
bool HasBehavior()
{
return std::is_base_of<T, std::remove_reference<decltype(*this)>::type>::value;
}
protected:
// protected members
};
Resizeable:
class Resizeble
{
public:
virtual void Resize(Vector size) = 0;
Vector Size() const noexcept
{
return size_;
}
protected:
Vector size_;
};
Rectangle:
class Rectangle : public Widget<Resizeble>
{
public:
Rectangle(Vector position, Vector size, Color color) : color_(color)
{
}
private:
Color color_;
};
SOLUTION:
Implement constructors for all base classes, and use virtual inheritance so that the most derived class (in this case, Rectangle
) can call the constructors of all parent classes.
c++ class visual-c++ constructor virtual
I'm trying to make a UI, and I want objects to inherit properties. The way I do this is I have a templated class Widget
which takes a variable amount of types that it inherits from (see code below).
However, when I try to make a class that inherits from Widget<Resizeable>
I get an error: The default constructor of Widget<Resizeable>
is deleted.
Could someone explain why this happens? Widget
and Resizeable
are both abstract, so why is the constructor being called?
Widget:
template<typename... B>
class Widget : public B...
{
public:
virtual void Move(Vector position) = 0;
virtual void Show(bool show) = 0;
template<typename T>
bool HasBehavior()
{
return std::is_base_of<T, std::remove_reference<decltype(*this)>::type>::value;
}
protected:
// protected members
};
Resizeable:
class Resizeble
{
public:
virtual void Resize(Vector size) = 0;
Vector Size() const noexcept
{
return size_;
}
protected:
Vector size_;
};
Rectangle:
class Rectangle : public Widget<Resizeble>
{
public:
Rectangle(Vector position, Vector size, Color color) : color_(color)
{
}
private:
Color color_;
};
SOLUTION:
Implement constructors for all base classes, and use virtual inheritance so that the most derived class (in this case, Rectangle
) can call the constructors of all parent classes.
c++ class visual-c++ constructor virtual
c++ class visual-c++ constructor virtual
edited Nov 15 at 9:12
TrebuchetMS
1,6071618
1,6071618
asked Nov 15 at 1:43
Krystian S
1719
1719
1
Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
– Some programmer dude
Nov 15 at 1:47
2
If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
– Paul Sanders
Nov 15 at 1:54
1
What'sVector
? Does it have a default constructor?
– songyuanyao
Nov 15 at 1:58
1
The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What isVector
?
– AnT
Nov 15 at 2:01
2
Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
– Pete Becker
Nov 15 at 2:21
|
show 3 more comments
1
Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
– Some programmer dude
Nov 15 at 1:47
2
If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
– Paul Sanders
Nov 15 at 1:54
1
What'sVector
? Does it have a default constructor?
– songyuanyao
Nov 15 at 1:58
1
The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What isVector
?
– AnT
Nov 15 at 2:01
2
Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
– Pete Becker
Nov 15 at 2:21
1
1
Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
– Some programmer dude
Nov 15 at 1:47
Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
– Some programmer dude
Nov 15 at 1:47
2
2
If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
– Paul Sanders
Nov 15 at 1:54
If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
– Paul Sanders
Nov 15 at 1:54
1
1
What's
Vector
? Does it have a default constructor?– songyuanyao
Nov 15 at 1:58
What's
Vector
? Does it have a default constructor?– songyuanyao
Nov 15 at 1:58
1
1
The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What is
Vector
?– AnT
Nov 15 at 2:01
The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What is
Vector
?– AnT
Nov 15 at 2:01
2
2
Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
– Pete Becker
Nov 15 at 2:21
Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
– Pete Becker
Nov 15 at 2:21
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311277%2fconstructor-call-to-pure-virtual-class-is-deleted%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Base class constructors always needs to be called, how would the base classes otherwise construct (and possibly initialize) their member variables? Besides, if the problem was related to the "abstractness" of the classes, the error would probably be different. And please try to create a Minimal, Complete, and Verifiable Example to show us, and include the full and complete copy-paste of the compiler output.
– Some programmer dude
Nov 15 at 1:47
2
If I hack the code provided to make it compile (!), I can't reproduce this problem. So, Minimal, Complete, and Verifiable example please. It's certainly nothing to do with abstract classes.
– Paul Sanders
Nov 15 at 1:54
1
What's
Vector
? Does it have a default constructor?– songyuanyao
Nov 15 at 1:58
1
The factors that forced the compiler to delete the default constructor are the factors that will also interfere with a naive manual implementation of that constructor. Judging by your current confusion, these factors will likely be just as surprising for you in that context as well. You are not providing enough information for us to figure out what these factors are. What is
Vector
?– AnT
Nov 15 at 2:01
2
Is that the entire error message? Usually the compiler produces a longer explanation of why that constructor is deleted.
– Pete Becker
Nov 15 at 2:21