abstract class + inheritance

hi,
if there is pure virtual class A and the derived class B,how is the copy constructor is done for the derived class B???.

thanks
Not sure I understand the question. The default copy constructor of a derived class is the same as one for any other class. You define the copy constructor exactly the same way as well.
let my try to explain my question again is:the below code won't pass any compilation because I have to define constructor for the base,but on the other side the Base class will not be interface !!! so:
1. if my interface have private members, how the derived Class can have access to it to make copy constructor? (any solotion other than protected?)
2. any purpose for having private members for pure virtual class(Base class in my code)?

" this code was copied from stackoverflow"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Base {
public:
    Base( const Base & );
    virtual ~Base();
    virtual void Method() = 0;
    // etc...
private:
    int *memberIntArray;
    int length;
    // etc...
};

class DerivedOne : public Base{
public:
    DerivedOne( const DerivedOne & );
    virtual ~DerivedOne();
    virtual void Method();
    // etc...
protected:
    // data, etc...
};

class DerivedTwo : public Base{
public:
    DerivedTwo( const DerivedTwo & );
    virtual ~DerivedTwo();
    virtual void Method();
    // etc...
protected:
    // data, etc...
};

///////////////////

Base::Base( const Base & other ) {
    this->length = other.length;
    this->memberIntArray = new int[length];
    memcpy(this->memberIntArray,other.memberIntArray,length);
}

//etc...

DerivedOne::DerivedOne( const DerivedOne & other )
    : Base( other )
{
    //etc...
}

DerivedTwo::DerivedTwo( const DerivedTwo & other )
    : Base( other )
{
    //etc...
}


thanks,
Last edited on
From what I know, by definition an interface doesn't have member variables. However an abstract (purely virtual) class can allow access to private variables the same way any other class can, like through friends, or (non-private) accessor methods.

In your above example it appears you only need to add a public constructor to your Base class, and derived classes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Abstract
class Base {
 private:
    int *array;
    int count;

 public:
    Base(int);
    virtual ~Base();

    //...
};

Base::Base(int n) : count(n) {
    array = new int[count];
}

Base::~Base() {
    delete[] array;
}

//...

//------------------------------------------------------------------------------
class Derived {
    //...

 public:
    Derived(int n);

    //...
};

Derived::Derived(int n) : Base(n) {
    //...
}

//... 
Topic archived. No new replies allowed.