在C ++类中声明struct和typedef的位置

问题描述 投票:0回答:1

我在C中有以下代码:

coordenadas.c

#include "coordenadas.h"
#include <math.h>
#include <stdio.h>

/// multiplica um vetor por um escalar
/// este é um exemplo entregue pronto pra você ;)
vetor multiplicaPorEscalar(vetor v, escalar alpha)
{
    vetor resultado =
    {
        v.x * alpha,
        v.y * alpha,
        v.z * alpha,
        v.w * alpha
    };
    return resultado;
}

vetor somaVetorComVetor(vetor v, vetor u)
{
    //TODO: implementar
    vetor resultado = v;
    return resultado;
}

vetor diferencaVetorComVetor(vetor v, vetor u)
{
    //TODO: implementar
    vetor resultado = v;
    return resultado;
}

vetor diferencaEntrePontos(ponto p, ponto q)
{
    //TODO: implementar
    vetor resultado = {0,0,0,0};
    return resultado;
}

ponto somaPontoComVetor(ponto p, vetor v)
{
    //TODO: implementar
    ponto resultado = p;
    return resultado;
}

escalar normaDoVetor(vetor v)
{
    //TODO: implementar
    escalar resultado = 0;
    return resultado;
}

vetor normalizado(vetor v)
{
    //TODO: implementar
    vetor resultado = v;
    return resultado;
}

escalar distanciaEntrePontos(ponto p, ponto q)
{
    //TODO: implementar
    escalar resultado = 0;
    return resultado;
}

escalar produtoEscalar(vetor v, vetor u)
{
    //TODO: implementar
    escalar resultado = 1;
    return resultado;
}

vetor produtoVetorial(vetor v, vetor u)
{
    //TODO: implementar
    // Produto vetorial só faz sentido em 3D
    // Ignorar a componente "w" de "v" e "u"
    // Como o resultado é um vetor, o "w" dele deve ser 0
    vetor resultado = v;
    return resultado;
}

///
/// Referências: http://localhost:8080/classes/geometry/#30
escalar anguloEntreVetores(vetor v, vetor u)
{
    //TODO: implementar
    escalar resultado = 0;
    return resultado;
}

///
/// Referências: http://localhost:8080/classes/geometry/#22
ponto combinacaoAfim2Pontos(ponto p, ponto q, escalar alpha)
{
    //TODO: implementar
    ponto resultado = p;
    return resultado;
}

/// Imprime um vetor ou ponto no terminal
/// Uso:
///   vetor r = somaVetorComVetor(a, b);
///   imprime("vetor r", r);
void imprime(struct coordenadas c, char* nome)
{
    printf("%s = { %.2f, %.2f, %.2f, %.2f }\n", nome, c.x, c.y, c.z, c.w);
}

coordenadas.h

// Cria um novo nome ("escalar") para o tipo primitivo double
typedef double escalar;

// Estrutura que armazena 4 escalares
struct coordenadas {
  escalar x, y, z, w;
};

// Novo nome para a estrutura coordenadas: vetor
typedef struct coordenadas vetor;
// Novo nome para a estrutura coordenadas: ponto
typedef struct coordenadas ponto;

vetor multiplicaPorEscalar(vetor, escalar);
vetor somaVetorComVetor(vetor, vetor);
vetor diferencaVetorComVetor(vetor, vetor);
vetor diferencaEntrePontos(ponto, ponto);
ponto somaPontoComVetor(ponto, vetor);
escalar normaDoVetor(vetor);
vetor normalizado(vetor);
escalar distanciaEntrePontos(ponto, ponto);
escalar produtoEscalar(vetor, vetor);
vetor produtoVetorial(vetor, vetor);
escalar anguloEntreVetores(vetor, vetor);
ponto combinacaoAfim2Pontos(ponto, ponto, escalar);

void imprime(struct coordenadas, char*);

我正在将此代码翻译为C ++。但我有一个问题。翻译typedefs和structs的最佳形式是什么?我创建了一个类,还是在头文件中使用?

我正在翻译成这种形式:

coordenadas.h

#ifndef COORDENADAS_H
#define COORDENADAS_H

//typedefs and structs:

// Cria um novo nome ("escalar") para o tipo primitivo double
typedef double escalar;

// Estrutura que armazena 4 escalares
struct coordenadas {
escalar x, y, z, w;
};

// Novo nome para a estrutura coordenadas: vetor
typedef struct coordenadas vetor;
// Novo nome para a estrutura coordenadas: ponto
typedef struct coordenadas ponto;

class coordenadas
{
    public:
        coordenadas();
        virtual ~coordenadas();
        vetor multiplicaPorEscalar(vetor, escalar);
        vetor somaVetorComVetor(vetor, vetor);
        vetor diferencaVetorComVetor(vetor, vetor);
        vetor diferencaEntrePontos(ponto, ponto);
        ponto somaPontoComVetor(ponto, vetor);
        escalar normaDoVetor(vetor);
        vetor normalizado(vetor);
        escalar distanciaEntrePontos(ponto, ponto);
        escalar produtoEscalar(vetor, vetor);
        vetor produtoVetorial(vetor, vetor);
        escalar anguloEntreVetores(vetor, vetor);
        ponto combinacaoAfim2Pontos(ponto, ponto, escalar);
        void imprime(struct coordenadas, char*);

    private:
};

#endif // COORDENADAS_H

有没有更好的方法在C ++中执行此操作?

我尝试搜索这个,但我没有找到任何东西。

我希望找到最简单,最实际和最合适的方法。

c++
1个回答
1
投票

在C ++中,通常在类中使用member variables and member functions。成员函数将使用成员变量,而不是将所有内容作为参数。你也可以使用operator overloading进行一些操作。

typedef double escalar;

class coordenadas
{
public:
    escalar x, y, z, w;

    // Overload operator *= for multiplication with double
    coordenadas& operator*=(escalar alpha) {
        x *= alpha;
        y *= alpha;
        z *= alpha;
        w *= alpha;
        return *this;
    }

    // Use named function for multiplication with coordenadas
    void dotProduct(coordenadas other) {
        x *= other.x;
        y *= other.y;
        z *= other.z;
        w *= other.w;
    }

    // Or overload operator *= for this as well
    coordenadas& operator*=(coordenadas other) {
        dotProduct(other);
        return *this;
    }
};

class vetor
{
public:
    vetor& operator*=(escalar alpha) 
    {
        c *= alpha;
        return *this;
    }

    void dotProduct(vetor other) 
    {
        c.dotProduct(other.c);
    }

    void imprime(char* nome)
    {
        printf("%s = { %.2f, %.2f, %.2f, %.2f }\n", nome, c.x, c.y, c.z, c.w);
    }        

private:
    coordenadas c;
};
© www.soinside.com 2019 - 2024. All rights reserved.