S
subramanian100in
Two days back, I have posted a question to comp.lang.c++ with the
subject line "derived class and virtual function".
In one of the replies to that question Francesco S. Carta had given
the following program:
#include <iostream>
using namespace std;
class Base {
public:
Base(int data = 0) : data(data) {};
Base(const Base& base) : data(base.data) {};
virtual Base* Clone() const {
cout << "cloning Base" << endl;
return new Base(*this);
}
int Data() const {
return data;
}
private:
int data;
};
class Derived : private Base {
public:
Derived(int data = 0) : Base(data) {};
Derived(const Derived& derived) : Base(derived) {};
Base* Clone() const {
cout << "cloning Derived" << endl;
return new Derived(*this);
}
};
int main() {
// fails, of course
Base* pb = new Derived(42);
Derived d(42);
// calls Derived::Clone()
Base* pb2 = d.Clone();
// calls Derived::Clone()
Base* pb3 = pb2->Clone();
// prints 42
cout << pb3->Data() << endl;
}
When I compiled this program with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra a.cpp
I get the following error:
a.cpp: In function `int main()':
a.cpp:35: error: `Base' is an inaccessible base of `Derived'
a.cpp:35: warning: unused variable 'pb'
This error corresponds to the line:
Base* pb = new Derived(42);
This is because since Base class is private, I am trying to convert
'Derived*' to 'Base*'. However, in the Derived class Clone() function,
I am returning 'new Derived(*this)' as 'Base*'.
ie I am converting 'Derived*' to 'Base*'. How is this accepted in this
function?
Base* Clone() const {
cout << "cloning Derived" << endl;
return new Derived(*this);
}
Kindly clarify.
Thanks
V.Subramanian
subject line "derived class and virtual function".
In one of the replies to that question Francesco S. Carta had given
the following program:
#include <iostream>
using namespace std;
class Base {
public:
Base(int data = 0) : data(data) {};
Base(const Base& base) : data(base.data) {};
virtual Base* Clone() const {
cout << "cloning Base" << endl;
return new Base(*this);
}
int Data() const {
return data;
}
private:
int data;
};
class Derived : private Base {
public:
Derived(int data = 0) : Base(data) {};
Derived(const Derived& derived) : Base(derived) {};
Base* Clone() const {
cout << "cloning Derived" << endl;
return new Derived(*this);
}
};
int main() {
// fails, of course
Base* pb = new Derived(42);
Derived d(42);
// calls Derived::Clone()
Base* pb2 = d.Clone();
// calls Derived::Clone()
Base* pb3 = pb2->Clone();
// prints 42
cout << pb3->Data() << endl;
}
When I compiled this program with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra a.cpp
I get the following error:
a.cpp: In function `int main()':
a.cpp:35: error: `Base' is an inaccessible base of `Derived'
a.cpp:35: warning: unused variable 'pb'
This error corresponds to the line:
Base* pb = new Derived(42);
This is because since Base class is private, I am trying to convert
'Derived*' to 'Base*'. However, in the Derived class Clone() function,
I am returning 'new Derived(*this)' as 'Base*'.
ie I am converting 'Derived*' to 'Base*'. How is this accepted in this
function?
Base* Clone() const {
cout << "cloning Derived" << endl;
return new Derived(*this);
}
Kindly clarify.
Thanks
V.Subramanian