• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Collapse

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "C++ technical question"

Collapse

  • BlasterBates
    replied
    It´s an old Beta version of Visual Studio, which doesn´t understand NULL and so set it to 0. Anyway it works with that online compiler. Results as expected. My faith in C++ has been restored.

    gr8 stuff, thx.

    Leave a comment:


  • VectraMan
    replied
    Just tried it in VC8 for you, and the destructor is only called once. Perhaps the problem is with the other code you're not showing us?

    You need a return type for the function (i.e. void) in VC8 otherwise it'll moan.

    Leave a comment:


  • BlasterBates
    replied
    Yes thx for that.

    ....of course a pointer to a pointer as it were.

    That online compiler is very useful.

    I´m using Microsoft Visual Studio C++ compiler which gives me that unexpected result of the destructor being called twice even though I set the ptr to NULL (when using *&).

    Leave a comment:


  • d000hg
    replied
    Which 'crappy' MS compiler are we talking here?

    Have you sanity-tested your code on any of these: https://www.google.co.uk/search?q=on...2B%2B+compiler

    Leave a comment:


  • VectraMan
    replied
    It'll call Kill() twice, but the second time ptr is NULL and so delete ptr will do nothing. Is that what you're saying is happening?

    If Kill just takes a T* then ptr=NULL is only changing the local pointer, not the original, so the second call will try to delete an object that has already been deleted, and bang.

    Leave a comment:


  • BlasterBates
    started a topic C++ technical question

    C++ technical question

    Given this code:

    template <class T> Kill (T *& ptr)
    {
    delete ptr;
    ptr = NULL;

    };

    class MyClass{};

    int main()
    {
    MyClass* myClass = new MyClass;
    Kill(myClass);
    Kill(myClass);
    }

    What should happen here?

    With my (crappy) Microsoft compiler I executed this and it destructs twice when I have *& but generates an exception when I use * instead of *& in the template definition.

    Surely when the pointer is set to NULL calling delete the second time should have no effect. Why was it executed twice with *&

    Is it so that the above code should just execute without error and delete the object only once?
    Last edited by BlasterBates; 26 March 2013, 16:13.

Working...
X