T
Tim H
I'm newish to C++ but not to C.
I'm confused by this code. test1() is fine. test2() fails to
compile.
/tmp/inherit_ptr.cpp: In function âvoid test2()â:
/tmp/inherit_ptr.cpp:52: error: no matching function for call to
âboost::shared_ptr<Base>::shared_ptr(test2():erived2*)â
/usr/include/boost/shared_ptr.hpp:119: note: candidates are:
boost::shared_ptr<T>::shared_ptr() [with T = Base]
/usr/include/boost/shared_ptr.hpp:106: note:
boost::shared_ptr<Base>::shared_ptr(const boost::shared_ptr<Base>&)
Is there a general rule that I am violating here?
Tim
#include <iostream>
#include <string>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
class Base {
public:
virtual ~Base() {}
virtual string evaluate() const = 0;
};
typedef shared_ptr<Base> Base_ptr;
//
// Test 1
//
// define Derived1 as a global type
class Derived1: public Base {
public:
Derived1(): Base() {}
virtual ~Derived1() {}
virtual string evaluate() const { return "Derived1"; }
};
void
test1()
{
// Base_ptr to subclass
Base_ptr base_der(new Derived1);
}
//
// Test 2
//
void
test2()
{
// define Derived2 as a local type
class Derived2: public Base {
public:
Derived2(): Base() {}
virtual ~Derived2() {}
virtual string evaluate() const { return "Derived2"; }
};
Base_ptr base_der(new Derived2);
}
int
main()
{
test1();
test2();
return 0;
}
I'm confused by this code. test1() is fine. test2() fails to
compile.
/tmp/inherit_ptr.cpp: In function âvoid test2()â:
/tmp/inherit_ptr.cpp:52: error: no matching function for call to
âboost::shared_ptr<Base>::shared_ptr(test2():erived2*)â
/usr/include/boost/shared_ptr.hpp:119: note: candidates are:
boost::shared_ptr<T>::shared_ptr() [with T = Base]
/usr/include/boost/shared_ptr.hpp:106: note:
boost::shared_ptr<Base>::shared_ptr(const boost::shared_ptr<Base>&)
Is there a general rule that I am violating here?
Tim
#include <iostream>
#include <string>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
class Base {
public:
virtual ~Base() {}
virtual string evaluate() const = 0;
};
typedef shared_ptr<Base> Base_ptr;
//
// Test 1
//
// define Derived1 as a global type
class Derived1: public Base {
public:
Derived1(): Base() {}
virtual ~Derived1() {}
virtual string evaluate() const { return "Derived1"; }
};
void
test1()
{
// Base_ptr to subclass
Base_ptr base_der(new Derived1);
}
//
// Test 2
//
void
test2()
{
// define Derived2 as a local type
class Derived2: public Base {
public:
Derived2(): Base() {}
virtual ~Derived2() {}
virtual string evaluate() const { return "Derived2"; }
};
Base_ptr base_der(new Derived2);
}
int
main()
{
test1();
test2();
return 0;
}