Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 25-10-2008
Member
 
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.

Reply With Quote
  #2  
Old 25-10-2008
Member
 
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
}
Reply With Quote
  #3  
Old 25-10-2008
Member
 
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.
Reply With Quote
  #4  
Old 25-10-2008
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Object test = new Object() <-- Java, best way in C++"
Thread Thread Starter Forum Replies Last Post
get classes and object in java preeti makkar Software Development 1 07-05-2012 11:08 AM
Getting HP AIO Device object server register class object failed message on my system Donoho Hardware Peripherals 6 06-06-2011 01:34 AM
Scope of Object or variable in Object oriented Programming Languages Dėfrim Software Development 3 08-01-2011 05:20 AM
Differents between object and static object Sarfaraj Khan Software Development 5 29-01-2010 12:11 PM
Object reference not set to an instance of an object KAIRU26 Software Development 3 05-09-2009 08:14 PM


All times are GMT +5.5. The time now is 07:29 PM.