Create class using new?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mlt

    Create class using new?

    I have the following class:

    class Test {

    public:
    Test() {
    a = 33;
    }

    private:
    int a;
    };


    When I want to create an instance of this class in eg java I do:

    Test t = new Test();

    but in C++ I either do:

    1) Test t;
    or
    2) Test t();

    (what are the difference between 1 and 2?).

    I can't do:

    Test t = new Test();

    but is that not the only way to make sure the instance lives after the
    calling function has returned?

  • Tonni Tielens

    #2
    Re: Create class using new?

    On Nov 21, 8:29 pm, "mlt" <a...@asd.comwr ote:
    but is that not the only way to make sure the instance lives after the
    calling function has returned?
    Option 1 and 2 are equal. If a class has a default constructor the ()
    behind the identifier can be omitted. In C++ you can also do

    Test* t = new Test();

    The difference with your options is that in this case a Test object is
    dynamically created and t is a Test pointer that points to that
    object. This is indeed the only way to make sure the instance lives
    after the function returned, but keep in mind that in C++ dynamically
    created objects are not freed automatically. You will have to
    explicitly call delete on a pointer to the object. Eg.

    Test* t = new Test();
    // Some code.
    delete t;

    If you don't, the object t points to will never be deleted and you
    have a memory leak. I suggest you to read a good book on C++ if these
    things are unfamiliar to you.

    Comment

    • acehreli@gmail.com

      #3
      Re: Create class using new?

      On Nov 21, 12:01 pm, Tonni Tielens <tonnitiel...@g mail.comwrote:
      On Nov 21, 8:29 pm, "mlt" <a...@asd.comwr ote:
      The options were

      1) Test t;
      or
      2) Test t();
      Option 1 and 2 are equal.
      That never fails to catch us off guard: Option 2 is the declaration of
      a function. :)

      Ali

      Comment

      • Salt_Peter

        #4
        Re: Create class using new?

        On Nov 21, 2:29 pm, "mlt" <a...@asd.comwr ote:
        I have the following class:
        >
        class Test {
        >
        public:
        Test() {
        a = 33;
        }
        >
        private:
        int a;
        >
        };
        >
        When I want to create an instance of this class in eg java I do:
        >
        Test t = new Test();
        >
        but in C++ I either do:
        >
        1) Test t;
        or
        2) Test t();
        >
        (what are the difference between 1 and 2?).
        >
        I can't do:
        >
        Test t = new Test();
        new returns a pointer, since t is an instance of Test, you can't store
        a pointer (to an allocated Test) into it, thankfully. This works:

        Test* p_t = new Test();
        ....
        delete p_t; // required

        It is unwise to allocate on the heap unless absolutely necessary. And
        when it is necessary its better to rely on RAII or smart pointers.
        So this is better since it will allocate + invoke ctor + get
        destructed automatically.

        Test t; // allocated on the stack, not heap
        >
        but is that not the only way to make sure the instance lives after the
        calling function has returned?
        No, definitely not the way. Its shoddy programming to allocate
        something over there and deallocate somewhere else. Managing the
        lifetime of an object should either be automatic or given to a single
        manager. In Java, your GC takes care of that while C++ uses a
        different discipline. Java only uses the stack to store primitives, C+
        + can use the stack to store anything.

        Comment

        • mlt

          #5
          Re: Create class using new?


          <acehreli@gmail .comwrote in message
          news:0042c6ae-fde7-42f2-912c-f7ba4f7b5105@w1 g2000prk.google groups.com...
          On Nov 21, 12:01 pm, Tonni Tielens <tonnitiel...@g mail.comwrote:
          On Nov 21, 8:29 pm, "mlt" <a...@asd.comwr ote:
          The options were

          1) Test t;
          or
          2) Test t();
          Option 1 and 2 are equal.
          That never fails to catch us off guard: Option 2 is the declaration of
          a function. :)

          Ali



          Ok so:

          Test t();

          actually declares a function that returns a type Test, which could be
          implemented/defined in e.g a .cpp file? But what about:


          Test* t = new Test();

          versus:

          Test* t = new Test;

          that would create the same pointer right?

          Comment

          • acehreli@gmail.com

            #6
            Re: Create class using new?

            On Nov 21, 12:46 pm, "mlt" <a...@asd.comwr ote:
            But what about:
            >
            Test* t = new Test();
            >
            versus:
            >
            Test* t = new Test;
            >
            that would create the same pointer right?
            The short answer is: yes, in both cases, t points to an object
            allocated on the heap.

            But there will be a difference whether Test is a POD or not. The rules
            are too complicated for me to remember. I need to do a search
            everytime I wonder the behavior.

            In this case, if Test is non-POD, the two are the same and the object
            is default constructed. But if Test is a POD, then the second will not
            "zero-initialize" the object.

            A search for "is there a difference between new MyClass; and new
            MyClass()" in this group finds a thread about the same issue with many
            more responses.

            Ali

            Comment

            • Paavo Helde

              #7
              Re: Create class using new?

              "mlt" <asdf@asd.comki rjutas:
              I have the following class:
              >
              class Test {
              >
              public:
              Test() {
              a = 33;
              }
              >
              private:
              int a;
              };
              >
              >
              When I want to create an instance of this class in eg java I do:
              >
              Test t = new Test();
              >
              but in C++ I either do:
              >
              1) Test t;
              or
              2) Test t();
              >
              (what are the difference between 1 and 2?).
              1) defines a default-constucted variable t of type Test.

              2) declares a function t(), taking no arguments and returning a Test
              object by value (welcome to the wonderful world of C++ cryptic
              declaration syntax!).
              >
              I can't do:
              >
              Test t = new Test();
              You probably want

              Test* t = new Test();

              In C++, the types Test* (a pointer) and Test (a class) are different.

              However, a raw pointer is usually not a good way to manage the ownership
              of the created object, it may get lost too easily in presence of
              exceptions, and there is no garbage collection present by default. A
              better way might be to use std::auto_ptr or boost::shared_p tr.
              >
              but is that not the only way to make sure the instance lives after the
              calling function has returned?
              Instances are needed only for "entity" objects. For "value" objects it is
              normal to return them by value from functions, just like int. BTW, even C
              allows returning structs by value.

              hth
              Paavo

              Comment

              Working...