how to mimic derived to base pointer auto cast? - c++

I have a Base class and a Derived class. Both have their own non-static data member and both have to have standard layout. So I can't inherit Derived from Base and have to declare then separately. I know I can define an operator Base& in the Derived class so that Base& base = derived works. Is there any way to make Base* pBase = pDerived works too (without an explicit cast)?

You can use a proxy object to represent a pointer:
struct Base;
struct Derived;
struct Ptr
{
Ptr(void* ptr) : ptr(ptr) { }
operator Base*() { return reinterpret_cast<Base*>(ptr); }
operator Derived*() { return reinterpret_cast<Derived*>(ptr); }
private:
void* ptr;
};
struct Base
{
Ptr operator&() { return Ptr(this); }
operator Derived&() { return *reinterpret_cast<Derived*>(this); }
};
struct Derived
{
Ptr operator&() { return Ptr(this); }
operator Base&() { return *reinterpret_cast<Base*>(this); }
};
void bar(Base*)
{ }
void foo()
{
Derived derived;
Base* base1 = &derived;
Base& base2 = derived;
bar(&derived);
}
This formal solution should be carefully analyzed for UB if used.

Related

Deep-copying a derived class when its base class has member pointers

I am trying to make a deep copy of the object d of class Derived as in the code below:
class A {
public:
int m_int;
A* clone() {
return new A(*this);
}
};
class Base {
public:
A* m_a;
virtual Base* clone() {
A* new_a = new A();
new_a = this->m_a->clone();
Base* clone = new Base(*this);
clone->m_a = new_a;
return clone;
}
};
class Derived : public Base {
public:
double m_dbl;
virtual Derived* clone() {
return new Derived(*this);
}
};
int main() {
Derived* d = new Derived();
d->m_dbl = 1.234;
A* a = new A();
a->m_int = -1;
d->m_a = a;
//clone d
Derived d_copy = d->clone();
//changing values of d's attributes must not affect d_copy
a->m_int = 10;
d->m_dbl = 3.456;
//check d_copy
cout << "m_int " << d_copy->m_a->m_int << endl;
cout << "m_dbl " << d_copy->m_dbl << endl;
}
output:
m_int 10 //wrong, should be -1;
m_dbl 1.234 //correct
As you can see, simply returning new Derived(*this) in Derived's clone() method is wrong since it does not deep-copy m_a.
If I "bring down" the deep-copying of m_a from Base to Derived then I will get the correct answer:
virtual Base* clone() = 0;
...
virtual Derived* clone() {
A* new_a = new A();
new_a = this->m_a->clone();
Derived* clone = new Derived(*this);
clone->m_a = new_a;
return new Derived(*this);
}
//gives out m_int = -1
If this is the case does this mean that each time I create further derived classes from Derived, I always have to "bring down" the contents of clone() to them?
Also, if for example Base has two derived classes Derived1 and Derived2, does this mean that I have to deep-copy the members of Base in each of them?
Any other ways to approach this?
You can consider of implementing the copy constructor that always is doing deep copy. In such case your clone implementation will always be trivial:
class Base {
public:
A* m_a;
Base(const A& other)
: m_a(other.m_a->clone())
{
}
virtual Base* clone() {
return new A(*this);
}
};
class Derived : public Base {
public:
double m_dbl;
Derived(const Derived& other)
: m_dbl(other.m_dbl)
virtual Derived* clone() {
return new Derived(*this);
}
};
If you don't want to change the behavior of the default constructor (you want the default constructor to make shallow copy for example) the alternative is to move copy implementation to a CopyTo method, so you can reuse it:
class Base {
public:
A* m_a;
static void CopyTo(const Base& from, Base& to)
{
to.m_a = from.m_a->clone();
}
virtual Base* clone() {
Base* result = new Base();
CopyTo(*this, result);
return result;
}
};
class Derived : public Base {
public:
double m_dbl;
static void CopyTo(const Base& from, Base& to)
{
Base::CopyTo(from, to);
to.m_dbl= from.m_dbl;
}
virtual Derived * clone() {
Derived * result = new Derived ();
CopyTo(*this, result);
return result;
}
virtual Derived* clone() {
return new Derived(*this);
}
};

C++: unique_ptr polymorphism for template classes derived from a non-template base class

I have the following class:
class Base {
public:
Base() = default;
virtual ~Base() {};
}
And, let's say I have a unique_ptr to this class, aka:
using BasePtr = std::unique_ptr<Base>;
Now, let's assume I have a template class that inherits from the base class.
template <typename T>
class Derived : public Base {
public:
Derived() = default;
Derived(const T x) : some_variable(x) {};
~Derived() override {};
void hello() { std::cout << some_variable << std::endl; }
private:
T some_variable;
}
For arguments sake, let's say I have a factory method that creates a unique_ptr to some new instance, such as:
template <typename T>
auto make_class(const T& x) -> BasePtr {
return std::unique_ptr<Derived<T> >(new Derived<T>(x));
}
If I try to build this:
int main() {
auto ptr = make_class<int>(5);
if (ptr) {
ptr->hello();
}
return 0;
}
With C++11, this results in a compile error (saying that Base does not have a hello() method), because it seems that the actual instance stored in the unique_ptr is a Base, not a Derived.
Based on my understanding (at least if Derived wasn't templated), this should not be an issue. What's happening here?
You function make_class returns a BasePtr:
auto make_class(const T& x) -> BasePtr
Then in your main function you say:
auto ptr = make_class(5);
that is, ptr is a BasePtr. The function cannot know that the pointer actually points to a derived class. For this reason, there is no hello() function that could be invoked.

C++ virtual function return type with shared pointers [duplicate]

Why do you need (in order to make it compile) the intermediate CloneImplementation and std::static_pointer_cast (see Section 3 below) to use the Clone pattern for std::shared_ptr instead of something closer (see Section 2 below) to the use of raw pointers (see Section 1 below)? Because as far as I understand, std::shared_ptr has a generalized copy constructor and a generalized assignment operator?
1. Clone pattern with raw pointers:
#include <iostream>
struct Base {
virtual Base *Clone() const {
std::cout << "Base::Clone\n";
return new Base(*this);
}
};
struct Derived : public Base {
virtual Derived *Clone() const override {
std::cout << "Derived::Clone\n";
return new Derived(*this);
}
};
int main() {
Base *b = new Derived;
b->Clone();
}
2. Clone pattern with shared pointers (naive attempt):
#include <iostream>
#include <memory>
struct Base {
virtual std::shared_ptr< Base > Clone() const {
std::cout << "Base::Clone\n";
return std::shared_ptr< Base >(new Base(*this));
}
};
struct Derived : public Base {
virtual std::shared_ptr< Derived > Clone() const override {
std::cout << "Derived::Clone\n";
return std::shared_ptr< Derived >(new Derived(*this));
}
};
int main() {
Base *b = new Derived;
b->Clone();
}
Output:
error: invalid covariant return type for 'virtual std::shared_ptr<Derived> Derived::Clone() const'
error: overriding 'virtual std::shared_ptr<Base> Base::Clone() const'
3. Clone pattern with shared pointers:
#include <iostream>
#include <memory>
struct Base {
std::shared_ptr< Base > Clone() const {
std::cout << "Base::Clone\n";
return CloneImplementation();
}
private:
virtual std::shared_ptr< Base > CloneImplementation() const {
std::cout << "Base::CloneImplementation\n";
return std::shared_ptr< Base >(new Base(*this));
}
};
struct Derived : public Base {
std::shared_ptr< Derived > Clone() const {
std::cout << "Derived::Clone\n";
return std::static_pointer_cast< Derived >(CloneImplementation());
}
private:
virtual std::shared_ptr< Base > CloneImplementation() const override {
std::cout << "Derived::CloneImplementation\n";
return std::shared_ptr< Derived >(new Derived(*this));
}
};
int main() {
Base *b = new Derived;
b->Clone();
}
The general rule in C++ is that the overriding function must have the same signature as the function it overrides. The only difference is that covariance is allowed on pointers and references: if the inherited function returns A* or A&, the overrider can return B* or B& respectively, as long as A is a base class of B. This rule is what allows Section 1 to work.
On the other hand, std::shared_ptr<Derived> and std::shared_ptr<Base> are two totally distinct types with no inheritance relationship between them. It's therefore not possible to return one instead of the other from an overrider. Section 2 is conceptually the same as trying to override virtual int f() with std::string f() override.
That's why some extra mechanism is needed to make smart pointers behave covariantly. What you've shown as Section 3 is one such possible mechanism. It's the most general one, but in some cases, alternatives also exist. For example this:
struct Base {
std::shared_ptr< Base > Clone() const {
std::cout << "Base::Clone\n";
return std::shared_ptr< Base >(CloneImplementation());
}
private:
virtual Base* CloneImplementation() const {
return new Base(*this);
}
};
struct Derived : public Base {
std::shared_ptr< Derived > Clone() const {
std::cout << "Derived::Clone\n";
return std::shared_ptr< Derived >(CloneImplementation());
}
private:
virtual Derived* CloneImplementation() const override {
std::cout << "Derived::CloneImplementation\n";
return new Derived(*this);
}
};

How to invoke a call to a not overridden derived class function?

class Base {
virtual void func1();
}
class Derived : Base {
void func1();
void func2();
}
vector<Base *> vec;
vec.push_back(new Base()), vec.push_back(new Derived());
What is the correct/clean way to call func2 without knowing which index corresponds to which class? Is there a convention to do such a thing? I also want to avoid using typeid.
In your case the objects are sliced, as mentioned in king_nak's answer, so there's no safe way to call func2().
But you can store pointers to Base instead of Base objects - in this case you can use dynamic_cast:
std::vector<Base*> vec;
vec.push_back(new Base());
vec.push_back(new Derived());
for (auto obj : vec)
{
Derived* d = dynamic_cast<Derived*>(obj);
if (d)
{
d->func2();
}
}
Some info on dynamic_cast: link
PS: Also, if you want to call function func2() on Base objects, I think it makes sense to add a stupid implementation to Base class and make the function virtual.
This function will take one of said pointers and call func2 if possible, and simply return false otherwise
bool CallFunc2(Base* Bae){
Derived* Der;
if (Der = dynamic_cast<Derived*>(Bae))
{Der->func2(); return true;}
else
return false;
}
This works on the principle that dynamic_cast returns a null pointer if the object being cast cannot be converted.
If you don't want to use RTTI at all (including dynamic_cast), you could simulate its behaviour like Qt does it with qgraphicsitem_cast
Outline:
class Base {
public:
enum { Type = 0 };
virtual int type() { return Type; }
};
class Derived : public Base {
public:
enum { Type = 1 };
int type() { return Type; }
};
template<typename T>
inline T myobject_cast(Base *b) {
if (b) {
// Requires C++11
if (int(std::remove_pointer<T>::type::Type) == b->type()) {
return static_cast<T>(b);
}
/* Pre C++11 (might be UB, but works on many compilers, OpenSource and Commercial)
if (int(static_cast<T>(0)->Type) == b->type()) {
return static_cast<T>(b);
}
*/
}
return NULL;
}
// use:
Base *b = new Base;
Base *d = new Derived;
Derived *o1 = myobject_cast<Derived*> (b); // NULL
Derived *o2 = myobject_cast<Derived*> (d); // d
Each class would require a unique Type member for this to work.
Be aware that this will not work with "intermediate" classes in the hierarchy. Only the actual, most derived type will can be cast to (e.g. a DerivedDerived cannot be cast to a Derived, or Base for that matter).
You might also find an overload for const handy:
template<typename T> inline T myobject_cast(const Base *b)
{ return (b && int(static_cast<T>(0)->Type) == b->type()) ? static_cast<T>(p) : 0; }
const Base *cb = new Derived;
const Derived *co = myobject_cast<const Derived *>(cb);

create instance of unknown derived class in C++

let's say I have a pointer to some base class and I want to create a new instance of this object's derived class. How can I do this?
class Base
{
// virtual
};
class Derived : Base
{
// ...
};
void someFunction(Base *b)
{
Base *newInstance = new Derived(); // but here I don't know how I can get the Derived class type from *b
}
void test()
{
Derived *d = new Derived();
someFunction(d);
}
Cloning
struct Base {
virtual Base* clone() { return new Base(*this); }
};
struct Derived : Base {
virtual Base* clone() { return new Derived(*this); }
};
void someFunction(Base* b) {
Base* newInstance = b->clone();
}
int main() {
Derived* d = new Derived();
someFunction(d);
}
This is a pretty typical pattern.
Creating new objects
struct Base {
virtual Base* create_blank() { return new Base; }
};
struct Derived : Base {
virtual Base* create_blank() { return new Derived; }
};
void someFunction(Base* b) {
Base* newInstance = b->create_blank();
}
int main() {
Derived* d = new Derived();
someFunction(d);
}
Though I don't think that this a typical thing to do; it looks to me like a bit of a code smell. Are you sure that you need it?
It's called clone and you implement a virtual function that returns a pointer to a dynamically-allocated copy of the object.