Re: [C++] Copy constructor
From: jeffc (nobody_at_nowhere.com)
Date: 10/14/03
- Next message: jeffc: "Re: IDE or not ?"
- Previous message: mathscat: "WinXP and Borland C++ command line tools"
- In reply to: Rich ²°°³: "[C++] Copy constructor"
- Next in thread: Martijn Lievaart: "Re: [C++] Copy constructor"
- Reply: Martijn Lievaart: "Re: [C++] Copy constructor"
- Reply: Jerry Coffin: "Re: [C++] Copy constructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 14 Oct 2003 11:42:11 -0400
"Rich ²°°³" <1@1.1> wrote in message
news:puUib.6709$fn2.58480823@news-text.cableinet.net...
> After reading a tutorial on OO concepts
> (http://www.gnacademy.org/twiki/bin/view/CPP/ConstructorsTopic), I thought
> i'd try out an example of a copy constructor myself.
>
> The following program compiles even though I commented out the necessary
> code for the copy constructor.
>
> I'm a bit confused now! Does C++ provide it's own copy constructor for
> classes? Or is this something to do with the compiler? (mingw32).
The funny thing about your code is that you haven't *declared* a copy
constructor, only defined one. I'm a little surprised it worked with the
definition only. It's true that a definition can also be a declaration, but
I would have thought that if you didn't declare the copy constructor, the
compiler would have gotten confused. In any case, when you comment out the
definition, yes, the compiler generates a copy constructor for classes. I
guess I just assumed that the compiler would have decided whether or not to
generate the copy constructor by the time it got to the end of the class
definition.
> #include <cstdio>
> #include <iostream>
>
> class Ctest
> {
> private:
> int x;
>
> public:
> void setx(const int val){x = val;}
> int getx(){return x;}
>
> Ctest(){
> x = 0;
> }
>
> Ctest(const int val){
> x = val;
> }
>
> // Ctest(Ctest& nCtest){
> // x = nCtest.getx();
> // }
>
> };
>
> int main()
> {
> Ctest one(25);
> Ctest two(one);
> std::cout << one.getx();
>
> getchar(); //for console pause
> return 0;
> }
>
>
- Next message: jeffc: "Re: IDE or not ?"
- Previous message: mathscat: "WinXP and Borland C++ command line tools"
- In reply to: Rich ²°°³: "[C++] Copy constructor"
- Next in thread: Martijn Lievaart: "Re: [C++] Copy constructor"
- Reply: Martijn Lievaart: "Re: [C++] Copy constructor"
- Reply: Jerry Coffin: "Re: [C++] Copy constructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|