views:

33

answers:

3
class B{
  private:
    int a;
}
class D: public B{
  private:
    int b;
}

B* b = new B;

Now for some reason I want turn b into a D* Type of Object. e.g. retain the information of B and become D with extra Informations required.

What I am currently thinking of is. static_cast to do the upcasting. the additional attributes will be set to null or garbage. then assign the additional attributes manually. But this leads to a dangling pointer risk. If the copy constructor is not coded with enough care.

So what else could be the proper Solution ?
and is there any solution of the Puzzle if we think it from a PHP perspective ?

A: 

If you do a static_cast<> then the additional attributes will not be null or garbage, they will actually be outside the memory allocated for the object. Use dynamic_cast<>, which will properly fail if the variable does not contain the correct type.

Also, the similarity between C++ and PHP in this case is merely superficial; do not use it to gain further "insight" into the C++ code.

Ignacio Vazquez-Abrams
Sorry I couldn't understand what you meant by implode. would `dynamic_cast<>` make additional properties null or garbage ? e.g. allocate memory for it ?
Ya I know But I've to make the architecture such that i runs both as CGI on C++ as well as on PHP
If used on a pointer to an inappropriate type, `dynamic_cast<>` will return `NULL`. There is no sane way to give space for a `D` if you've only created a new `B`. Either create a new `D` in the first place, or live with the `B` you have.
Ignacio Vazquez-Abrams
Then There exists no solution in this casting way. so should I make a `template<typename From, typename To>class Adapter` Template ? or anything similer ? that calls some method of `To` Given `From` to Adapt ? or there exist an well established idiom for that ?
Write a `D` constructor that takes a `B`, and properly copies the attributes you care about.
Ignacio Vazquez-Abrams
Ya Thats a simple way but I need to take care of all the attributes. everytime I change B I need to change D's copy constructor.
*shrug* No one said it would be easy. Or pleasant. If what you need is a dynamic language, then you should be using a dynamic language.
Ignacio Vazquez-Abrams
+1 Hmm I I hate labour. I thought It would be done with a little mind tricks.
Oli Charlesworth
A: 

b points to an object of type B. It can't become a D. If you want a D, you have to allocate one and copy the fields from *b.

Never think of things from a PHP perspective unless you're writing something in PHP. Never write anything in PHP.

Steve M
Its kind of wired to model C++ compromised for PHP. But the thing is If it has a solution with C++ then and only then it has a solution in any other language. and if it has no solution in C++ it has no solution in anything else.
It *does have* a solution in C++. It just doesn't have a naive solution as you've given.
Ignacio Vazquez-Abrams
A: 

Reconstructing a base object to a Derived object is generally not possible. First of all, The allocation only allocated enought space for B and not for D. If you allocated enought space for a D to create the B object (which you currently didn't, and is a bit hard to do anyway), then you could use hacks to 'reconstruct' a object like this :

// EXTREME HACK, BE CAREFUL, AND USE WITH CARE
// IF YOU DON'T UNDERSTAND IT, DON'T USE IT.
// you should have allocated (sizeof (D)) space to your b pointer.
D* d_object = new (b) D(B(*b));
// you should consider any previous pointer pointing toward the old B* as dangling.
BatchyX
Whilst this may work, it would be easier, clearer and less error-prone just to do `D *d = new D(*b);`.
Oli Charlesworth
You need to have a copy constructor on `D` that takes `B`
i figured that it has to be there for the sake of completeness. Might be useful to someone else ending there from google
BatchyX