简体   繁体   English

Javascript to C ++:对象等效吗?

[英]Javascript to C++: Object Equivalent?

I've been tasked with converting some code from javascript to C++. 我的任务是将一些代码从javascript转换为C ++。 While I'm decently familiar with Javascript, I'm pretty inexperienced with C++. 虽然我对Java相当熟悉,但是我对C ++缺乏经验。

The javascript code heavily makes use of objects. javascript代码大量使用对象。 For example the following code is used to convert angles from degrees to any other specified unit: 例如,以下代码用于将角度从度转换为任何其他指定单位:

var allConversions = {
"Angle": {
    "degrees": {
        "degrees":function(inputNum) { return inputNum*1},
        "minutes":function(inputNum) { return inputNum*60},
        "radians":function(inputNum) { return inputNum*0.0174532925},
        "revolutions":function(inputNum) { return inputNum*0.00277777778},
        "seconds":function(inputNum) { return inputNum*3600},
    }
}

exports.convertUnits= function(unitType, inUnit, outUnit, inputVal) {
    return allConversions[unitType][inUnit][outUnit] (inputVal);
}

I'm wondering what is the best practice for how to create something similar in C++? 我想知道如何在C ++中创建类似内容的最佳实践是什么? Should I try and create something similar with a struct or class? 我应该尝试用struct或class创建类似的东西吗?

Not exactly sure what all the down votes are about. 不确定所有的否决票是关于什么的。 I see nothing wrong with your question. 我认为您的问题没有错。

JavaScript is a typeless language, and it's a bit... flexible in it's construction of objects. JavaScript是一种无类型的语言,它在对象构造方面有点...灵活。 Depending on the actual code, you have two options and you'll want to use a mix of both. 根据实际的代码,您有两个选择,并且您想同时使用两者。

Option 1: Create a Class 选项1:建立课程

In this case, you'd create a class for the specific data structure, with properties for each value you need. 在这种情况下,您将为特定的数据结构创建一个类,并为每个所需的值提供属性。

Use this when the JavaScript object is consistent throughout all it's uses. 当JavaScript对象在所有使用过程中保持一致时,请使用此选项。

Option 2: Use a Hash Map 选项2:使用哈希图

There are a variety of different hash map classes. 有许多不同的哈希映射类。 Which you choose is up to the specific version and framework(s) you're using. 您选择哪个取决于所使用的特定版本和框架。

Regardless though, these generally work like a JavaScript object, where you can create key/value pairs. 无论如何,它们通常像JavaScript对象一样工作,您可以在其中创建键/值对。 Use this when your not quite sure what you're data will be. 当您不太确定数据是什么时,请使用此选项。

Depends a lot on overall structure and context - that is missing in your code snippet. 在很大程度上取决于整体结构和上下文-代码段中缺少这些内容。 Probably, a simple class with some inline functions would do. 一个带有某些内联函数的简单类可能会做。 But if I had to attempt an equivalent code as provided, I'd have to write something like this: 但是,如果我必须尝试提供的等效代码,则必须编写如下代码:

someClass.hpp someClass.hpp

#ifndef SOMECLASS_H
#define SOMECLASS_H

#include <string>

using std::string;

class degrees
{
    public:
        double inputVal;

        degrees(string, string, string, double); // a constructor

        double degreesFunc(double); // double?, can't have same name func
        double minutes(double);
        double radians(double);
        double revolutions(double);
        double seconds(double);
};

class Angle : public degrees
{
    public:
        Angle(string, string, string, double);
};

class allConversions : public Angle
{
    public:
        allConversions(string, string, string, double);
};

#endif /* SOMECLASS_H */

someClass.cxx someClass.cxx

#include "someClass.hpp"

degrees::degrees(
        string unitType,
        string inUnit,
        string outUnit,
        double inputVal)
{
    this->inputVal = inputVal;
}

double degrees::degreesFunc(double inputNum)
{
    return inputNum*1;
}

double degrees::minutes(double inputNum)
{
    return inputNum*60;
}

double degrees::radians(double inputNum)
{
    return inputNum*0.0174532925;
}

double degrees::revolutions(double inputNum)
{
    return inputNum*0.00277777778;
}

double degrees::seconds(double inputNum)
{
    return inputNum*3600;
}

//-------------------------------------------------

Angle::Angle(
        string a,
        string b,
        string c,
        double d)
    : degrees(a, b, c, d) { }

allConversions::allConversions(
        string a,
        string b,
        string c,
        double d)
    : Angle(a, b, c, d) { }

test.cpp 测试文件

#include <iostream>
#include "someClass.hpp"

using std::cout;

int main()
{
    allConversions convertUnits("what?", "what?", "what?", 10);

    cout << convertUnits.inputVal << '\n';
    cout << convertUnits.radians(10) << '\n';
    cout << convertUnits.minutes(10) << '\n';
}

Compiling with g++ with Makefile: 使用带有Makefile的g ++进行编译:

all:
    g++ -c someClass.cxx
    g++ -c test.cpp
    g++ someClass.o test.o -o run

Run: ./run 运行: ./run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM