Re: Why copy constructor is not called
From: Aaron Queenan (aqueenan_DieSpammerDie__at_contingent.com.au)
Date: 02/09/04
- Next message: tom_usenet: "Re: Length of an array of bytes"
- Previous message: Rodrigo Corral González [MVP]: "Re: Length of an array of bytes"
- In reply to: Toma Bussarov: "Why copy constructor is not called"
- Next in thread: Doug Harrison [MVP]: "Re: Why copy constructor is not called"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: tom_usenet: "Re: Length of an array of bytes"
- Previous message: Rodrigo Corral González [MVP]: "Re: Length of an array of bytes"
- In reply to: Toma Bussarov: "Why copy constructor is not called"
- Next in thread: Doug Harrison [MVP]: "Re: Why copy constructor is not called"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|