Results 1 to 4 of 4

Thread: Object test = new Object() <-- Java, best way in C++

  1. #1
    Join Date
    Oct 2008
    Posts
    89

    Object test = new Object() <-- Java, best way in C++

    In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want.

    could someone please help me understand the different ways to do the same thing in C++. I find my self sometimes, trying

    Object app = Object();
    Object *app = Object();
    Object app = new Object();

    randomly till something works... and this is bad because I am confused what is happening, and the differences. Someone please help...

    As a java programmer, I really like the new keyword.

  2. #2
    Join Date
    May 2008
    Posts
    71

    Re: Object test = new Object() <-- Java, best way in C++

    Have you tried a C++ book? If yes, which one? If not, why not?

    C++ is a complicated language that cannot be simply guessed. If you really want to learn C++, _study_ it.

    Code:
    class Object {};
    int main()
    {
    Object obj; // declares and defines an instance of Object
    // that has _automatic_ storage duration and
    // will go away when the block in which it is
    // defined, closes
    
    Object *pobj = new Object; // declares and defines a pointer
    // to Object and initialises it
    // by creating a _dynamic_ object
    // that will need to be destroyed at
    // the end of your program by using
    delete pobj; // ... the 'delete' keyword
    }

  3. #3
    Join Date
    May 2008
    Posts
    115

    Re: Object test = new Object() <-- Java, best way in C++

    It's also bad because you can't always tell that something isn't
    working. Things might look like they're working when in fact you have
    memory leaks or you are accessing memory for an object which has been
    destroyed but whose storage hasn't been cleaned up.

    You really need to learn the basic rules of the language before you
    start programming. C++ is designed so that you can start programming
    before you are familiar with each language feature, but you certainly
    can't jump in from Java with no C++ background and hope to know what's
    going on.

  4. #4
    Join Date
    Oct 2008
    Posts
    24

    Re: Object test = new Object() <-- Java, best way in C++

    One of the assets of C++ is the ability to explicitly control how an object is created and destroyed (some people call this a liability but we know better). However C++ allows you to create some very interesting code because of this but you don't get there unless you understand all of the ways the compiler manages your objects.

    I wrote some code below that shows most of the ways objects are created
    and destroyed and some common errors.

    Code:
    struct Thing
    {
    int life_determined_by_Thing;
    };
    
    Thing globally_allocated_thing;
    
    Thing * globallay_allocated_pointer_to_thing;
    
    Thing * FunctionReturnsPointerToThing()
    {
    
    static Thing statically_allocated_constructed_on_first_call;
    // this object dies on exit of the process (or
    // unloading of function)
    
    return & statically_allocated_constructed_on_first_call;
    }
    
    void DoThings()
    {
    
    Thing auto_allocated_thing_dies_on_return;
    
    // The thing below dies when explicity delete ed
    Thing * pointer_to_thing = new Thing;
    
    return;
    // pointer_to_thing is gone and the Thing it was
    // pointing to can't be deleted any more - a memory
    // leak.
    }
    
    void DoMore()
    {
    
    // THIS IS REALLY REALLY BAD - unpredictable and likely
    // bad behaviour happens here
    delete FunctionReturnsPointerToThing();
    }
    
    void DoAnotherThing( Thing temporary_copy )
    {
    // take the address of the parameter
    Thing * pointer_to_thing = & temporary_copy;
    
    // temporary_copy is destroyed after return from
    // DoAnotherThing()
    }
    
    
    void DoMoreThings( Thing & reference_to_passed_variable )
    {
    // reference_to_passed_variable "points" to the parameter
    // that is passed into this function.
    
    // This makes a temporary thing and destroys it once
    // the expression is complete
    Thing();
    }
    
    Thing * ImaBroken()
    {
    Thing auto_allocated_thing_dies_on_return;
    
    return & auto_allocated_thing_dies_on_return;
    // OOPS - a stale pointer is being returned - the caller
    // will get a surprise when they try to use the return value.
    }
    and I haven't covered lvalues, references, const references,
    allowable optimizations and some funny constructor syntax multiple
    inheritance and virtual inheritance and how they affect the way objects
    are constructed and destroyed.

Similar Threads

  1. get classes and object in java
    By preeti makkar in forum Software Development
    Replies: 1
    Last Post: 07-05-2012, 11:08 AM
  2. Replies: 6
    Last Post: 06-06-2011, 01:34 AM
  3. Replies: 3
    Last Post: 08-01-2011, 06:20 AM
  4. Differents between object and static object
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 01:11 PM
  5. Object reference not set to an instance of an object
    By KAIRU26 in forum Software Development
    Replies: 3
    Last Post: 05-09-2009, 08:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,656,431.93822 seconds with 17 queries