Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how can this program work correctly

how can this program work correctly

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    carter2000
    wrote on last edited by
    #1

    #include <iostream> using namespace std; class Test { public: Test() { } int GetValue() { return 2; } }; int main() { Test* test = new Test(); cout << test->GetValue() << endl; //test = NULL; delete test; cout << test->GetValue() << endl; system("pause"); return 0; } Output: 2 2 I'm a beginer.

    S _ L _ 4 Replies Last reply
    0
    • C carter2000

      #include <iostream> using namespace std; class Test { public: Test() { } int GetValue() { return 2; } }; int main() { Test* test = new Test(); cout << test->GetValue() << endl; //test = NULL; delete test; cout << test->GetValue() << endl; system("pause"); return 0; } Output: 2 2 I'm a beginer.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      This works because executing GetValue doesn't access any part of a Test object. The function is resolved at compile time. It doesn't access any data members, so the fact that you've deleted the object doesn't affect the execution of the code. But even though this case works, don't do this in production code or anything...please.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      C 1 Reply Last reply
      0
      • C carter2000

        #include <iostream> using namespace std; class Test { public: Test() { } int GetValue() { return 2; } }; int main() { Test* test = new Test(); cout << test->GetValue() << endl; //test = NULL; delete test; cout << test->GetValue() << endl; system("pause"); return 0; } Output: 2 2 I'm a beginer.

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #3

        Check this:

        class Test
        {
        	int p;
        public:
        	Test()
        	{
        		p=2;
        	}
        
        	int GetValue()
        	{
        	return p;
        	}
        	~Test()
        	{
        	}
        };
        
        int main()
        {
        	Test* t1 = new Test();
        	cout << t1->GetValue() <GetValue()<
        

        Beware of the consequences of writing this type of code that you wrote You need to google first, if you have "It's urgent please" mentioned in your question. ;-)**_AnShUmAn_**

        1 Reply Last reply
        0
        • S Stuart Dootson

          This works because executing GetValue doesn't access any part of a Test object. The function is resolved at compile time. It doesn't access any data members, so the fact that you've deleted the object doesn't affect the execution of the code. But even though this case works, don't do this in production code or anything...please.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          C Offline
          C Offline
          carter2000
          wrote on last edited by
          #4

          Well i got it,and thank you for your help.

          1 Reply Last reply
          0
          • C carter2000

            #include <iostream> using namespace std; class Test { public: Test() { } int GetValue() { return 2; } }; int main() { Test* test = new Test(); cout << test->GetValue() << endl; //test = NULL; delete test; cout << test->GetValue() << endl; system("pause"); return 0; } Output: 2 2 I'm a beginer.

            L Offline
            L Offline
            ludi0423
            wrote on last edited by
            #5

            If you are using Visual Studio 2008, you may try #include "stdafx.h" #include using std::cout; using std::endl; class Test { public: Test() { } int GetValue() { return 2; } }; int main() { Test* test = new Test(); cout << test->GetValue() << endl; //test = NULL; delete test; cout << test->GetValue() << endl; system("pause"); return 0; } 1. add the "#include "stdafx.h"" before "include " 2. add the "using std::cout; using std::endl;"

            C 1 Reply Last reply
            0
            • L ludi0423

              If you are using Visual Studio 2008, you may try #include "stdafx.h" #include using std::cout; using std::endl; class Test { public: Test() { } int GetValue() { return 2; } }; int main() { Test* test = new Test(); cout << test->GetValue() << endl; //test = NULL; delete test; cout << test->GetValue() << endl; system("pause"); return 0; } 1. add the "#include "stdafx.h"" before "include " 2. add the "using std::cout; using std::endl;"

              C Offline
              C Offline
              carter2000
              wrote on last edited by
              #6

              I consider the second GetValue would return a weird number, but it works to be 2. now i have got the answer. Thank you for your attention.

              1 Reply Last reply
              0
              • C carter2000

                #include <iostream> using namespace std; class Test { public: Test() { } int GetValue() { return 2; } }; int main() { Test* test = new Test(); cout << test->GetValue() << endl; //test = NULL; delete test; cout << test->GetValue() << endl; system("pause"); return 0; } Output: 2 2 I'm a beginer.

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #7

                It will even work without ever calling the new operator.

                Test* test = NULL;
                test->GetValue();

                Stuart gave you the reason for that.

                «_Superman_» I love work. It gives me something to do between weekends.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups