-
Creating an instance of another class? (i think that was java talk)
Hi all, this is my first post so please be nice :)
I'm not a great programmer, but I seem to be able to get through my uni work alright so far, until now. I've hit a snag.
What we did was create this program that would tell you what day of the week any given day from the start of time was. We did this in java, and then were required to convert it to c++. I think I've done everything in that conversion, except one part. One of the classes calls the other class and creates and instance of it. In java I know how to do this, it's:
Code:
Date date = new Date (20, 3, 2009);
But how do I do this in C++? Leaving that line gives me an error:
'conversion from Date* to non-scalar type 'Data' requested'.
I'm pretty sure to complete this section of the practical I just need to change this bit of code, but nothing I've tried has worked, so any help is greatly appreciated.
Thanks
Denno
-
Code:
Date date(20, 3, 2009) ;
-
Vijayan's solution uses stack memory for allocating the object, which is normally the preferred approach in C++. You can also do it with pointers (if you really have to):
Date *date = new Date (20, 3, 2009);
Don't forget that C++ objects are value objects, not aliases to references, as opposed to objects in Java.
Danny Kalev
-
The pointer approach is actually the closer conversion to the Java code (even though you would want to use the stack approach more often in C++). Otherwise you could be in for a shock when you're passing around that variable - that's the warning Danny gave at the end of his reply.
-
Thing to remember: People say Java don't use pointers, truth is it always uses them, but calls them reference instead. Java's '.' operator is C++'s '->' or arrow operator. Only thing with Java is it type-checks the pointers strictly, even with a cast, and checks for null references before calling code. That means, basically, to convert Java code to C++ code as is, for using objects,
Java:
Code:
Object object; // Declaration
Object object = new Object(); // Declaration and initialization
object = new Object(); // Initialization
object.x = val; // Assignment
int val = object.x; // Access
Object val = object.x; // Access
Object val; val = object.x; // Access
val = object.x.y; // Nested access
void method(Object object) {val=object.val} // Method declaration and usage
Object method() {return global_object; } // Method declaration to return object
Equvalent C++ (not the preferred way in C++ most of the time):
Code:
Object *object; // Declaration
Object *object = new Object(); // Declaration and initialization
object = new Object(); // Initialization
object->x = val; // Assignment
int val = object->x; // Access
Object *val = object->x; // Access
Object *val; val = object->x; // Access
val = object->x->y; // Nested access
void method(Object *object) {val=object->val} // Method declaration and usage
Object* method() {return global_object; } // Method declaration to return object
The preferred way in C++ is to not use these pointer stuff, unless its actually necessary.
-
and one other thing to remember: Java objects are memory managed (garbage collected) C++ objects allocated on the heap are *generally not*, which means objects allocated with new or new[] have to be de-allocated with delect and delete[] respectively.
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
Similar Threads
-
By Osiris43 in forum .NET
Replies: 1
Last Post: 08-04-2006, 12:15 PM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
Replies: 1
Last Post: 06-06-2001, 11:29 AM
-
Replies: 2
Last Post: 10-12-2000, 06:41 AM
-
Replies: 3
Last Post: 06-09-2000, 08:18 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks