|
| ||||||||||
| Tags: java |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Object test = new Object() <-- Java, best way in C++
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
| ||||
| ||||
| 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
| ||||
| ||||
| 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
| |||
| |||
| 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.
} allowable optimizations and some funny constructor syntax multiple inheritance and virtual inheritance and how they affect the way objects are constructed and destroyed. |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Object test = new Object() <-- Java, best way in C++" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 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 |
| Adding an object in java | Gunner 1 | Software Development | 5 | 27-02-2010 02: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 |