-7

I would like to write something like the following:

class A {
    virtual typedef int foo_t;

    void bar() {
       cout << "foo_t is " << get_demangled_name(typeid(foo_t));
    }
}

class B : A {
    typedef float foo_t;
}

And for bar() to behave polymorphically. But - this is not possible in C++. What should I do instead?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 2
    Why do you want that ? – Jarod42 Jul 14 '14 at 13:17
  • 1
    Seems like an XY question, where you THINK this is the solution to some problem, and you'd be better off describing the original problem. [Most likely you either want a template class, or two classes with different virtual functions, but if you describe the actual problem you are trying to solve, there may be other options that are even better] – Mats Petersson Jul 14 '14 at 13:21
  • @Jarod42: [Gedankenexperiment](http://en.wikipedia.org/wiki/Thought_experiment). I'm trying to deepen my C++ knowledge. – einpoklum Jul 14 '14 at 13:23
  • @MatsPetersson: I didn't say it was "the solution" to anything... – einpoklum Jul 14 '14 at 13:26
  • In C++, expression types are inherently static. There's no way for an expression to have a different type depending on runtime conditions, and so no way for anything quite like this to work. What to do instead depends on the problem you're trying to solve; there are various ways to emulate various aspects of dynamic typing. If, as you say, there's no problem to solve, the only sensible thing to do is nothing. – Mike Seymour Jul 14 '14 at 13:35
  • I never cease to be impressed by how much SO people are a mean crowd. – einpoklum Jul 14 '14 at 13:38
  • 1
    @einpoklum: It is not being mean, it's trying to answer the question you are asking, by understanding what your question is, instead of answering something that isn't what you are looking for. Of course, if the question is akin to "If the moon is made of cheese, what does it taste like", then the answer is very much "Nobody knows, because the moon isn't made from cheese". – Mats Petersson Jul 14 '14 at 13:45
  • @MatsPetersson: I wasn't talking about you, your comments are ok. It's the downvotes. Voting to close is one thing, downvoting is saying that a question is relevant, but it sucks beyond repair. It's not customary to downvote without good cause on some other SX network sites. – einpoklum Jul 14 '14 at 13:54
  • Yeah, I'm slightly confused about the number of downvotes on many questions. Unfortunately, seems like most people that do such downvoting also don't explain why, which is even more annoying, because it gives you no way to actually improve for next time. – Mats Petersson Jul 14 '14 at 14:01
  • @MatsPetersson: It's not just 'next time', the courteous thing to do is to suggest how to improve or reformulate the question. – einpoklum Jul 14 '14 at 14:11

1 Answers1

2

So, if this is just a thought-experiment, then the answer is "You can't do that".

If you actually want a hierarchy where your derived classes have different types, you could do something like this:

class Base
{
   public:
     virtual void bar() = 0;
};

class A: public Base
{
   public:
     void bar() { ... do stuff here }
   private:
     typedef int foo_t;
};


class B: public Base
{
   public:
     void bar() { ... do stuff here }
   private:
     typedef float foo_t;
};

Alternatively, if inheritance isn't required:

template<typename T>
class TemplateA
{
   typedef T foo_t;
   void bar() { ... do stuff here ... }
}

typedef TemplateA<int> A;
typedef TemplateA<float> B;

What the virtual function gives is that you can have a:

vector<Base&> v; 
A a;
B b;
v.push_back(a);
v.push_back(b);

and use it in a generic way. Templates don't give quite that flexibility (in this example, it would of course be possible to produce some sort of templated solution that also has a base-class, and then the both solutions are more or less identical)

It all depends on what your thoughtexperiment actually is trying to solve - even if it's just a thoughtexperiment, these two solutions will have different pro's and con's, depending on how you want to use them.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227