0

I would like to know if there is a way to program a class conversion in c++ I'am not talking about any form of type_casting, but a clone and convert of an instance to another one.

If I have this code :

class BaseClass
{
    string baseData;
};

class DerivedClassB : public BaseClass
{
    int intData;
};

class DerivedClassA : public BaseClass
{
    float floatData;
};

lets say that I have an instance of DerivedClassA, and I would like to 'convert' that instance to DerivedClassB or even BaseClass i'm not concerned about the loss of the 'floatData', as long as the 'baseData' of the BaseClass is kept

can I achieve converssion from any type to any type ?

BaseClass -> DerivedClassA
DerivedClassA -> DerivedClassB
DerivedClassB -> BaseClass 

How would I have to implement such functionnality ?

Thanks very much for the enlightenment.

scimunk
  • 73
  • 1
  • 7
  • 1
    I think it is what you are looking for http://en.cppreference.com/w/cpp/language/cast_operator – Andrew Kashpur May 02 '17 at 08:43
  • The first is a dynamic cast and may fail if the object isn't an instance of `DerivedClassA`. The second is a type conversion and you should create a constructor or type-cast operator. The third is a static cast because all instances of `DerivedClassB` are by definition instances of `BaseClass`. You need a tutorial on ''inheritance' and 'type-cast operator' in C++. Stackoverflow isn't the place for such broad questions. – Persixty May 02 '17 at 08:46
  • I though people wouldn't understand what I really ask ... i know about type_cast and inheritance, i'm talking about actuall conversion like this : BaseClass *baseC = new BaseClass(); DerivedClassA *derA = new DerivedClassA(baseC); DereivedClassB *derB = new DerivedClassB(derA); BaseClass *baseC2 = new BaseClass(DerB); in a more fashionable manner. – scimunk May 02 '17 at 08:50

1 Answers1

1

Can I achieve conversion from any type to any type ?

Well, Yes, that's the purpose of reinterpret_cast right?. But you'll most likely invoke Undefined Behavior if you break strict aliasing rules


How would I have to implement such functionnality ?

For your use case, you may simply provide a Converting Constructor.

//Forward declaration
class BaseClass;
class DerivedClassB;
class DerivedClassA;

class BaseClass
{
    //Converting constructor: DerivedClassB -> BaseClass 
    BaseClass(const DerivedClassB&);

    BaseClass() = default;
    string baseData;
};

class DerivedClassB : public BaseClass
{
    //Converting constructor: DerivedClassA -> DerivedClassB
    DerivedClassB(const DerivedClassA&);

    DerivedClassB() = default;
    int intData;
};

class DerivedClassA : public BaseClass
{
    //Converting constructor: BaseClass -> DerivedClassA
    DerivedClassA(const BaseClass&);

    DerivedClassA() = default;
    float floatData;
};

// --------------------------

//Implement them out of class
Community
  • 1
  • 1
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
  • Thanks you, this is more close to what I'm looking for, but what if I have hundred of subclass, and if I want to be able to Convert them in any other subclass no matter what is it ? creating a constructor for each become kind of impracticable. note that I don't care about the loss of data in the subclass as long as I can keep the data in the BaseClass. – scimunk May 02 '17 at 09:08
  • @epixerion How about first casting them to the super class and then casting them to the new sub class? Only requires one constructor or cast operator each. That said, this is obviously code smell. Can you give us your context? Can you instead go by not using inheritance at all, making the former super class that contains the relevant data simply a member and then you go like `OldClass old_object; NewClass new_object(old_object.get_former_super_class())`? Would be far more clean. – Aziuth May 02 '17 at 09:15
  • I'll try give a context : I'm trying to do a node editor, each node is an instance of a baseClass called `Node` and I have this special node called `Graph` (which subclass `Node`) which containt a certain amount of node, now I have Different type of graph : `Symmetry,ForEach,For,Etc` which all subClass `Graph` and affect the node inside differently, if the user want to change the type of graph, what I just need to do is Convert that class to the target graph, and all I need is the data contained in the `Graph` class, the data being in the `Symmetry,ForEach,For` subclass are now obselette. – scimunk May 02 '17 at 09:25
  • @epixerion Yeah, sounds like what I recommended, go with a member variable that stores this data. And you talked about copying? Don't copy. Simply move that member. That said, your names sound wrong. We are talking about a network graph, right? Then Node is hardly a super class of that. Doing that would mean that "a graph is a node", instead of the reality being "a graph consists of nodes". Also, may I ask what those subtypes represent? Never heard of a ForEach graph. By the way, if you answer me, do the @, if not, I will only notice your comment by chance. An @ results in a notification. – Aziuth May 02 '17 at 10:01
  • @Aziuth I understand what you mean, I have a particular kind of graph, where you stack graph on top of each other, which is why a graph is also a node, you can basically put a graph in a graph in a graph ..Etc, that said, I have special kind of subGraph like the forEach one, which will repeat his content X amount of time defined by the parameter on himself as a node, if you are curious about it can show you in mp. unless there is something I'm missing, I must subGraph `Graph`in order to all my engine to work. – scimunk May 02 '17 at 10:51