Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a special class for all the data types that I use in a project

Tags:

c++

class

typedef

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

like image 563
Hani Goc Avatar asked Nov 18 '25 16:11

Hani Goc


2 Answers

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.

like image 167
Wojtek Surowka Avatar answered Nov 20 '25 07:11

Wojtek Surowka


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.

like image 25
Konrad Rudolph Avatar answered Nov 20 '25 05:11

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!