Java转Cpp代码转换,关于static和new关键字的问题

huangapple go评论91阅读模式
英文:

Java to Cpp Code Conversion , problem regarding static and new keyword

问题

class WFG1 {
    // Define WFG1 class here
};

class ProblemDef {
private:
    static WFG1* w12;

public:
    ProblemDef() {
        w12 = new WFG1(1, 23, 2);
    }

    ~ProblemDef() {
        delete w12;
    }
};

// Define the destructor for ProblemDef outside the class
ProblemDef::~ProblemDef() {
    delete w12;
}

// Initialize the static member variable outside the class
WFG1* ProblemDef::w12 = nullptr;

int main() {
    ProblemDef pd;
    // Rest of your code
    return 0;
}

In C++, the equivalent of the "new" keyword-based object declaration is the dynamic memory allocation using the "new" operator, which returns a pointer to the newly allocated object. The "delete" operator is used to free the dynamically allocated memory.

Static declaration of objects in C++ is done using the "static" keyword. When you declare a static member variable in a class, there is only one copy of that variable shared among all instances of the class. Static member variables must be defined outside the class using the scope resolution operator (::).

英文:
public class ProblemDef {
        private static WFG1 w12;
        }
 public ProblemDef()
        {
            w12 = new WFG1(1,23,2);
}

If I write this in C++ as follows

class ProblemDef{
         private:
                static WFG1* w12;
         public:
                ProblemDef(){
                w12=new WFG1(1,23,2);
                }
};

It shows an error, how can I proceed. What is the equivalent of a "new "keyword-based object declaration in C++? How is static declaration of objects done in C++?

答案1

得分: 2

在C++中,应尽量少使用new,因为您还需要delete您所创建的内容,而这往往很难做到正确。这里根本不需要使用new

class ProblemDef{
    private:
        static WFG1 w12;      // 没有指针

    public:
        ProblemDef() {}
};

WFG1 ProblemDef::w12(1,23,2); // 静态变量的初始化

这会使所有的ProblemDef实例共享相同的WFG1

如果您真的希望每个ProblemDef实例都有一个新的WFG1,就不要将它声明为static

英文:

In C++ you should use new as little as possible because you also need to delete what you've newed, which is tricky to get right. There's no need for new at all here:

class ProblemDef{
    private:
        static WFG1 w12;      // no pointer

    public:
        ProblemDef() {}
};

WFG1 ProblemDef::w12(1,23,2); // initialization of the static variable

This makes all instances of ProblemDef to share the same WFG1.

If you really want a new WFG1 for each instance of ProblemDef, don't make it static.

答案2

得分: 0

首先让我们从关键字 "new" 的作用开始。"new" 用于创建对象,并通过分配内存来实现此功能。在 Java 和 C++ 中,"new" 的作用基本相同,但在 C++ 中分配内存时需要谨慎。

在 C++ 中,没有像 Java 中那样的自动垃圾回收机制。

所谓 "自动垃圾回收" 就是在 Java 中,当你调用关键字 "new" 时,底层的编译器代码会删除使用关键字 "new" 创建的程序不再使用的对象。

在 C++ 中,作为程序员,你需要负责编写 "析构函数",它相当于一段代码,用于检测对象是否不再被使用,并应将其删除。

一个忠告是,析构函数可能会比较棘手,因此我总是建议刚开始学习 C++ 的程序员编写一个 "销毁" 方法。这个方法会以一种算法方式遍历并删除所有对象(例如,在你使用完链表后,销毁函数会遍历并删除链表中的每个节点)。

希望这能帮到你!

英文:

First lets start with what the keyword "new" is doing. New is creating an object and is doing this by allocating memory. new in Java and C++ relatively do the same purpose of allocating memory however, you need to be careful when allocating memory in C++.

In C++ there is no automatic garbage collection like there is in Java.

All that "Automatic garbage collection" means is that in java when you call the keyword new there is underlying compiler code that is deleting objects created with key word new that your program is no longer using.

You the programmer are in charge of creating "De constructor's" in C++. which is the equivalent to code that will run and detect if a object is no longer being used and should be deleted.

A word to the wise, Deconstructors can be tricky so I always tell new C++ programmers to have a "Destroy" method. This method goes through and deletes all objects in an algorithmic way (For example when you are done with a linked list the destroy function will loop through and delete each node in the linked list).

Hope this helps!

huangapple
  • 本文由 发表于 2020年3月17日 01:12:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/60710328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定