CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2004
    Posts
    13

    110 percent novice

    I have written a good deal of code in java and now must port a program to C++.
    If I have a method that returns an object in java equivalently I will return a pointer in c++?

    example:
    //java

    public treeNode getNode(){
    return rootNode;
    }

    //C++
    treeNode* getNode(){
    return *rootNode;
    }
    //please pick apart any and all syntactical errors I have made. Thanks!!

  2. #2
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: 110 percent novice

    Quote Originally Posted by hexaplus
    If I have a method that returns an object in java equivalently I will return a pointer in c++?
    Not neccesarily. In C++ you have the choice to return objects by either value (like ints and floats in Java) or by reference (through pointers/references). Which one you choose depends completely on what you are trying to achieve. Generally speaking you should only use pointers if there is a particular reason why returning an actual object won't work. (Like I suspect it is in your case.)

    One thing though... Trying to directly port a piece of code from Java to C++, is almost always a bad idea. Although very similar in syntax, there are a lot of differences between the two languages, most of which greatly impact the way you implement different concepts. Java programmers like yourself do for example have a tendency to be looking for the Object class, and often try to make one themselves if they want a function to take any kind of object. A much better solution would be to take advantage of C++' template facillity, something most of them overlook completely.

    My point is, don't get too caught up in the "Java-way", and try to understand how C++ works instead. Just a friendly piece of advice.
    Quote Originally Posted by hexaplus
    //please pick apart any and all syntactical errors I have made. Thanks!!
    To tell if you have done any syntactical blunders, we will need to see some more code. It all depends on how rootNode is declared.
    Last edited by wien; November 28th, 2004 at 07:50 PM.
    Insert entertaining phrase here

  3. #3
    Join Date
    Nov 2004
    Posts
    13

    Re: 110 percent novice

    mm rootNode is a non-static variable in an instance of treeNode. I guess a better name for the example would be leftNode as in a linked list binary tree.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: 110 percent novice

    Quote Originally Posted by wien
    Java programmers like yourself do for example have a tendency to be looking for the Object class, and often try to make one themselves if they want a function to take any kind of object. A much better solution would be to take advantage of C++' template facillity, something most of them overlook completely.
    Not only that, Java programmers who are trying to port without learning C++ properly believe that "new" in Java is the same as "new" in C++. A sure-fire way to know that a Java programmer has (poorly) written a C++ program is the overuse of "new" all over the place in the program. The program is then full of memory leaks, other run-time programs, and is near impossible to maintain.

    The "do it myself object class" and the overusage of "new"

    http://www.codeguru.com/forum/showth...ht=object+java
    http://www.codeguru.com/forum/showth...ht=object+java

    are the two most obvious mistakes Java programmers make when writing a C++ program.
    My point is, don't get too caught up in the "Java-way", and try to understand how C++ works instead. Just a friendly piece of advice.
    More than friendly advice, it is mandatory that the C++ language is learned correctly, independent of Java.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: 110 percent novice

    Quote Originally Posted by hexaplus
    I have written a good deal of code in java and now must port a program to C++.
    If I have a method that returns an object in java equivalently I will return a pointer in c++?
    No, not necessarily.

    But in general, it is not a good idea to translate from any language to C++ "line-by-line" -- it doesn't work that way. You must actually sit down and learn the language, and then accomplish what you want to do in terms of the C++ language, not in terms of Java, Cobol, Fortran, Basic, or any other language you may have used before.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: 110 percent novice

    Quote Originally Posted by hexaplus
    If I have a method that returns an object in java equivalently I will return a pointer in c++?
    //C++
    treeNode* getNode(){
    return *rootNode;
    }
    You can do this as well:
    Code:
    treeNode getNode(){
      return rootNode;
    }
    But in this case treeNode must have an overloaded = operator:
    Code:
    class treeNode
    {
      treeNode(); // default cst
      treeNode(const treeNode& node){
         // copy node into this
      }
      const treeNode& operator=(const treeNode& rval){
         // copy rval into this
         return *this;
      }
    };
    However, returning a pointer or a reference is faster than returning an object, because no copy of objects is involved.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: 110 percent novice

    Quote Originally Posted by cilu
    But in this case treeNode must have an overloaded = operator:
    Well...actually you only need to supply an overloaded assignment operator, if the class deals with dynamically allocated memory...otherwise simply use the default one provided by the C++ implementation.

    Furthermore, one should always define all three constructors/operators in case of dynamically allocated memory...

  8. #8
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: 110 percent novice

    Quote Originally Posted by hexaplus
    mm rootNode is a non-static variable in an instance of treeNode. I guess a better name for the example would be leftNode as in a linked list binary tree.
    I meant, what type is rootNode? Is it a treeNode, treeNode*, treeNode**, foo*, what?

    Again, if you show us more code, we can help you out a lot better by suggesting solutions to things you might be doing wrong.
    Insert entertaining phrase here

  9. #9
    Join Date
    Jun 1999
    Posts
    153

    Re: 110 percent novice

    Quote Originally Posted by Paul McKenzie
    No, not necessarily.

    But in general, it is not a good idea to translate from any language to C++ "line-by-line" -- it doesn't work that way. You must actually sit down and learn the language, and then accomplish what you want to do in terms of the C++ language, not in terms of Java, Cobol, Fortran, Basic, or any other language you may have used before.

    Regards,

    Paul McKenzie
    Same applies when translating from C++ to any other language. You need to learn the idioms of the other language. Though, typically, going from these other languages to C++ is the more difficult transition.
    Kevin

  10. #10
    Join Date
    Nov 2004
    Posts
    13

    Re: 110 percent novice

    sorry more code it is:


    /** ALP.h
    *
    */
    class ALP
    {
    public:

    treeNode * root;//first node in tree
    treeNode * mom;//temp var for roundUmUp method
    treeNode * curNode;//holds current tree position i
    treeNode * garbage;//temp variable that holds cur
    string ansQuest;//the yes or no answer to distingQ
    string distingQ;//holds a quesiton
    string ansAnimal;//the animal the user chose that was not in the tree
    string guessedAnimal;//animal that curNodes returns from getAnimal method
    bool notDone; //variable that is responsible for exiting startdown loop
    bool cameFrom;// which child curnode


    int main()
    {
    }
    string talk()//performs input from keyboard
    {
    }

    private:

    string play;//the y/n/q var
    int state; //holds int that is returned by treeNode.askQuest
    string userGarbage;//trash var for i/0

    /**
    *Traverses tree starting at staic location root
    *
    *
    * @param none
    * @return void
    */
    void startDown()
    {
    }

    /**
    * Modifies tree so that:
    * curNode is replaced by new non leaf node, garbage.
    * curNode is made a child of the new leaf node.
    * another child(leaf) is created from users response.
    *
    * @param none
    * @return void
    */
    void roundUmUp()
    {
    }
    //--------------------//


    /**
    Creates abstract data types, treeNodes has all necessary methods to modify tree
    *
    * @version Version 1.0
    */
    class treeNode
    {
    public:
    /**
    * An empty constructor for all question nodes
    *
    * @param none
    * @return treeNode
    */
    treeNode ()
    {

    }
    /**
    * A constructor for all end nodes, that adds an end flag on creation.
    *
    * @param int
    * @return treeNode
    */
    treeNode (int blah)
    {
    }
    /**
    * Returns the left child of this.treeNode.
    *
    * @param none
    * @return treeNode
    */
    treeNode getLeft()
    {
    }
    /**
    * Method used to ask the nodes resident question.
    * Returns an int under the following convention:
    * 0 The answer to question was yes and the node is not at the end of the tree.
    * 1 The answer to question was no and the node is not at the end of the tree.
    * 2 The answer to question was yes and the node is a leaf.
    * >=3 The answer was no and the node it s leaf.
    * @param none
    * @return int
    */
    int askQuest()
    {
    }
    /**
    * returns the qeustion of this.treeNode for debuging purposes
    *
    * @param none
    * @return String
    */
    string getQuest(){
    }
    /**
    * Sets this.treeNode's question to the parameter string.
    *
    * @param String
    * @return void
    */
    public void setQuest(String q)
    {
    }
    /**
    * Sets this.treeNode's left child to the parameter.
    *
    * @param treeNode
    * @return treeNode
    */
    treeNode setLeft( treeNode leftVal )
    {
    }
    /**
    * Returns this.treeNode's right child.
    *
    * @param none
    * @return treeNode
    */
    public treeNode getRight( )
    {
    }
    /**
    * Sets this.treeNode's right child to parameter.
    *
    * @param treeNode
    * @return treeNode
    */
    treeNode setRight( treeNode rightVal )
    {
    }
    /**
    * Returns this.treeNode's anima, if no animal is present returns to spaces.
    *
    * @param none
    * @return String
    */
    string getAnimal( )//returns ansAnimal
    {
    }

    string setAnimal(string ani)// non static modifyer method
    {
    }
    private:
    treeNode left; // Reference to the left child
    treeNode right; // Reference to the right child
    boolean end;// variable that denotes if node is leaf
    string quest;// question set by setQuest
    string animal;// animal set by setAnimal method auto sets quest var
    string ans;// temp var for answer

    } // interface treeNode
    Last edited by hexaplus; October 21st, 2005 at 12:53 AM.

  11. #11
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: 110 percent novice

    Heh.. Complete with JavaDoc and everything.

    First off. When posting code in the forums, use the code tags. The code is close to impossible to read now.

    Secondly. This code doesn't even begin to compile. I don't even know were to begin commenting on it. It looks very much like you have simply moved your code over from Java, and made changes from there. That is not the way to go... really...

    I suggest you find some nice C++ tutorials on the web, and start out programming a few "Hello World"ish programs to get a feel for how C++ works, and work your way up from there. Since you say you have written a lot of code in Java, you shouldn't have too much trouble getting the hang of it fairly quickly.
    Insert entertaining phrase here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured