Questions tagged [covariant-return-types]
13 questions
1
votes
1
answer
98
Views
How do C++ containers work together with the Liskov Substitution Principle?
For return types Covariance of the return values are required by the Liskov Substitiution Principle.
Lets assume the small type hierarchy for return types:
class B {};
class D : public B {};
The working classes then could have
class Base {
virtual B& func();
};
class Derived : public Base {
virtual...
0
votes
1
answer
30
Views
Covariant return type with primitive types
I have the following:
template
class AbsContainer {
public:
virtual T operator[](ptrdiff_t) = 0;
};
template
class specialContainer : public AbsContainer, Box {
class Proxy;
public:
Proxy operator[](ptrdiff_t i) {
return Proxy(i, this);
};
};
template
class SpecialContainer::Proxy {
ptrdiff_t _i;...
17
votes
2
answer
5.6k
Views
Why does Resharper say, “Co-variant array conversion from string[] to object[] can cause run-time exception on write operation” with this code? [duplicate]
This question already has an answer here:
Co-variant array conversion from x to y may cause run-time exception
7 answers
This code:
comboBoxMonth.Items.AddRange(UsageRptConstsAndUtils.months.ToArray());
public static List months = new List
{
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'...
3
votes
0
answer
159
Views
MSVC: Covariant Return Types And Virtual Inheritance
I'm unsure whether this is a bug in the visual-c++ compiler or undefined behaviour.
Setup
struct DummyBase { virtual ~DummyBase() = default; };
struct DummyDerived : virtual public DummyBase {};
Just a class and a derived class using virtual inheritance
DummyDerived derived;
DummyBase* base = &deriv...
1
votes
2
answer
515
Views
C++ Virtual Method return different derived types
Referencing this question: C++ virtual function return type
Let's consider the following set of objects.
class ReturnTypeBase
{
};
class ReturnTypeDerived1 : public ReturnTypeBase
{
public:
int x;
};
class ReturnTypeDerived2 : public ReturnTypeBase
{
public:
float y;
};
class Base
{
public:
virtual...
23
votes
8
answer
12.5k
Views
What's the best signature for clone() in C++?
As Scott Myers wrote, you can take advantage of a relaxation in C++'s type-system to declare clone() to return a pointer to the actual type being declared:
class Base
{
virtual Base* clone() const = 0;
};
class Derived : public Base
{
virtual Derived* clone() const
};
The compiler detects that clone...
3
votes
1
answer
40
Views
invalid covariant return type, nested class c++
I have a first class A that contain an iterator nested class with virtual methods:
template
class A {
public:
class iterator {
public:
virtual ~iterator() {};
virtual T& operator++();
};
virtual iterator begin() const = 0;
};
I have a second class B, that override virtuals methods:
template
class...
4
votes
1
answer
54
Views
Why this covariant return type declaration doesn't produce an unckecked warning?
Why doesn't the following code produce a compile-time unchecked warning:
class Parent {
public List method(){
return null;
}
}
class Child extends Parent {
public List method() {
return null;
}
}
while the following actually does:
class Parent {
public List method(){
return null;
}
}
class Child ext...
2
votes
2
answer
131
Views
C++ polymorphic cloning: How to obtain a Derived pointer from a Base pointer?
This is already several posts that I sent about this subjet and I apologize if this bothers some of you. After playing around several days and trying different alternatives (patterns) I came up with the following conclusions, which allows me to better formulate my question.
I have a vector of Base *...
2
votes
2
answer
168
Views
C++ : Covariant return type without pointer
I create two simple classes by inheritance, and I add a virtual function and the override in the child class.
class Parent
{
public:
virtual Parent foo();
};
class Child : public Parent
{
public:
Child foo() override;
};
In this case, my overridden function get an error : error C2555: 'Child::foo':...
2
votes
2
answer
113
Views
covariant return type for default method
I can override a method with covariant return type, but is it possible to override a default method with covariant return type? In the following example, I would like to override getFirstLeg without rewriting the default method, but Java does not allow that. I also do not want to make Animal a gener...
2
votes
1
answer
140
Views
C++ invalid conversion error using covariant return types with virtual functions
In the following code, I can assign the return of the D::clone() to a pointer to B, but not a pointer to D. Is is possible to return the actual polymorphic type from a call of the base pointer?
struct B
{
virtual B * clone() { return this; }
};
struct D : B
{
D * clone()
{
std::cout
2
votes
2
answer
70
Views
Statically chosen function and virtual function
Recently I've seen this C++ standard paragraph (http://eel.is/c++draft/expr.post#expr.call-5):
If the postfix-expression designates a destructor, the type of the function call expression is void; otherwise, the type of the function call expression is the return type of the statically chosen function...