In C ++ what is the command corresponding to Java super ()?

4

In Java:

public class Teste extends Testando
{
    private String nome;
    private int numero;
    public Teste(String teste, String nome, int numero)
    {
        super(teste);
        this.nome = nome;
        this.numero = numero;
    }
}

What is the command corresponding to super() in C++ ?

    
asked by anonymous 26.03.2016 / 21:10

2 answers

8

You explicitly call the class name, because in C ++ there is multiple inheritance and may have more than one ancestor. In a rough way this would be it:

class Teste : public Testando {
    String nome;
    int numero;
    public:
        Teste(String teste, String nome, int numero) : Testando(teste) {      
            this.nome = nome;
            this.numero = numero;
    
26.03.2016 / 21:19
2

There is no direct relationship in C ++.

You can do something similar if you create a super typedef for all classes that want to use this feature, as in the case below:

class Derivada: public Base
{
   private:
      typedef Base super; // não deve ser público porque cada classe pode 
                          // ter apenas uma classe super
} ;

And then you could use super in place of the base class name, as in the example:

super::meumetodo();

However, we do not recommend abusing this feature. In C ++ there is the possibility of multiple inheritance and this would already complicate the solution and could cause confusion.

    
26.03.2016 / 21:18