About uncopyable class (on book:Effective C++)


 
Thread Tools Search this Thread
Top Forums Programming About uncopyable class (on book:Effective C++)
# 1  
Old 12-01-2012
About uncopyable class (on book:Effective C++)

See the following code:
Code:
#include<iostream>
using namespace std;
class Uncopyable
{
protected:
    Uncopyable(){cout<<"hehe\n";}
    ~Uncopyable(){}
private:
    Uncopyable(const Uncopyable &rhs){cout<<"oooops\n";};
    Uncopyable& operator=(const Uncopyable &rhs);
};

class Dog : private Uncopyable
{
    string name;
    int value;
public:

    Dog(const string &s, const int &v):name(s), value(v){}
public:
    //Dog(const Dog &rhs){};             //here is the question!
};

int main()
{
    Dog a("a", 9);
    Dog b(a);
}

If I comment out that line, g++ will complaint "Uncopyable::Uncopyable(const Uncopyable&) is private". I think that mean the default copy ctor of derived class created by g++ called the base class' copy ctor.


If I make available of that line, every thing is ok. It out put
Code:
hehe
hehe

I think this mean the default copy ctor of derived class created by g++ called the base class' default ctor.
Am I right? If yes, why doesn't the default copy ctor derived class, in condition 2, call the base class' copy ctor? Why it behave so inconsistently?

thank you!

Last edited by vistastar; 12-01-2012 at 06:31 AM..
# 2  
Old 12-01-2012
A copy constructor's not going to get called unless you copy it. Your Dog(const Dog &rhs) constructor does literally nothing -- it's not even going to copy Dog, and passes nothing into Uncopyable. Lacking any other information, the compiler uses the default.

Last edited by Corona688; 12-01-2012 at 01:45 PM..
# 3  
Old 12-01-2012
Thanks, but I want a more precise answer...
# 4  
Old 12-03-2012
More precise than what? There is not a lot more information to be had.

1) You have overloaded the = operator.
2) You don't assign to that class using the = operator anywhere.
3) Therefore, the = operator is not called.

Next,

1) You have overloaded the Dog() constructor. This means you get to choose what you pass into Uncopyable().
2) You don't pass anything into Uncopyable().
3) Therefore nothing gets passed into Uncopyable(). It uses the default constructor.

It's not going to assume what things it shouldn't and shouldn't take from a custom constructor. If you wanted it to use the other constructor, you'd have to tell it, something like so:

Code:
Dog(const Dog &rhs):Uncopyable(rhs) {}


Last edited by Corona688; 12-03-2012 at 04:23 PM..
# 5  
Old 12-04-2012
You can ignore operator=, since I don't use it.

I mean, If I comment out that line copy ctor, g++ will generate one, and it tries to call Uncopyable(const Uncopyable &rhs){cout<<"oooops\n";}, and then of course fails due to private attribute.
If I reserve that line, g++ will not generate the default version of copy ctor, and my version tries to call Uncopyable(){cout<<"hehe\n";} and of course succeed.

I don't know why it behave so inconsistently. aka, Why don't both call Uncopyable(){cout<<"hehe\n";} or Uncopyable(const Uncopyable &rhs){cout<<"oooops\n";}(which I think is more reasonable)?

Last edited by vistastar; 12-04-2012 at 08:40 AM..
# 6  
Old 12-04-2012
It's not inconsistent, you get what you ask for, nothing more, nothing less.

Quote:
Originally Posted by Corona688
1) You have overloaded the Dog() constructor. This means you get to choose what you pass into Uncopyable().
2) You don't pass anything into Uncopyable().
3) Therefore nothing gets passed into Uncopyable(). It uses the default constructor.
Quote:
1) You have overloaded the Dog() constructor. This means you get to choose what you pass into Uncopyable().
Quote:
This means you get to choose what you pass into Uncopyable().
What did you pass into Uncopyable() there? Nothing -- absolutely nothing.

What did it pass into Uncopyable()? Nothing, absolutely nothing.

The default copy constructor isn't called by the Dog() class because you overloaded it. It does what you say, instead of the defaults. That's what overloading is for. If you want default behavior, either don't overload it, or call it yourself...
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies

2. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

3. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies

4. Programming

static use for class inside the same class c++

Hi, I believe the next code is wrong: class Egg { Egg e; int i; Egg(int ii=0) : i(ii) {} }; because you would end up with an endless definition (memory allocation) of Egg objects, thus int i. Ok, so God Eckel proposes for a singleton: class Egg { static Egg e; int... (5 Replies)
Discussion started by: xavipoes
5 Replies

5. UNIX for Dummies Questions & Answers

Real and Effective IDs

Can anyone explain me in details of Real and Effective IDs (6 Replies)
Discussion started by: kkalyan
6 Replies

6. Programming

C++ class definition with a member of the same class

Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class? For example, a linked list or i want to convert this C code to C++ class (Elemento) typedef struct elemento { char name; char value; List<struct elemento> ltElementos; ... (7 Replies)
Discussion started by: pogdorica
7 Replies

7. UNIX for Dummies Questions & Answers

car class (not school class)

im just trying to have some fun and kill some time writing a c++ program that has a person type in a car make and model then gives them a year and a price. or something like that. i always have problems getting it goin but once the ball is rolling im usually pretty good. anyone wanna help me out? ... (1 Reply)
Discussion started by: rickym2626
1 Replies

8. UNIX for Dummies Questions & Answers

most effective search ?

what's the most efficient and effective search for a file in a dir ? I see many guys use this # find - print or something as such ? and sometimes pipe it to something else ? Is there a better way of using "grep" in all of this ? thanks simon2000 (3 Replies)
Discussion started by: simon2000
3 Replies

9. Cybersecurity

Changing effective user

I would like to give execution rights for a script to one user. (that's the easy part...) When that user is running the script, I would like the effective user ID to be that of the file-owner. Is this possible? (6 Replies)
Discussion started by: hilmel
6 Replies
Login or Register to Ask a Question