0

I'm working on some C++11 examples, but i'm a little rusty. I'm trying to add an object instance to a class attribute. I have something like this:

Entity.h

class Entity {
    private:
        MyClass object;

    public:
        Entity();
        void doTest();
};


Entity.cpp

#include "Entity.h"

Entity::Entity() {
}

void Entity::doTest(stuff) {
    object = new MyClass(stuff);
}

Is this correct? How can i do this in C++?

vinnylinux
  • 7,050
  • 13
  • 61
  • 127

4 Answers4

2

It's all correct apart from the new. Only use that when you need dynamic allocation; in this case, you just want to create an object:

object = MyClass(stuff);

Or perhaps you want to initialise it in the constructor instead:

Entity(stuff) : object(stuff) {}
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

It is wrong. object is an object not a pointer. but your code

object = new MyClass(stuff);

treat object as a pointer.

You can either declare object as a pointer in the class Entity or change your function doTest;

If you want a pointer it is better to use smart pointers in C++, such as unique_ptr.

CS Pei
  • 10,869
  • 1
  • 27
  • 46
0

You want to use in the decleration:

MyClass* object

Also, if you are going to use new MyClass make sure you use delete object to avoid leaks.

i.e.

Entity::Entity() { object = NULL; } //constructor

Entity::doTest(stuff) {
    delete object;
    object = new MyClass(stuff);
}

//Following rule of three, since we need to manage the resources properly
//you should define Copy Constructor, Copy Assignment Operator, and destructor.

Entity::Entity(const Entity& that) { //copy constructor
    object = that.object; 
    //assumes you've correctly implemented an assignment operator overload for MyClass
}

//invoke copy and swap idiom* if you wish, I'm too lazy
Entity& Entity::operator=(const Entity& source) {
    MyClass* temp = new MyClass(source.object)
    //assumes you've correctly implemented an copy constructor (or default one works) for MyClass.
    delete object;
    object = temp;
    return *this;
}

Entity::~Entity() {   //destuctor
   delete object;
}

You should avoid dynamic allocation if it is at all possible. You should also use smart pointers (like std::shared_ptr) but if you do wish to use raw pointers, then abide by the rule of three.

*copy and swap idiom

Community
  • 1
  • 1
Chemistpp
  • 2,006
  • 2
  • 28
  • 48
  • What's the difference between MyClass* object and MyClass *object? – vinnylinux Oct 03 '13 at 17:39
  • What does delete MyClass do? Where should i do this? After using the object? – vinnylinux Oct 03 '13 at 17:39
  • nothing. I would put a test to check if you've created a new object (i.e. ran your doTest function) then if so, in your destructor, call delete object if you have created one. You need to delete it to free the memory allocated by the system. If you do not, you can have memory leaks that will destroy performance. – Chemistpp Oct 03 '13 at 17:40
  • 1
    @vinnylinux: `MyClass* object` and `MyClass *object` are exactly the same: white-space has no significance in C++. Use `delete` when you've created something with `new` and for some reason don't feel like managing it safely with a smart pointer. – Mike Seymour Oct 03 '13 at 17:41
  • What's the ~ doing in that example? – vinnylinux Oct 03 '13 at 17:42
  • What on earth is that `Created` flag for? If you want to know whether the pointer has been set, check the pointer. – Mike Seymour Oct 03 '13 at 17:44
  • @MikeSeymour lol, I didn't feel like testing to make sure that would work. I was winging it. – Chemistpp Oct 03 '13 at 17:44
  • 1
    @vinnylinux: `~Entity` is the destructor, called automatically when an object is destroyed. It's used to clean up any resources the object was managing; in this case, to delete the dynamically allocated object. Usually, you won't need one, because you won't be messing around with raw pointers like this. – Mike Seymour Oct 03 '13 at 17:46
  • No difference in between `MyClass *object` or `MyClass *object` – Jean-Baptiste Yunès Oct 03 '13 at 17:47
  • I was also going to say if you don't know what constructor/destructors are, then you probably shouldn't be using dynamic allocation. – Chemistpp Oct 03 '13 at 17:47
  • @Chemistpp: You shouldn't be using dynamic allocation without a good reason in any case. I'm not quite sure why you're suggesting it here, still less why you're suggesting juggling a raw pointer. If you really must keep this bad example, then you should also explain the [Rule of Three](http://stackoverflow.com/questions/4172722). (Also, there's no need to test the pointer before deleting it). – Mike Seymour Oct 03 '13 at 17:49
  • @MikeSeymour I only suggested it because the OP had `object = new MyClass(stuff);` which made me assume he knew this but spaced on the declaration. Also, explaining the rule of three would be just a reiteration of what someone else said, smarter/better than me, on SO already. – Chemistpp Oct 03 '13 at 17:54
  • @Chemistpp: If you believe that the `new` is needed, you should at least mention the rule; or, better still, demonstrate how to make the class correct. As it is, this answer appears to say that you only need a destructor, which is dangerously wrong. – Mike Seymour Oct 03 '13 at 17:58
  • There is no need for the `if (p != NULL) delete p;` logic. It can just be written as `delete p;` – Zac Howland Oct 03 '13 at 18:10
0

In C++ your object field is really an object. That means that there is an allocated memory inside every Entity object you may create. The problem is how you can initialize that object field ?

  • if MyClass has no ctor or a ctor callable with no parameter, everything is ok.
  • if not, you should define the initialization of the field at the same time you define the ctor of Entitythis way

    Entity::Entity() : object(parameters) { code for Entities initialization }

    this is a way to ensure the correctness of your initialization so that object is initialized before you have control on the initialization of the Entity.

Your object is statically initialized inside each Entity, this is a good way, in C++, to code what is called a composition in object oriented programming.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69