0

I want to have a typedef in my base class to be specialized to each class derived from this base class. code:

template<class X>
class SharedPointer
{
public:
    X* data;
    SharedPtr(X *val)
    {
        data = val;
    }
};

template<class T=Base> /* default type, I know this is a mistake. 
The reason to have this here is to just indicate that the default argument 
should be Base itself. so it'll have a Base type of shared pointer. */
class Base
{
public:
    typedef SharedPointer<T> MyTypeOfPtr;
    virtual MyTypeOfPtr Func()
    {
        Base *b = new Base;
        return MyTypeOfPtr(b);
    }
};

class Derived : Base<Derived>
{
public:
    MyTypeOfPtr Func()
    {
        Derived *d = new Derived;
        return MyTypeOfPtr(d);      
    }
};

main()
{
 Base b;
 Base::MyTypeOfPtr ptr1 = b.Func();
 Derived d;
 Derived::MyTypeOfPtr ptr2 = d.Func();  
}

but this doesn't compile. is there a way to have this functionality?

sithereal
  • 1,656
  • 9
  • 16
  • 3
    There is no template typedef. What specifically doesn't work? What are you trying to do as there is always a way. – AJG85 Oct 04 '11 at 17:51
  • Probably related - http://stackoverflow.com/questions/6006614/c-static-polymorphism-crtp-and-using-typedefs-from-derived-classes – Bo Persson Oct 04 '11 at 18:00
  • related: [virtual template function](http://stackoverflow.com/questions/5871722/how-to-achieve-virtual-template-function-in-c), [more](http://stackoverflow.com/questions/7610350/how-to-simulate-virtuality-for-method-template), – sehe Oct 04 '11 at 18:01
  • please include compile errors – Tom Kerr Oct 04 '11 at 18:03
  • @AJG85: any suggestions about having a type T that is actually another template class(SharedPtr) initialized with the type of owner of T then? – sithereal Oct 04 '11 at 18:05
  • @Taz_d: I'm sorry, but I cannot understand what you're asking. You seem to be using the word "initialized" wrong. Also, "ownership" has a (at least colloquial) meaning which does not seem to apply to anything to do with your question. Did you mean the `base` or `derived` type of `T`? Is this `T` still a template parameter or a hypothetical class? – Mooing Duck Oct 04 '11 at 19:30
  • 1
    @Taz_d We need a bit more clarification as others have pointed out. As it stands all anyone can do is guess at your intent and make general assumptions. – AJG85 Oct 04 '11 at 20:44

1 Answers1

2

You have to get all sorts of details right:

  • Spelling: "SharedPointer" or "SharedPtr"?

  • Templates and classes aren't the same thing, so you can't have class T = Base: T is a class, Base isn't. Also, you can't have the default refer to itself, so even class T = Base<T> doesn't work. Remove the default type.

  • Class inheritance is private by default, so say class Derived : public Base<Derived>.

  • Make the constructor of SharedPointer public.

  • Base::Func() makes no sense; maybe it should say new T.

I should seriously suggest that you start with simpler examples and build up slowly.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    well, first of all thank you for your answer. but do you have anything to provide to help me what I'm trying to achive other than being concerned about errors which are totally unrelated with the topic and coming up with "how to study c++" suggestions? – sithereal Oct 04 '11 at 18:15
  • @Taz_d: it depends on what you're trying to achieve. The compiler errors all go away when you do as he suggests: http://codepad.org/uDuSWULn, but we can't tell what you want to use `T` for. Base doesn't need to be templated to do what it looks like your trying to achieve. – Mooing Duck Oct 04 '11 at 18:54
  • @MooingDuck: Indeed. I created a working version of the code precisely by applying one fix for each listed point. – Kerrek SB Oct 04 '11 at 18:56
  • 2
    @Taz_d But it would be easier to answer if not for all those things that Kerrek pointed out. I can't answer because I really have no idea what your question is about. – Luc Danton Oct 04 '11 at 19:23