By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

shared_ptr and local types

P: n/a
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()::Deri ved2*)‚
/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<BaseBase_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;
}

Mar 13 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Mar 13, 9:22 pm, "Tim H" <thoc...@gmail.comwrote:
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()::Deri ved2*)‚
/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?

Yes - local types can't be used as template arguments (something do do
with having no linkage? I'm sure someone will correct / elaborate on
this).
I have never seen local class definitions in "real" code, and would
hesitate to use them because a) they're exotic and potentially
confusing, and b) the limitation you just ran into rather limits their
usefulness. Could you consider using an anonymous namespace instead?

Mar 14 '07 #2

P: n/a
On Mar 13, 5:07 pm, "Pete C" <5gv7rq...@sneakemail.comwrote:
On Mar 13, 9:22 pm, "Tim H" <thoc...@gmail.comwrote:
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()::Deri ved2*)‚
/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?

Yes - local types can't be used as template arguments (something do do
with having no linkage? I'm sure someone will correct / elaborate on
this).
I have never seen local class definitions in "real" code, and would
hesitate to use them because a) they're exotic and potentially
confusing, and b) the limitation you just ran into rather limits their
usefulness. Could you consider using an anonymous namespace instead?
The example arose as part of a unit-test, where the Derived2 class was
defined as part of the test function.

Tim

Mar 14 '07 #3

P: n/a
Pete C <5g*******@sneakemail.comwrote:
On Mar 13, 9:22 pm, "Tim H" <thoc...@gmail.comwrote:
>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()::Der ived2*)‚
/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?


Yes - local types can't be used as template arguments (something do do
with having no linkage? I'm sure someone will correct / elaborate on
this).
Yes, I think the term is "external linkage", which local types do not
have.
I have never seen local class definitions in "real" code, and would
hesitate to use them because a) they're exotic and potentially
confusing, and b) the limitation you just ran into rather limits their
usefulness.
There have been several times when I wanted to declare a local/nested
class that served as a utility for the outer class but had no use
outside of this class; however, I could not use any of the <algorithm>s
or std containers on them because of the above reason. If I could use
templates with local classes, they would make my code slightly cleaner.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.