Class members that MUST be initialized by temporary objects in amember initialization list

Discussion in 'C++' started by Olumide, Dec 6, 2007.

  1. Olumide

    Olumide Guest

    Hello C++ gurus,

    I've got this basic class thats the backbone of my project and I
    frequently use use an instance of it to initialize other objects like
    so. However, my latest class, a simple assignment in a constructor did
    not work, like so:

    class VeryBasicClass{
    public:
    VeryBasicClass(){};
    };

    class LatestClass{
    public:
    LatestClass( VeryBasicClass &vbo );
    private:
    VeryBasicClass veryBasicObject;
    };

    LatestClass::LatestClass( VeryBasicClass &vbo )
    {
    veryBasicObject = vbo; // this does not work
    // halts plugin execution
    }

    The following constructor caused the environment (I'm developing a
    plugin) to crash with a BLOCK_TYPE_IS_VALID assert

    LatestClass::LatestClass( VeryBasicClass &vbo ):veryBasicObject( vbo )
    {
    }

    The following constructor however worked

    LatestClass::LatestClass( VeryBasicClass
    &vbo ):veryBasicObject( VeryBasicObject() )
    {
    }

    I would know why the first why the first two constructors failed, and
    the only the third works, and I'd hate to be in the dark about these
    things. Its better to know whats going on in case something goes
    wrong.

    All thats unique about VeryBasicClass is that itself contains a member
    object that must be initialized by a temporary object in a member
    initialization lists of a constructor. I'm not the author of the
    members class (its part of the plugin API).

    Thanks

    - Olumide
     
    Olumide, Dec 6, 2007
    #1
    1. Advertisements

  2. * Olumide:
    It seems that at the end of writing the above, in the very last
    paragraph, a glimmer of insight started to surface. Try to follow that
    thought. All that came before is worthless for diagnosing what the
    technical problem is.

    Cheers, & hth.,

    - Alf
     
    Alf P. Steinbach, Dec 6, 2007
    #2
    1. Advertisements

  3. Olumide

    Andre Kostur Guest

    To paraphrase your answer: "The only interesting part of VeryBasicClass
    is the part I'm not going to show you".

    Provide a _minimal_, _compilable_ example which shows your problem.
     
    Andre Kostur, Dec 6, 2007
    #3
  4. Olumide

    yurec Guest

    I think you know, but to be sure : you'll get slicing while copying
    VeryBasicClass ( as it's value in your member list)
    Maybe something is missed in copying VeryBasicClass?
     
    yurec, Dec 7, 2007
    #4
  5. Olumide

    James Kanze Guest

    Hmmm. It doesn't cause any problems in my environment. Are you
    sure you're showing us the complete code, and that
    VeryBasicClass is really that simple.
    I'm not that sure. What's the difference between the last two?
    About the only thing I can see is that there is something wrong
    in the copy constructor of VeryBasicClass, and that the compiler
    optimizes the actual call of the copy constructor out in the
    last case.

    The fact remains that the code he has posted is fine. But since
    the code he has posted isn't the code which is causing him
    problems, that doesn't help us (or him) much.
     
    James Kanze, Dec 9, 2007
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.