-5

In Java

nodes[i] = new Object();

is a valid statement

What is the C++ equivalent?

EDIT:

It seems I am not providing enough context.

I was reading some Java code implementing a QuadTree, and I was curious on how I could rewrite that segment of code in C++.

I did not need a direct equivalent, I wanted something that essentially means the same thing.

In Java, you can do things like

class A
{
public A (someObject o);
}

main method:

A aObject = new A(new someObject());

or in a loop:

for(int i = 0; i < arr.length; i++)
{
arr[i] = new someObject();
}

In C++ I know you can do:

std::vector<someObject*> arr;
someObject* o = new someObject;
arr.push_back(o);

Is there something similar to java's new Object(); style of creating objects without explicit declaration in C++?

RN_
  • 838
  • 2
  • 11
  • 29
  • 2
    There is no direct equivalent. It depends on what you want to store in `nodes`. – juanchopanza May 28 '14 at 14:11
  • 3
    Learn C++ properly and don't use Java as a guide in writing C++ code. Looking for "equivalences" will just lead down the path of writing awful C++ code. – PaulMcKenzie May 28 '14 at 14:40
  • @PaulMcKenzie ...unless this is a matter of translating code from Java to C++. Hard to tell without knowing the context. – Spook May 28 '14 at 14:50
  • @Spook There's no point in "translating" from one language to another. Especially if there is no direct translation. – juanchopanza May 28 '14 at 14:54
  • @juanchopanza Is it? A lot of sample algorithm implementations are available on the Internet in Java and I had to translate them to C++. And IMO if you *really*, ***really*** need a *direct* translation, simply implement a base `Object` class and you're good to go. – Spook May 28 '14 at 14:58
  • @Spook Translating an algorithm which usually consist of basically looping constructs and decision statements can be done from any language to any language. What the OP posted is not an algorithm. – PaulMcKenzie May 28 '14 at 15:25
  • I edited the OP to provide more context/examples. – RN_ May 30 '14 at 15:51

2 Answers2

4

Java is a garbage-collected language, while C++ is not. The languages differ so much on what the code above "means", that it is not trivial to quote a direct "equivalent".

A similar method of implementing this in C++, would be to use shared_ptr, which is not garbage collected, but instead ensures that the underlying objects are destroyed when all references to them go out of scope.

#include <vector>
#include <memory>
using namespace std;


class Object
{
};

int main()
{
    std::vector<std::shared_ptr<Object>> nodes(1);

    nodes[0] = std::make_shared<Object>();

    return 0;
}
Chad
  • 17,081
  • 2
  • 40
  • 60
1

There are actually a few aspects regarding your question, depending on what do you really mean by the piece of code you posted.

1. Explicit command

Yes, this command can be issued in C++, for example:

class Object
{

};

int main(int argc, char * argv[])
{
    std::vector<Object *> nodes;
    nodes.push_back(nullptr);
    int i = 0;

    nodes[i] = new Object();

    // To prevent memory leaks
    delete nodes[i];
}

2. Using generic base class, Object

C++ does not have a universal base class such as Object in Java or C#. You have to instantiate something to put it into the array or std::vector. (read more: Root base class in C++)

If you really need such class in your code, you can simply define one, for example:

class Object
{
    virtual std::string ToString()
    {
        return "Object";
    }

    virtual int GetHashCode()
    {
        return (int)this;
    }

    virtual bool Equals(Object & other)
    {
        return this == &other;
    }
};

3. Memory management

In C++ you can explicitly instantiate class at some point.

However, C++ does not have garbage collector working for the dynamic objects such as Java or C#. If you allocate memory explicitly using new operator, you have to delete allocated memory at some point.

On the other hand, C++ tries lately to catch up to high-level languages by providing a set of classes simplifying memory management, such as std::shared_ptr, std::weak_ptr or std::unique_ptr, for example:

class Object
{

};

int main(int argc, char * argv[])
{
    std::vector<std::unique_ptr<Object>> nodes;
    nodes.resize(1);
    int i = 0;

    nodes[i] = std::unique_ptr<Object>(new Object());

    // nodes destructor will call std::unique_ptr<Object>
    // destructor, which will eventually destroy instance
    // of the Object class.
}

Read more here: What is a smart pointer and when should I use one?

4. Indexing arrays, classes

You can always use [] to index arrays. You may use [] to index class instances (such as std::vector) if class supports that (overloads [] operator).

int main(int argc, char * argv[])
{
    // Statically allocated array
    int test[5];
    test[0] = 1;

    // Dynamically allocated array
    // This is useful if you work with
    // some C libraries or simply (really)
    // need to allocate a block of memory.
    int * test2 = new int[5];
    test2[0] = 1;
    delete[test2];

    // This is a lot more C++ way of keeping
    // an array of items:
    std::vector<int> test3;
    test3.resize(1);
    test3[0] = 1;
}
Community
  • 1
  • 1
Spook
  • 22,911
  • 14
  • 79
  • 146