Results 1 to 19 of 19

Thread: Weird thing while trying to avoid pointers in C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Weird thing while trying to avoid pointers in C++

    I had this weird thing while I was trying to avoid pointers and just create objects on the stack.

    Qt Code:
    1. #include <iostream>
    2. #include <string>
    3. #include <cstring>
    4. #include <vector>
    5. #include <cstdio>
    6. using namespace std;
    7.  
    8. class Node {
    9. public:
    10. string move;
    11. int state[10];
    12. Node(const Node &p) {
    13. move = "";
    14. memcpy(state,p.state,sizeof(int)*10);
    15. }
    16. Node(){
    17. move = "";
    18. for(int i = 0;i<10;i++){
    19. state[i] = i;
    20. }
    21. }
    22. void AddNode(Node p){
    23. this->childNode.push_back(p);
    24. }
    25. Node GetChild(int i) {
    26. return childNode[i];
    27. }
    28. int GetCount() {
    29. return this->childNode.size();
    30. }
    31. protected:
    32. vector<Node> childNode;
    33. };
    34.  
    35. class Move {
    36. public:
    37. Move() {
    38.  
    39. }
    40. void run() {
    41. Node n;
    42. n.move = "A100";
    43. operate(n);
    44. }
    45.  
    46. void operate(Node &n) {
    47.  
    48. Node c(n);
    49. c.move = "B100";
    50. n.AddNode(c);
    51. cout << "Output : " << endl;
    52. Node t = n.GetChild(n.GetCount() - 1);
    53. //Node t = n.childNode[0];
    54. for(int i = 0;i<10;i++)
    55. cout << t.state[i];
    56.  
    57.  
    58. cout << endl << t.move; // doesn't display B100 ????
    59.  
    60. }
    61.  
    62. };
    63.  
    64. int main(int argc,char** argv) {
    65.  
    66. Move m;
    67. m.run();
    68. return 0;
    69. }
    To copy to clipboard, switch view to plain text mode 

    Here is a sample of something that I'm trying to do. My question is why isn't B100 move displayed ? Do I need an assignment operator in the Node class ? I believed push_back takes references and that the node with move value B100 should be stored in the vector. Correct state array is stored but the move string is not, any suggestions ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Weird thing while trying to avoid pointers in C++

    ... where to begin...
    believed push_back takes references and that the node with move value B100 should be stored in the vector.
    Programming is not a religion.
    You don't believe stuff, you read the docs!
    Since your definition of your vector was not with reference, it takes copies, not references.
    I am not sure even why this code compiles, since you are trying to assign c to t but you didn't define an '=' operator for your Node class.
    Qt Code:
    1. Node t = n.GetChild(n.GetCount() - 1); //I don't know why this compiles, it should not
    To copy to clipboard, switch view to plain text mode 
    maybe a default assignment operator is called, but it knows nothing about your Node members, so it can't possibly copy inner members of your Node members such as 'move'.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Weird thing while trying to avoid pointers in C++

    Well it does take reference.

    Qt Code:
    1. void push_back ( const T& x );
    To copy to clipboard, switch view to plain text mode 

    However, even if it takes copy, shouldn't there be a value of move in the vector ?

    I added the assignment operator
    Qt Code:
    1. Node& operator=(const Node &r) {
    2.  
    3. if(&r != this) {
    4. memcpy(this->state,r.state,sizeof(int)*10);
    5. this->move = r.move;
    6.  
    7. }
    8.  
    9. return *this;
    10. }
    To copy to clipboard, switch view to plain text mode 

    But still There is no output.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Weird thing while trying to avoid pointers in C++

    Take a look at your copy constructor:
    Qt Code:
    1. Node(const Node &p) {
    2. move = ""; //!< change this to p.move
    3. memcpy(state,p.state,sizeof(int)*10);
    4. }
    To copy to clipboard, switch view to plain text mode 
    When you take out an object from vector, it's copy is made and previously assigned string is cleared in your copy-constructor.

    ------------------
    ok, maybe i was not clear enough, i mean this line:
    Qt Code:
    1. Node t = n.GetChild(n.GetCount() - 1);
    To copy to clipboard, switch view to plain text mode 
    this is copy - construction, its not the same as:
    Qt Code:
    1. Node t;
    2. t = n.GetChild(n.GetCount() - 1);
    To copy to clipboard, switch view to plain text mode 
    Assignment operator is not called anywhere in your code ( add cout's to see it yourself ).
    Last edited by stampede; 14th February 2011 at 09:41. Reason: additional comment

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Weird thing while trying to avoid pointers in C++

    Well it does take reference.
    That is true, however think about it:
    If it really only took a reference, and left it as a reference, all the original objects you would assign to it would need to stay alive, which is not the case.
    So it takes a reference, but internally it creates a copy.
    Last edited by high_flyer; 14th February 2011 at 10:20. Reason: removed wrong information
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Weird thing while trying to avoid pointers in C++

    Quote Originally Posted by high_flyer View Post
    Unless you define the vector as QVector<MyObject&> - then it indeed will take references.
    I'm pretty sure you can't do that, since references in C++ have their "oddies" (like the reference is for all it's life bound to an object and it should be set at declaration)

    So the remaining solution is QVector<MyObject*>

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Weird thing while trying to avoid pointers in C++

    You are correct Zlatomir.
    I have never tried it before actually, because it never made any practical sense.
    I just assumed it is possible.
    Now I tried it, and indeed, the compiler complains.
    Thanks for the heads up!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Weird thing while trying to avoid pointers in C++

    Quote Originally Posted by high_flyer View Post
    I am not sure even why this code compiles, since you are trying to assign c to t but you didn't define an '=' operator for your Node class.
    Qt Code:
    1. Node t = n.GetChild(n.GetCount() - 1); //I don't know why this compiles, it should not
    To copy to clipboard, switch view to plain text mode 
    This is equivalent to:
    Qt Code:
    1. Node t(n.GetChild(n.GetCount()-1));
    To copy to clipboard, switch view to plain text mode 
    The copy constructor kicks in here. Besides if you don't declare your own default constructor, copy constructor or assignment operator, the compiler will provide them for you.


    Quote Originally Posted by ct View Post
    Well it does take reference.

    Qt Code:
    1. void push_back ( const T& x );
    To copy to clipboard, switch view to plain text mode 
    No, it takes a const reference. That's two totally different things.
    Last edited by wysota; 14th February 2011 at 19:01.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Weird thing while trying to avoid pointers in C++

    Thanks, I got it. The copy constructor was being called all the time. I got the difference now.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  10. #10
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Weird thing while trying to avoid pointers in C++

    One more thing on the topic though. Is it a good practice to avoid using pointers and use the stack space instead of heap ? That way there would be no memory leaks.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  11. #11
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Weird thing while trying to avoid pointers in C++

    No; that will quickly lead to convoluted code in all but the simplest applications. Pointers are fundamental to C/C++; there is no reason to avoid them.

    If you're really worried about memory leaks, have a look at the STL auto_ptr class. Or run your code through a tool like valgrind to check for memory leaks, which is something you really ought to be doing anyway.

Similar Threads

  1. KDChart. What do you thing about?
    By Nadia in forum Qt-based Software
    Replies: 1
    Last Post: 15th February 2011, 01:55
  2. doing a thing after the form is displayed
    By franco.amato in forum Newbie
    Replies: 3
    Last Post: 26th January 2011, 05:05
  3. Very strange thing
    By Sheng in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2008, 22:44
  4. There is QTimeEdit another thing in Qt4
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2006, 16:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.