guskenny83
guskenny83

Reputation: 1373

Can I declare a different typedef depending on class?

Is it possible to have a different type definition based on which derived class is instantiated?

Say I have a parent class with a virtual function func(), two int members and a vector of type myType, and two child classes, which share the same int members, and the vector, but their implementation of func() require myType to be slightly different.

For example:

class Parent {
  protected:
    int myMember;
    int myOtherMember;
    std::vector<myType> vec;

  public:
    Parent(variable);
    virtual int func() = 0;
}

class Child1 : public Parent {
  private:
    typedef <some type definiton> myType;

  public:
    Child1(variable) : Parent(variable){};
    int func() {return someFunc();};
}

class Child2 : public Parent {
  private:
    typedef <some other type definiton> myType;

  public:
    Child2(variable) : Parent(variable){};
    int func() {return someOtherFunc();};
}

Can I do something like this? when I have tried it, it creates a circular dependency in the header files, because class Parent is required to be included first, but then it requires myType to be defined.

Is there a way of forward declaring myType depending on class? or do I just need to include a different vector of myType in each class like so:

class Parent {
  protected:
    int myMember;
    int myOtherMember;

  public:
    Parent(variable);
    virtual int func() = 0;
}

class Child1 : public Parent {
  private:
    typedef <some type definiton> myType;
    std::vector<myType> vec;

  public:
    Child1(variable) : Parent(variable){};
    int func() {return someFunc();};
}

class Child2 : public Parent {
  private:
    typedef <some other type definiton> myType;
    std::vector<myType> vec;     

  public:
    Child2(variable) : Parent(variable){};
    int func() {return someOtherFunc();};
}

Upvotes: 0

Views: 1289

Answers (3)

user4581301
user4581301

Reputation: 33932

This is a job for templateman!

First we declare Parent

template <class TYPE>
class Parent {
  protected:
    int myMember;
    int myOtherMember;
    std::vector<TYPE> vec;

  public:
    Parent(TYPE variable);
    virtual int func() = 0;
};

Unfortunately a custom type contained within Child1 is out with this approach because the type needs to be declared before we can specialize a template on it. I'm using int and double here for ease. Replace as needed.

using Child1Type = int;
// or typedef int Child1Type; pre-C++11
class Child1 : public Parent<Child1Type> {
  public:
    Child1(Child1Type variable) : Parent(variable){};
    int func() {return someFunc();};
};

using Child2Type = double;
class Child2 : public Parent<Child2Type> {

  public:
    Child2(Child2Type variable) : Parent(variable){};
    int func() {return someOtherFunc();};
};

EDIT

Kicking myself for not getting this earlier (and burning way too much time because I knew it had to be possible, but was thinking in the wrong direction)

Declare the type up in Parent. Set the access based on who need to be able to see the type. Here I made Type public to test it out in main. The Type is declared in Parent and is visible to (and through because it's public) the Children.

Also stripped out everything that wasn't immediately necessary and used fixed width integer types so I could easily demonstrate the differences.

I think this is exactly what Pixelchemist was alluding to.

template <class TYPE>
class Parent {
  public:
    using Type = TYPE; // declare Type here
  protected:
    int myMember;
    int myOtherMember;
    std::vector<Type> vec; // visible here

  public:
    Parent(Type variable); // here
    virtual ~Parent(){}
    virtual int func() = 0;
};

class Child1 : public Parent<uint16_t> { // complicated type needs to be reproduced 
                                         // only once, here in the specialization
  public:
    Child1(Type variable) : Parent(variable){};
};

class Child2 : public Parent<uint32_t> {

  public:
    Child2(Type variable) : Parent(variable){};
};

int main()
{
    // and visible way down here in main through Child1 and Child2
    std::cout << "Child 1: " << sizeof(Child1::Type) << std::endl; 
    std::cout << "Child 2: " << sizeof(Child2::Type) << std::endl;
}

output is

Child 1: 2
Child 2: 4

Upvotes: 3

Pixelchemist
Pixelchemist

Reputation: 24946

You could simply use templates:

template<class T>
struct Base
{
  std::vector<T> v;
};

struct D1 : public Base<int>
{
  // all stuff in here comes into play when deriving from Base is already done
  // Thus, compiler cannot know any typedefs from here inside Base...
};

struct D2 : public Base<double>
{
};

where you cannot use a typedef from derived in base class. See CRTP refer to typedef in derived class from base class.

Upvotes: 1

Assimilater
Assimilater

Reputation: 964

This is what templates are for:

<template typename T>
class Child : public Parent {
private:
    std::vector<T> vec;
public:
    Child(variable) : Parent(variable) {};
    int func() { return someOtherFunc(); };
}

Then Child is declared as something like:

Child<int> myChildInstance(10);

Upvotes: 0

Related Questions