0

I have a class that has a const member, const pointer and enum class member,

My Questions for the sample code bellow:

  1. How to nuliffy a enum class member of "other"` in move constructor properly (what value to assign to it?)
  2. How to nullify a const pointer of "other"` in move constructor so that destructor of other does not delete a memory of object that is being constructed, and so that a pointer reamins valid?
  3. How to nullify a constant member of "other" in move constructor so that destructor of other does not get called?

enum class EnumClass
{
    VALUE0, // this is zero
    VALUE1
};

class MyClass
{
  public:
    MyClass() : 
        member(EnumClass::VALUE1),
        x(10.f),
        y(new int(4)) { }

    MyClass(MyClass&& other) : 
        member(other.member),
        x(other.x),
        y(other.y)
    {
        // Can I be sure that this approach will nullify a "member" and avoid
        // destructor call of other
        other.member = EnumClass::VALUE0;

        // Or shall I use this method?
        other.member = static_cast<EnumClass>(0);

        // ERROR how do I nullify "x" to avoid destructor call of other?
        other.x = 0.f;

        // ERROR the same here, delete is going to be called twice!
        other.y = nullptr;
    }

    ~MyClass() 
    {
        delete y;
    }

  private:
    EnumClass member;
    const float x;
    int* const y;
};
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
codekiddy
  • 5,897
  • 9
  • 50
  • 80
  • can you make your enums start at 1, using 0 as a special case? – PlasmaHH Apr 26 '12 at 15:20
  • @ PlasmaHH How is that going to help? also I spend 20 minutes but can't solve this code tags in my post, it just don't work. – codekiddy Apr 26 '12 at 15:22
  • 1
    If you want to change a value, you just cannot make it const. It's that simple. – Bo Persson Apr 26 '12 at 15:34
  • I'm fairly certain that if your class contains constant pointers, you're not going to be able to come up with a satisfactory answer for move construction. You'll have similar problems writing an assignment operator (copy or move based). You'll have to determine if the cost saving is worth changing the pointer to be non-constant. – Dave S Apr 26 '12 at 15:34
  • possible duplicate of [Move constructor and const member variables](http://stackoverflow.com/questions/6317429/move-constructor-and-const-member-variables) – Bo Persson Apr 26 '12 at 15:37
  • @Dave So that means that classes having *const* members are useless because they can't be copied or moved? – codekiddy Apr 26 '12 at 15:41
  • Const members can be copied. That's no problem at all. Copy constructors do it all the time. – Rob Kennedy Apr 26 '12 at 16:09
  • Ok, I understand, and what about 1st question? how do I solve that? thanks. – codekiddy Apr 26 '12 at 16:14
  • For the enum class (and the others, quite frankly), I would use `std::move()` (e.g. `member(std::move(other.member))` ), and let their own move constructors handle it. If they've got a proper 'invalid' state, their move constructors should handle it, and not leave it up to the user to set their state invalid. – Dave S Apr 26 '12 at 17:03

1 Answers1

0

You don't need to worry about nullifying non-pointer types. They cannot be "freed" more than once, because their data is located inside of the class, not on the heap. Your const int pointer does need to be nullified, and it looks like you are doing that right.

Take a look at this MDSN article for more info: http://msdn.microsoft.com/en-us/library/dd293665.aspx

EDIT:

If you declare an int* const y, you are declaring the pointer to be const. If you simply want the int to be const, declare it as const int* y. You cannot change the address of a pointer declared as int* const y.

gcochard
  • 11,408
  • 1
  • 26
  • 41
  • The MSDN article provided does not explain my question because the article doesn't uses **const** members, What you mean by "it looks like you are doing that right" ? I'm not doing right cos my code does not compile :D – codekiddy Apr 26 '12 at 15:35
  • Whoops, missed the whole const part there. If you declare an int* const y, you are declaring the pointer to be const. If you simply want the int to be const, declare it as const int* y. You cannot change the address of a pointer declared as int* const y. – gcochard Apr 26 '12 at 15:42
  • non-pointer types cannot be freed but it's preferrable they do not continue to exist in a _valid_ state after the move has occured. – Captain Obvlious Apr 26 '12 at 15:43