How to change an object's class to a subclass of its current class in C ++?

I have an array of pointers to a base class, so I can get these pointers to indicate (different) subclasses of the base class, but still interact with them. (really, just a few methods that I made virtual and overloaded). I am wondering if I can avoid using pointers, and instead just create an array of a base class, but somehow set the class to a subclass of my Choice. I know that there must be something where the class is indicated, since it should use this to find the function pointer for virtual methods. By the way, all subclasses have the same ivars and layout.

Note: the design is actually based on using the template argument instead of the variable, due to increased performance, so the abstract base class is only an interface for subclasses that are all the same except for their compiled code.

thank

EDIT: ALL KNOWNS (and, if necessary) THE BASIC CLASS HAS THE SAME SITE / SIZE

Otherwise, a strategy strategy would be good, but it adds a pointer to the class, and what I'm trying to avoid is definitely one respect.

In simpler terms, I'm trying to do

class base {
int ivars[100];
public:
virtual char method() = 0;
}

template <char c>
class subclass : base {
public:
  char method() {
    return c;
  }
}

base things[10];
some_special_cast_thingy(things[2], subclass);
printf("%c", things[2].method());

obviously this is a lot more complicated, but as for the classes / things that I like, everything I'm looking for. This is probably a language feature, by the way.

+3
source share
5

, , . , . ( ) . , , :

class Base
{
public:
  int A;
  int B;
}

class ChildOne : Base
{
public:
  int C;
}

class ChildTwo : Base
{
public:
  double C;
}

Base[10] ( 32- *) 8 : , 4- int. , ChildOne 8 , 4 C. ChildTwo 8 , 8 double C. , 8- Base, .

, , - ( 4 32- ), , . Base ChildTwo, , .

dynamic_cast Base* ChildTwo*, .

( ), :

class Data
{
public:
  int A;
  int B;

  Data(HandlerBase* myHandler);
  int DoSomething() { return myHandler->DoSomething(this) }
protected:
  HandlerBase* myHandler;
}

class HandlerBase
{
public:
  virtual int DoSomething(Data* obj) = 0;
}

class ChildHandler : HandlerBase
{
public:
  virtual int DoSomething(Data* obj) { return obj->A; }
}

, DoSomething , ( ChildHandler), ( ). , , , - . Data , , .

, , , - .

* nitpickers: , , , , vtable, . .

II: . , , . Mea culpa, , .

- - , , . , - "", , - .

: , , , . , , , , , , . , .

, (, - ?), , . , , , , - , , , , - reinterpret_cast .

class Base
{
public:
  int A;
  int B;

  void DoSomething();
}

class Derived : Base
{
  void DoSomething();
}

void DangerousGames()
{
  // create an array of ten default-constructed Base on the stack
  Base items[10];
  // force the compiler to treat the bits of items[5] as a Derived,
  // and make a ref
  Derived& childItem = reinterpret_cast<Derived>(items[5]);
  // invoke Derived::DoSomething() using the data bits of items[5], 
  // since it has an identical layout
  childItem.DoSomething();
}

, reinterpret_cast , , , , : " , , , , , , ". " " , -, Base Derived, , , , , undefined. , . , , , , .

+1

, . , , . , .

+2

.

. , , . , , , .

class Base;

class Data{
  string name;
  int age;
  Base* base;
};

class Base{
  virtual void method1(Data&)=0;
  // other methods
};

class Subclass1: public Base{
  void method1(Data& data){ data.age=0; }
};

vector<Data> dataVector;
Subclass1 subclass1;
Data* someData = ...
someData->base=&subclass1;

someData->base->method1(*someData);
+1

you can use an array of pointers. If you need access to parts of the interface of derived classes, you can use dynamic_cast. http://en.wikipedia.org/wiki/Dynamic_cast

0
source

The array must be of the exact type, because the compiler must be able to calculate the exact position of each element. Thus, you cannot have a mixture of subclasses or even treat an array of a subclass as an array of a base class.

0
source

Source: https://habr.com/ru/post/1738819/


All Articles