-1

I noticed a few strange errors when I was trying to initialize constructors. It's easiest if I just show them.

#include <iostream>


class MyClass {

public:

    int var1, var2, var3;

    MyClass() {

        var1, var2, var3 = 0;

    }

    MyClass(int one, int two, int three) {

        var1 = one;
        var2 = two;
        var3 = three;
    }

};


int main()
{
    MyClass object1(5, 6, 7);
    std::cout << object1.var1; // This works

    MyClass object2();
    std::cout << object2.var1; //Error object must have class type.

    MyClass object3 = MyClass();
    std::cout << object3.var1; //Now it works for some reason

    delete &object1;
    MyClass ojbect1(5, 6, 7); //error redefinition; multiple initialization
}

As you can see, for some reason, when I initialize object2, the values won't print unless, as with object3, the syntax is written as MyClass object3 = MyClass();

Furthermore when I delete object1 I have to use a & operator. This surprised me considering in this question Deleting an object in C++ they don't use the ampersand.

Finally, when I try to re-initialize myobject, the compiler complains that there are multiple definitions. I tried leaving out the MyClass keyword and it still wouldn't allow it.

Now I can certainly make the program do what I want with various workarounds, but I don't understand why these things can't be done the way I wrote them here.

Community
  • 1
  • 1
bigcodeszzer
  • 916
  • 1
  • 8
  • 27
  • you create a `myobj2` but then you print ´myobj.var1´ is this typo? – 463035818_is_not_an_ai Nov 23 '15 at 21:13
  • o_O why are you trying to `delete` a local variable? – brettwhiteman Nov 23 '15 at 21:13
  • 2
    Default initialization syntax looks like `MyClass obj;`, not `MyClass obj();`. The second one is actually a function that takes no parameters and returns `MyClass`. – Weak to Enuma Elish Nov 23 '15 at 21:15
  • 1
    Please recheck your code, it is not consistent with the errors you commented in. In particular the output statements. – Captain Giraffe Nov 23 '15 at 21:15
  • C++ memory allocation works this way. You can explicitly delete what you have explicitly created, like "MyClass *ptr = new MyClass()". All automatic variables like "MyClass myObj;" are not yours. they are deleted automatically according to their lifetime. You always need to use pointers for delete. – Sreekar Nov 23 '15 at 21:18
  • If you want to destroy and reinitialize a variable with automatic storage: `MyType obj; obj.~MyType(); new (&obj) MyType();` – Weak to Enuma Elish Nov 23 '15 at 21:18
  • 1
    The reason they don't have to use `&` in the other question is that the argument to `delete` is a pointer variable, not a variable of class type. – Barmar Nov 23 '15 at 21:19
  • Should we not close this as a duplicate of this: http://stackoverflow.com/q/17060725/14065 – Martin York Nov 23 '15 at 21:33

2 Answers2

4
MyClass object2();
std::cout << object2.var1; 

doesn't work since object2 is parsed as "a function that takes no arguments and returns a MyClass". See Most vexing parse: why doesn't A a(()); work?

delete &object1; 

is cause for undefined behavior. You can delete only objects that were created using new.

MyClass object1(5, 6, 7);

is a problem like the compiler told. You cannot redeclare the name in the same scope. If you want it have a new value, you can use:

object1 = MyClass(5, 6, 7);
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • the question has been edited, there were a couple typos in the code – bigcodeszzer Nov 23 '15 at 21:24
  • Okay, that was working for a while, but now I overloaded the = operator and it is invoking an infinite loop in the copy constructor. – bigcodeszzer Nov 24 '15 at 20:00
  • @bigcodeszzer, Please start a different question with that particular problem. As always, it is better if you can post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – R Sahu Nov 24 '15 at 20:04
3
MyClass myobj2();

Is not what you think it is. It declares a function named myobj2 that returns a MyClass.

MyClass myobj(5, 6, 7);

Causes an error, because even if you delete myobj before, it is still declared to be of type MyClass. Deleting the object does not undeclare the variable. If you delete it and want to recreate it do this:

myobj = MyClass(5,6,7);

However, deleting a local object is nonsense. You dont have to delete an object that anyway will be deleted once it goes out of scope. Actually you should only delete objects that were created via new, otherwise you have undefined behaviour.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185