D
David Jobet
Hello,
here's a simple program :
listing 1 :
---
#include <stdio.h>
class A
{
public :
A() { printf("A()\n"); }
A(bool b) { printf("A(bool)\n");}
//private :
A(const A&) { printf("A(const A &)\n"); }
private :
const A &operator =(const A &) { printf("const A &operator =(const A &);
\n"); return *this;}
};
int main(int argc, char **argv)
{
A a0;
A a3 = true;
return 1;
}
---
I compile it with g++ (4.4.3 (Ubuntu 4.4.3-4ubuntu5)) and run it. Here's the
output :
A()
A(bool)
Cool, the copy constructor is not invoked.
Now let's make a simple modification :
(uncommenting the private access modifier in listing 1 to make the copy
constructor private)
listing 2
---
#include <stdio.h>
class A
{
public :
A() { printf("A()\n"); }
A(bool b) { printf("A(bool)\n");}
private :
A(const A&) { printf("A(const A &)\n"); }
private :
const A &operator =(const A &) { printf("const A &operator =(const A &);
\n"); return *this;}
};
int main(int argc, char **argv)
{
A a0;
A a3 = true;
return 1;
}
---
Still compiling it with the same compiler. Here are the errors :
test.cpp: In function ‘int main(int, char**)’:
test.cpp:10: error: ‘A::A(const A&)’ is private
test.cpp:18: error: within this context
Not cool. Don't understand.
First listing "shows" that I'm not using the copy constructor, but in
listing 2, if I make this "not used" copy constructor private, it does not
compile ?!!
What am I missing here ?
Tx
David
PS : oh, and I'm just copmpiling it like this :
$> g++ test.cpp
then invoking the output (if it compiles) from
$> ./a.out
here's a simple program :
listing 1 :
---
#include <stdio.h>
class A
{
public :
A() { printf("A()\n"); }
A(bool b) { printf("A(bool)\n");}
//private :
A(const A&) { printf("A(const A &)\n"); }
private :
const A &operator =(const A &) { printf("const A &operator =(const A &);
\n"); return *this;}
};
int main(int argc, char **argv)
{
A a0;
A a3 = true;
return 1;
}
---
I compile it with g++ (4.4.3 (Ubuntu 4.4.3-4ubuntu5)) and run it. Here's the
output :
A()
A(bool)
Cool, the copy constructor is not invoked.
Now let's make a simple modification :
(uncommenting the private access modifier in listing 1 to make the copy
constructor private)
listing 2
---
#include <stdio.h>
class A
{
public :
A() { printf("A()\n"); }
A(bool b) { printf("A(bool)\n");}
private :
A(const A&) { printf("A(const A &)\n"); }
private :
const A &operator =(const A &) { printf("const A &operator =(const A &);
\n"); return *this;}
};
int main(int argc, char **argv)
{
A a0;
A a3 = true;
return 1;
}
---
Still compiling it with the same compiler. Here are the errors :
test.cpp: In function ‘int main(int, char**)’:
test.cpp:10: error: ‘A::A(const A&)’ is private
test.cpp:18: error: within this context
Not cool. Don't understand.
First listing "shows" that I'm not using the copy constructor, but in
listing 2, if I make this "not used" copy constructor private, it does not
compile ?!!
What am I missing here ?
Tx
David
PS : oh, and I'm just copmpiling it like this :
$> g++ test.cpp
then invoking the output (if it compiles) from
$> ./a.out