Is it possible to make a member of the base class change a type in a derivative if the derivative has a type that has the same name as its type?

To make it easier, you can imagine that rb_tree :: NODE is called rb_tree :: rb_NODE.

struct search_tree { struct NODE { NODE * parent; NODE * left; NODE * right; }; NODE * root; // функция поиск максимального ΠΊΠ»ΡŽΡ‡Π° // ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ Ρ‡Π»Π΅Π½ search_tree::root int max_() { root; return 1; } insert(); find(); delete(); min(); search_tree::search_tree(){root = new NODE;} }; struct rb_tree : public search_tree { // ΠΌΠΎΠΆΠ½ΠΎ Ρ‚Π°ΠΊΠΆΠ΅ Π½Π°Π·Π²Π°Ρ‚ΡŒ rb_NODE struct NODE : public search_tree::NODE { char color; }; //!! Π²ΠΎΡ‚ здСсь Π³Π»Π°Π²Π½Ρ‹ΠΉ смысл. // ΠΏΠΎΠΏΡ‹Ρ‚ΠΊΠ° ΠΏΡ€ΠΈΡΠ²ΠΎΠΈΡ‚ΡŒ rb_tree::NODE Ρ‚ΠΈΠΏ search_tree::NODE // мСня интСрСсуСт, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΡ€ΠΈ создании ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° root ΠΈΠΌΠ΅Π» // Ρ‚ΠΈΠΏ rb_tree::NODE int insert() { NODE * node2; node2 = root; } delete(); }; 

The general essence is this:

 1) класс search_tree Ρ€Π΅Π°Π»ΠΈΠ·ΠΎΠ²Π°Π½, Ρ‚Π°ΠΌ Π΅ΡΡ‚ΡŒ insert find delete max min 2) класс rb_tree ΠΏΠΎΠ΄ΠΎΠ±Π΅Π½ search_tree измСняСтся Ρ‚ΠΎΠ»ΡŒΠΊΠΎ insert delete Но Π² rb_tree Ρ‚ΠΈΠΏ NODE Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΠΌΠ΅Ρ‚ΡŒ ΠΏΠΎΠ»Π΅ color. БоотвСтствСнно ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ rb_tree:: ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡŽΡ‚ rb_tree::NODE, Π° ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ search_tree:: search_tree::NODE 

Of course, the easiest way is to simply add the color field to the NODE type in the search_tree class and delete the rb_tree :: NODE field, which is now done.

But what interests me is the possibility of implementation as in the question.

  • 2
    Make the search_tree and rb_tree templates from the TNode type. - Costantino Rupert

2 answers 2

What is described in the task is very similar to such a thing as Double dispatch . The same principle is used in the Visitor pattern.

Double call dispatching is the ability to dynamically determine an executable method not only based on the type of object on which the method is executed (supported explicitly in C ++, Java and any other object-oriented language), but also on the basis of information about the types of argument available during run and inaccessible statically (at compile time). (taken here )

Read the links below and I think you will find the right solution to the problem, perhaps a small modification of the code.

Well, no one has canceled Google + by Scott Meers.

  • I do not really understand about this. Very little information. It seems to be similar. Does this mean overloading functions for different types? That is, the definition of methods must be removed from the class? Like others advise doing through templates? - manking

What to do - Kitty has already explained: the template function is the simplest and most reliable.

Why is that. The Node type in the search_tree and rb_tree classes are different types. One is called search_tree :: Node, the other is rb_tree :: Node.