DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009

    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:

    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.


  2. #2
    Join Date
    Dec 2007
    Date date(20, 3, 2009) ;

  3. #3
    Join Date
    Nov 2003
    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

  4. #4
    Join Date
    Jul 2004
    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.
    David Anton
    Convert between VB, C#, C++, & Java
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  5. #5
    Join Date
    Mar 2007
    Bangalore, India
    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,
    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):
    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.

  6. #6
    Join Date
    Jan 2005
    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.
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd

Similar Threads

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  2. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  3. How does ASP call Java class
    By Sam in forum ASP.NET
    Replies: 1
    Last Post: 06-06-2001, 11:29 AM
  4. Replies: 2
    Last Post: 10-12-2000, 06:41 AM
  5. private and static
    By Mark in forum Java
    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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.