Question

I am using the following data types in my project:

std::vector<std::shared_ptr<float>>;
std::vector<std::shared_ptr<string>>;
pair<std::vector<string>,double>;

These data types are used repeatedly in many classes of the project. I want to use typedef. My question is the following: Can I create a class especially for data types? For example:

class Foo
{
   typedef  std::vector<std::shared_ptr<float>> floatVec;
   typedef  std::vector<std::shared_ptr<string>> stringVec;
   typedef  pair<std::vector<string>,double> pairVec;

};

What do you think of this practice?


As a conlusion from what I understood:

1- typedef shout preferably be used when a class is passed as a template type (Konrad Rudolph)

2- DO NOT ABUSE THE SHARED_PTR (C++ IS NOT JAVA) ok

Was it helpful?

Solution 2

You can do this – as in, it compiles and will work.

But why do you want to do this? It seems to have no purpose in this general form. Are you maybe trying to use a class as a namespace?

In general, this screams of lack of thought about the class design. You seem to have only few types without clearly designated purpose, and you’re apparently using (shared) pointers in a completely inappropriate manner.

OTHER TIPS

You should use namespace instead:

namespace Foo

and the rest the same (just skip the last semicolon). Namespaces are much better suited for such cases as yours.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top