954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

copy constructor calling

If an explicit copy constructor is defined that makes a complex copy, does:

int main()
{
            class shoe shoe1;
            class shoe shoe_copy(shoe1);
}

And

int main()
{
             class shoe shoe1;
             class shoe shoe_copy;
             shoe_copy=shoe1;
}

Basically I'm asking if the assignment operator accesses the complex copy constructor as an initialization of a class object to another class object would.

CPPRULZ
Junior Poster in Training
91 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Well I dont think it would work out. Basically constructors should be members of that particular class or if you design it explicitly it will become a function with the same name of the class and will produce out an error.

If you wish to make the above methods work out . You will have to again call out the function from the constructors of your class for it to work.

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 
jencas
Posting Whiz
366 posts since Dec 2007
Reputation Points: 395
Solved Threads: 71
 

only the first example invokes copy constructor. If the second one needs to work in the same manner as copy constructor does, you have to overload the assignment operator.

kbshibukumar
Junior Poster in Training
65 posts since Jan 2009
Reputation Points: 12
Solved Threads: 8
 

Thank you Sky Diploma, jencas, and kbshibukumar the site and your advice was very helpful. So basiclly what you were all saying was that the copy constructor and assignment operator are 2 different functions that must be defined by the coder seperately. I think that there is an automatic version of the overloaded assignment operator but it may be compiler dependant-I have one with Visual studio prfessional 2008.

CPPRULZ
Junior Poster in Training
91 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You