Construct followed by setter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vainstah
    New Member
    • Mar 2008
    • 8

    #1

    Construct followed by setter

    Hello Guys and Gals,

    Been trying to throw as follows.

    throw MyException("ba d exception OMG").inputValu e("value");

    I observed a strange error in my code the hypothetical MyException class has a pointer which needs to be "new" allocated. The problem was that said pointer was not initialized.

    This leads me to believe that copying is going on and the copy constructor is not defined by my code. There has to be a way around the copy constructor in the first place.

    I have tried

    class MyException
    {
    char * value;
    .
    . //My Exception Constructor
    .
    MyException & inputValue(char * value_in);
    { value=value_in ; return *this}
    }

    and

    class MyException
    {
    char * value;
    .
    . //My Exception Constructor
    .
    MyException inputValue(char * value_in);
    { value=value_in ; return *this}
    }

    What signature should I be using ? if you tell me I need to use a constructor than I might as well move the setter into the constructor.

    Regards

    Vain
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by vainstah
    I observed a strange error in my code the hypothetical MyException class has a pointer which needs to be "new" allocated.
    That is not what the posted code does in fact new is not called anywhere.

    Putting a char * in an exception (or any class really) and passing in a pointer to assign to it is highly dangerous. What type of memory is that pointer pointing to? Who is responsible for deallocting it if that is required.

    Memory management needs to be fully encapsulated in the class so that it can be rigorously consistent. That means the class has to handle allocatings data for the memory and initialising the data from the provided parameter. You have to assume that the passed parameter only exists for the time of the method call.

    Since you are using a string this is rather simply solved for you by using a string rather than a char *.

    Comment

    • vainstah
      New Member
      • Mar 2008
      • 8

      #3
      With all due respect. you looked at the (psuedo) code and not the question, the char * shown in the object is really a const char * (as it only sets char * to hardcoded strings) and the new is not shown in the snippit because I want to know what signature to use to avoid the default copy constructor being called.

      What I want to do is use a constructor and then use a function to further initialize it in one statement. The problem is with the copy constructor being called between.

      MyException("So me Exception")

      and the setter function

      MyException("So me Exception").set Value(char * someValue)

      I have tried two signatures (mentioned in my previous post) which I think should not cause a copy.

      I was to construct using the common parameters of the base class MyException and define setValue depending on the requirements of the derived class.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I see no reason why the copy constructor would be called between calling the constructor and calling setValue regardless of the signiture of setValue which should have no effect.

        I wouldn't be entirely surprised of the throw was calling the copy constructor. You might more easily determine exactly what is going on by not doing everything on 1 line i.e. rather than

        throw MyException("ba d exception OMG").inputValu e("value");

        use

        Code:
        MyException me("bad exception OMG");
        me.inputValue("value");
        throw me;
        However the loss of pointer you are reporting is indicative of calling the default copy constructor somewhere and to my knowledge the only way of preventing this is to write your own copy constructor and correctly copy any pointers contained in the class.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by vainstah
          This leads me to believe that copying is going on and the copy constructor is not defined by my code. There has to be a way around the copy constructor in the first place.
          There is no way around a copy constructor. If you don't have one then a default copy constrcutor is always provided by the compiler. That default copy constructor calls the copy ctor on each data member of your class. Your pointer, therefore, is merely copied complete with the bad address.

          This situation is precisely why a copy ctor is in C++ in the first place.

          In your case, make sure that pointer is initialized somehow before you attempt to copy the object. That way the compiler's default copy constrcutor will be OK for you.

          Comment

          Working...