Re: Why copy constructor is not called

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Aaron Queenan (aqueenan_DieSpammerDie__at_contingent.com.au)
Date: 02/09/04


Date: Mon, 9 Feb 2004 17:07:03 +0100

Copy constructors should use the syntax "Class(const Class& rhs)".
Similarly, the assignment operator should take const reference arguments.

Regards,
Aaron Queenan.

"Toma Bussarov" <admin@grao.government.bg> wrote in message
news:6C5BDE2D-3895-42B1-9994-0CA9E11E3BF5@microsoft.com...
> Hi, here is a sample program that I'm wondering why copy constroctor is
not called, but instead the compiler prefers to cast the temp. object to
int, and then from this int using the int constructor to build new object
>
> #include <iostream>
>
> class CToken
> {
> private:
> int m_nIntVal;
> public:
> CToken()
> {
> std::cout<<"Inside CToken() - default ctor"<<std::endl;
> }
> CToken(CToken& tok)
> {
> m_nIntVal = tok.m_nIntVal;
> std::cout<<"Inside CToken(CToken&) - copy ctor"<<std::endl;
> }
> CToken& operator = (CToken& tok)
> {
> m_nIntVal = tok.m_nIntVal;
> std::cout<<"Inside CToken& operator =(CToken&) - assignment
operator"<<std::endl;
> return *this;
> }
> CToken (int i)
> {
> m_nIntVal = i;
> std::cout<<"Inside CToken(int) - ctor with int param"<<std::endl;
> }
> operator int ()
> {
> std::cout<<"Inside operator int() - int casting operator"<<std::endl;
> return m_nIntVal;
> }
> ~CToken()
> {
> std::cout<<"Inside dtor ~CToken()"<<std::endl;
> }
> };
>
>
> CToken func1()
> {
> std::cout<<"Inside func1()"<<std::endl;
>
> return CToken(1);
> }
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> std::cout<<"Inside main(...)"<<std::endl;
>
> CToken mainTok;
>
> std::cout<<"mainTok object constructed"<<std::endl;
>
> mainTok = func1();
>
> std::cout<<"Exiting from main()"<<std::endl;
>
> return 0;
> }
>
>
> Yes, I know that non-const params, but this is Level 4 warning in VC++,
and since it is only warning should work



Relevant Pages

  • Exception safe at what cost
    ... Programming with Exceptions. ... int sz; ... This is a bad assignment operator implementation because ... potentially usable memory before allocating the new array. ...
    (comp.lang.cpp)
  • Re: Does std::map provides a copy assignment operator?
    ... >> copy constructor and assignment operator are not inheritable. ... The compiler will generate a derived copy ... > constructor/assignment operator and these will call the base class ... > automatically generate Default Constructors, Destructors, Copy ...
    (microsoft.public.vc.stl)
  • Re: Does std::map provides a copy assignment operator?
    ... >> copy constructor and assignment operator are not inheritable. ... The compiler will generate a derived copy ... > constructor/assignment operator and these will call the base class ... > automatically generate Default Constructors, Destructors, Copy ...
    (microsoft.public.vc.stl)
  • Re: Does std::map provides a copy assignment operator?
    ... > copy constructor and assignment operator are not inheritable. ... > Since map inherits from xtree, it does not inherit xtree's copy ... You need to research under what circumstances the compiler will ... automatically generate Default Constructors, Destructors, Copy Constructors ...
    (microsoft.public.vc.stl)
  • Re: Q about increment and assignment
    ... public static void main{ ... int i = 1; ... I understand the assignment operator happening before the ++, ... What is the difference between the original problem and this one? ...
    (comp.lang.java.programmer)