STL & Copy constructor..help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Naren

    STL & Copy constructor..help

    Hello All,

    I get an unexpected behaviour for this

    class D{

    /*something*/
    };

    class B
    {
    D *ptr;
    /* something more */
    };

    class A
    {
    vector<B> Blist;

    };
    in some function of class A
    for(i=0;i<n;i++ )
    {
    B obj;
    Blist.push_back (obj); //throws an exception
    }

    i have defined copy constructors for all the classes and used explicit
    keyword also in the declaration

    I think STL makes a copy using this copy constrcutor to store internally.Plz
    throw some light

    Thaanx in advance
    Rgds,
    Naren.





  • Jim Fischer

    #2
    Re: STL &amp; Copy constructor..he lp

    Naren wrote:[color=blue]
    > i have defined copy constructors for all the classes and used explicit
    > keyword also in the declaration[/color]

    Comment 1) Are the assignment operator '=' methods for classes D and B
    implemented correctly?

    Comment 2) There is not enough information in your original post to
    determine the actual problem. You really need to post the source code
    for a short, complete, compilable program that excatly demonstrates the
    problem you're describing. If we don't have some actual code to look at,
    it's anyone's guess as to what the actual problem(s) might be.

    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link58 09{at}now.here. com


    Comment

    • John Harrison

      #3
      Re: STL &amp; Copy constructor..he lp


      "Naren" <narendranath.t s@in.bosch.com> wrote in message
      news:bimhvm$cvp $1@ns2.fe.inter net.bosch.com.. .[color=blue]
      > Hello All,
      >
      > I get an unexpected behaviour for this
      >
      > class D{
      >
      > /*something*/
      > };
      >
      > class B
      > {
      > D *ptr;
      > /* something more */
      > };
      >
      > class A
      > {
      > vector<B> Blist;
      >
      > };
      > in some function of class A
      > for(i=0;i<n;i++ )
      > {
      > B obj;
      > Blist.push_back (obj); //throws an exception
      > }
      >
      > i have defined copy constructors for all the classes and used explicit
      > keyword also in the declaration
      >
      > I think STL makes a copy using this copy constrcutor to store[/color]
      internally.Plz[color=blue]
      > throw some light[/color]

      Yes the STL makes a copy using your copy constructor.

      If you have defined your copy constructors correctly then this wouldn't be a
      problem. Please post the code you are really using.
      [color=blue]
      >
      > Thaanx in advance
      > Rgds,
      > Naren.
      >[/color]

      john


      Comment

      • Kevin Goodsell

        #4
        Re: STL &amp; Copy constructor..he lp

        Naren wrote:
        [color=blue]
        > Hello All,
        >
        > I get an unexpected behaviour for this
        >
        > class D{
        >
        > /*something*/
        > };
        >
        > class B
        > {
        > D *ptr;
        > /* something more */
        > };
        >
        > class A
        > {
        > vector<B> Blist;
        >
        > };
        > in some function of class A
        > for(i=0;i<n;i++ )
        > {
        > B obj;
        > Blist.push_back (obj); //throws an exception[/color]

        What exception?
        [color=blue]
        > }
        >
        > i have defined copy constructors for all the classes and used explicit
        > keyword also in the declaration[/color]

        I don't see any copy constructors.
        [color=blue]
        >
        > I think STL makes a copy using this copy constrcutor to store internally.Plz
        > throw some light[/color]

        The standard containers may copy using copy constructors or copy
        assignment. Objects you store in them must support both operations with
        the usual semantics.

        Your problem is not apparent from what you posted. Please post a minimal
        complete program that demonstrates the problem. You should *always* do
        this when asking a question here.

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        Working...