-1

I am learning C++, this may be a silly question.

I want to create a class for common type definitons and using it in many cpp files by inluding. Header is "CommonTypesCls.h":

class CommonTypesCls
{
public:     
    typedef unsigned int UINT32;
    typedef int INT32;
}

And I want to use these attributes in source.cpp like that:

#include "CommonTypesCls.h"
int main()
{    
    int a = sizeof(UINT32);
    return 0;
}

Is it possible to using a inclueded class' member type without scope resolution?

  • I see no "attribute" in your code. Do you mean "member type"? –  Feb 24 '18 at 09:49
  • Yes, I mean member type. –  Feb 24 '18 at 09:52
  • 1
    Fixed size integer types are provided by ``. The common convention in C++ is to reserve ALL UPPERCASE identifiers for macro symbols (though many beginners/novices and even some professionals do this, you misapplied a Java/Python convention to a language where it's not at all suitable). Anyway, please do post **real code**, not some "like that" pseudo-code. – Cheers and hth. - Alf Feb 24 '18 at 10:02
  • @Cheersandhth.-Alf: I think in this particular case it looks more like the misapplication of a C convention, or perhaps the bad influence of `` typedefs. – Christian Hackl Feb 24 '18 at 10:43
  • Make sure your header has header guards. – Jesper Juhl Feb 24 '18 at 13:05

4 Answers4

3

No, it's not possible unless you use a type alias.

using UINT32 = CommonTypesCls::UINT32;

But you shouldn't do that because a class is not a suitable place to collect type definitions.

Put them in the global scope or in a namespace instead.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
2

Loooon ago, the C++ standard included namespace for exactly this purpose (btw, it appears you're re-inventing a wheel):

#include <cstdint>

namespace CommonTypesCls {
  using uint32 = std::uint32_t;
  using  int32 = std::int32_t;
}

int foo() {
  using namespace CommonTypesCls;
  uint32 a = 666;
}

If your types are specific to a particular class, for example dependent on a template parameters (such as std::vector<>::value_type), then you must use the scope resolution operator somewhere, but you can create an alias:

template<typename Container>
void bar(Container const&container)
{
  using value_type = typename Container::value_type;   // scope resolution
  value_type temporary_storage[4];
  for(auto const&x : container) {
    /* do something with x and using temporary_storage */
  }
}

However, when using auto variable declarations, it is often not necessary to explicitly specify the type.

Walter
  • 44,150
  • 20
  • 113
  • 196
1

A class is generally the wrong way to provide common definitions: use a namespace.

Also, the particular definitions you appear to want, of fixed size integer types, are provided by <stdint.h>.

That said, you can use public and protected definitions from a class C unqualified in code in a class derived from C. But that would be using two generally wrong tools: a class to provide definitions, and inheritance to gain access. Still it can be appropriate in some cases, e.g. for providing access to the names in an enum type.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

Sure:

#include "CommonTypesCls.h"
int main()
{
    typedef typename CommonTypesCls::UINT32 UINT32;
    int a = sizeof(UINT32);
    return 0;
}

Seriously though, not really.

Zebrafish
  • 11,682
  • 3
  • 43
  • 119