How to pass the class (Teste2 teste2) as reference in the ("super") C++ constructor?

Asked

Viewed 69 times

1

On the basis of that reply In C++ what command corresponds to super() of Java?

Class Teste2{
int x;
int y;
public:
  Teste2(int x, int y){
.
.
.
}
}
class Testando{
 Teste2 *teste2;
public:
   Testando(Teste *teste2)
{.
 .
 .
 .
class Teste : public Testando {
    string nome;
    int numero;
    public:
         //Supondo que Teste2 teste2 foi instanciado na Classe Testando
        Teste(Teste2 *teste2, string nome, int numero) : Testando(teste2) {      
            this.nome = nome;
            this.numero = numero;

How to reference a class in the constructor ?

  • I didn’t understand what you want, nor the goal, mainly, but not only, because it lacks context.

  • @mustache on the other question was a string now a class or type Test2, it was clear or not?

  • 1

    It’s hard to help you because even though you say it’s going to change, you always put everything in half, no context, no organization, we get lost without even knowing what the code should do and have to keep completing, tidying, until the code compiles. Have you read this? http://answall.com/help/mcve. The code must be compileable at least in the part that is not part of the doubt. Playing code anyway makes any help difficult.

  • @bigown understand, that this code create on the spot here on the site I have not ready, but I believe it is the libraries that are missing. #include<Teste2>

1 answer

1


The code has several small problems, I don’t even know what to say to clarify the question because I didn’t understand it, I don’t even know if you have a specific question. There was a code played without much criterion and that was not even to be tested. Note that I didn’t change anything to make it work what was asked, I was fixing basic syntax errors and what else could be a doubt is the change of the operator . for the operator -> in the this.

#include <string>
using namespace std;
class Teste2 {
    int x;
    int y;
    public:
        Teste2(int x, int y) {}
};
class Testando {
    Teste2 *teste2;
    public:
        Testando(Teste2 *teste2) {}
};
class Teste : public Testando {
    string nome;
    int numero;
    public:
         //Supondo que Teste2 teste2 foi instanciado na Classe Testando
        Teste(Teste2 *teste2, string nome, int numero) : Testando(teste2) {      
            this->nome = nome;
            this->numero = numero;
        }
};
int main() {
    auto teste = Teste(new Teste2(1, 2), "joão", 1);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

To tell you the truth I think that most of these codes don’t make sense and I don’t believe that this is teaching anything really useful.

  • my doubt was in this excerpt of the code Teste(Teste2 *teste2, string nome, int numero) : Testando(teste2) { the way I put it was right?

  • Yeah, I just don’t know if you really need this, but how I say loose code without context anything that compiles is right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.