CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Feb 2004
    Posts
    223

    std::Vector push_back

    i want to know what happens when push_back is called?


    Code:
    class A
    {
    public:
    	int n;
    	int *pn;
    public:
    	A(void)
    	{
    		n = 10;
    		pn = new int[n];
    	}
    	~A(void){ delete [] pn; }
    };
    
    vector<A> B;
    A         a;
    B.reserve(10);
    B.clear();
    B.push_back(a);
    do i need to override the operator=?

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    For your class yes, since your class is not copyable by default.

    A class is copyable with the auto-generated copy constructor if it only contains PODs (plain old datatypes like int, double and char) or copyable objects. A pointer to memory is not in there so you'll have to define your own copy constructor and assignment operator. In general it's a very good indication that if you need a destructor, you also need explicit copy semantics.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Join Date
    Feb 2004
    Posts
    223
    thanks Yves M for so quick reply!

    i want to know if i dont define copy contructor, what will be happen when calling push_back and pop_back.

    and can you give a easy example to show how to define a class with copy constructor and operator= . and what is basic rules to define a class with more than POD data types? sorry for my ignorance, i seldem define copy constructor though i know maybe i should define it, because i dont know in which circumstance i should define it. and i seldem override operator= for the same reason. can you give me a elegant way to solve the problem,thanks.

  4. #4
    Join Date
    May 2004
    Posts
    340
    yeah,you can define a copy constructor in this way:
    inside the class
    classname (classname&objectname);
    or outside the class
    classname::classname(classname&objectname)
    {
    implementation

    }
    its main use is to cpoy old objects to new objects
    as for opertor = it has something with the polymorphism
    it is a way of polymorphism
    you can define it in this way :
    opertaor =(class/struct/union objectname) and it targets the class/struct/union object .it provides conveince for operations in the main function ,it cannot run pod data.

  5. #5
    Join Date
    May 2004
    Posts
    340
    yeah ,you can use it in this way :
    copy constructor
    inside the class
    classname (classname&objectname)
    outside the class
    classname::classname(classname&objectname)

    its main advantage is to use the old objects to give value to new objects .
    opertor =
    it is a way of polymorphism it often treat class/struct/union object
    the parameter is not pod,and it provides conveince for the main function

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by fantasy2004
    i want to know if i dont define copy contructor, what will be happen when calling push_back and pop_back.
    Well...the problem will occur while destroying the classes...as soon as your class will be copy-constructed or assigned, the pointer to your dynamically allocated memory will simply be copied...thus, resulting in two or more class instances pointing to the same allocated memory...if every instance tries to delete the memory...crash...boom...bang...

    Originally posted by fantasy2004
    and can you give a easy example to show how to define a class with copy constructor and operator= . and what is basic rules to define a class with more than POD data types?
    By default the copy constructor and assignment operator are making so-called shallow copies of the object meaning that all of the member values will be copied. This works well if the members are values, but causes problems for members which actually points to dynamically allocated memory. The pointer will be copied - but not the memory it points to...this will result in having both members pointing to the same dynamically allocated memory.

    Therefore, if you have a class with dynamic memory allocation a deep copy is required to guarantee that all members gets copied properly. To make a deep copy, you must write a copy constructor and overload the assignment operator.
    Code:
    class CFoo()
    {
    public:
      // Constructor
      CFoo() { m_pArray = new char&#091;100&#093;; }
    
      // Destructor
      ~CFoo() { delete &#091;&#093; m_pArray; }
    
      // Copy constructor
      CFoo(const CFoo &refcSource)
      {
        // Allocate new space
        m_pArray = new char&#091;strlen(refcSource.m_pArray) + 1&#093;;
    
        // Copy values
        strcpy(m_pArray, refcSource.m_pArray);
      }
    
      // Assignment operator
      CFoo& operator=(const CFoo &refcSource)
      {
        // Check whether it is the same instance
        if(&refcSource != this)
        { 
          // Release old memory
          delete &#091;&#093; m_pArray;
    
          // Allocate new space
          m_pArray = new char&#091;strlen(refcSource.m_pArray) + 1&#093;;
    
          // Copy values
          strcpy(m_pArray, refcSource.m_pArray);
        }
    
        return *this;
      }
    
    private:
      char *m_pArray;
    };

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by jolley
    classname (classname&objectname)
    It should rather be
    Code:
    classname (const classname& rhs)
    Originally posted by jolley
    opertor =
    it is a way of polymorphism it often treat class/struct/union object
    the parameter is not pod,and it provides conveince for the main function
    Well..the assignment operator has nothing to do with polymorphism...but simply is a different way of creating an instance of the class (other than construct or copy-construct)...

  8. #8
    Join Date
    May 2004
    Posts
    340
    thanks Andreas Masur
    you teach me a lot !

  9. #9
    Join Date
    Jul 2004
    Posts
    5

    the copy constructor is needn't...

    the copy constructor is needn't, if you save the pointer in the vector only .

    vector<A*> vtr;
    A *pA = new A();
    vtr.push_back(pA); //It's safety!

    remember, you need to free the memory by yourself:

    vector<A*>::iterator it = vtr.begin();
    for(;it!=vtr.end();it++)
    {
    delete *it; //(*it)==pA
    }
    Last edited by SkyMountain; July 2nd, 2004 at 02:20 AM.

  10. #10
    Join Date
    Feb 2004
    Posts
    223
    thanks jolley,Andreas Masur and SkyMountain:

    you teach me a lot !

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by fantasy2004
    thanks Yves M for so quick reply!

    i want to know if i dont define copy contructor, what will be happen when calling push_back and pop_back.
    You're concentrating too much on what a vector will do with your object. It isn't vector that is the problem -- a simple program that doesn't use vector will cause the same problems:
    Code:
    int main()
    {
        A   A1;
        A   A2 = A1;  // copy constructor (problem)
        A   A3;
        A3 = A1;   // assignment - another problem
    }   // destructor for A1, A2, and A3, yet another problem
    Your class doesn't even work for the code I have above. There is no vector at all in the code I posted. This is why you define a copy constructor and assignment operator (and destructor), so that simple operations such as I have above work correctly.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Just for completeness:
    Code:
    class foo
    {
    public:
        foo(foo&);
        foo(const foo&);
        foo(volatile foo&);
        foo(const volatile foo&);
    };
    are all valid copy constructors...
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  13. #13
    Join Date
    Feb 2004
    Posts
    223
    thanks Paul McKenzie

    you are absolutely right. can you give me some constructive suggestion such as the common rules to deal with this kind of problem, i can read c++ codes and try to understand it step by step, but i lack the ability to write it on my own.

    and thanks Graham.

    Code:
    class foo
    {
    public:
        foo(foo&);
        foo(const foo&);
        foo(volatile foo&);
        foo(const volatile foo&);
    };
    i dont very sure about the "Just for completeness".
    do you mean it is just for completeness in theroy and it is not practical?
    or it is complete in every circumstance?
    and i want to know what others things i should do besides copy constructor?
    thanks for your kindness!

  14. #14
    Join Date
    Jan 2001
    Posts
    588
    Originally posted by fantasy2004
    thanks Paul McKenzie

    you are absolutely right. can you give me some constructive suggestion such as the common rules to deal with this kind of problem, i can read c++ codes and try to understand it step by step, but i lack the ability to write it on my own.
    Andreas Masur already did that for you. He spelled out exactly what you need to do: write a copy constructor and overload the assignment operator for your class.

  15. #15
    Join Date
    Feb 2004
    Posts
    223
    thanks Bob Davis

    Andreas Masur already did that for you. He spelled out exactly what you need to do: write a copy constructor and overload the assignment operator for your class.
    i want to know what others things i should do besides copy constructor and assignment operator?
    or in other words, it's enough to write a copy constructor and overload the assignment operator for your class?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured