typedef

add preddefined data for typedef enums in c

What is the best approach to define additional data for typedef enums in C? Example: typedef enum { kVizsla = 0, kTerrier = 3, kYellowLab = 10 } DogType; Now I would like to define names for each, for example kVizsla should be "vizsla". I currently use a function that returns a srting using a large switch block. ...

How do you read C declarations?

I have heard of some methods, but none of them have stuck. Personally I try to avoid complex types in C and try to break them into component typedef. I'm now faced with maintaining some legacy code from a so called 'three star programmer', and I'm having a hard time reading some of the ***code[][]. How do you read complex C declaration...

Is it correct to use inheritance instead of name aliasing in c#?

In other words, is it correct to use: public class CustomerList : System.Collections.Generic.List<Customer> { /// supposed to be empty } instead of: using CustomerList = System.Collections.Generic.List<Customer> I'd rather use the first approach because I'd just define CustomerList once, and every time I needed a customer list ...

Equivalent of typedef in C#

Is there a typedef equivalent in C#, or someway to get some sort of similar behaviour? I've done some googling, but everywhere I look seems to be negative. Currently I have a situation similar to the following: class GenericClass<T> { public event EventHandler<EventData> MyEvent; public class EventData : EventArgs { /* snip */ ...

typedefs for templated classes?

Is it possible to typedef long types that use templates? For example: template <typename myfloat_t> class LongClassName { // ... }; template <typename myfloat_t> typedef std::vector< boost::shared_ptr< LongClassName<myfloat_t> > > LongCollection; LongCollection<float> m_foo; This doesn't work, but is there a way to achieve a si...

Why should we typedef a struct so often in C?

I have seen many programs consisting of structures like the one below typedef struct { int i; char k; } elem; elem user; I have seen this many times. Why is it needed so often? Any specific reason or applicable area? ...

Polluting the global namespace

I think most C++ programmers here would agree that polluting the global namespace is a bad idea, but are there times when this rule can be ignored? For example, I have a type that I need to use all over a particular application - should I define it thus: mytypes.h typedef int MY_TYPE; foo.cpp MY_TYPE myType; Or use a namespace: m...

Enforce strong type checking in C (type strictness for typedefs)

Is there a way to enforce explicit cast for typedefs of the same type? I've to deal with utf8 and sometimes I get confused with the indices for the character count and the byte count. So it be nice to have some typedefs: typedef unsigned int char_idx_t; typedef unsigned int byte_idx_t; With the addition that you need an explicit cast...

typedef a std::string - Best practice

I am writing a library in standard C++ which does the phonetic conversion. I have used std::string as of now. But in future I may have to change this to someother (std::wstring or something else). So I need to write my library in such a way that I can switch this easily. I have done the following so far to achieve this. Created a heade...

Difference between "struct foo*" and "foo*" where foo is a struct?

In C, is there a difference between writing "struct foo" instead of just "foo" if foo is a struct? For example: struct sockaddr_in sin; struct sockaddr *sa; // Are these two lines equivalent? sa = (struct sockaddr*)&sin; sa = (sockaddr*)&sin; Thanks /Erik ...

Inheritance instead of typedef

C++ is unable to make a template out of a typedef or typedef a templated class. I know if I inherit and make my class a template, it will work. Examples: // Illegal template <class T> typedef MyVectorType vector<T>; //Valid, but advantageous? template <class T> class MyVectorType : public vector<T> { }; Is doing this advantageous s...

Destructors of builtin types (int, char ect..)

In C++ the following code gives a compiler error: void destruct1 (int * item) { item->~int(); } This code is nearly the same, I just typedef the int to another type and something magic happends: typedef int myint; void destruct2 (myint * item) { item->~myint(); } Why does the second code works? Does an int gets a destructor ju...

When don't I need a typedef?

I encountered some code reading typedef enum eEnum { c1, c2 } tagEnum; typedef struct { int i; double d; } tagMyStruct; I heard rumours that these constructs date from C. In C++ you can easily write enum eEnum { c1, c2 }; struct MyStruct { int i; double d; }; Is that true? When do you need the first variant? ...

Is it acceptable to add a "using namespace" immediately after the namespace declaration?

I have a small namespace containing some type definitions, which I use to make my code look cleaner. However I don't want to have to add a "using namespace ..." line to every file that uses one of these types, after all I already have to add a #include for the file. MyFile.cpp: #include "typedefs.h" void Bob() { IntList^ list = gcne...

C: What is the best way to modify an int pointer passed via a typedef?

I have a typedef int my_type and i have a function which looks like void my_func (my_type* x); How should I use this func to modify x using the best practice? ...

typedef std containers?

I wanted to do typedef deque type; //error, use of class template requires template argument list type<int> container_; But that error is preventing me. How do I do this? ...

typedef equivalent for overloading in c#

I have a bunch of code that has lots integers with different meanings (I'd rather a general solution but for a specific example: day-of-the-month vs. month-of-the-year vs. year etc.). I want to be able to overload a class constructor based on these meanings. For example int a; // takes role A int b; // takes role B var A = new Foo(a)...

Valid use of typedef?

I have a char (ie. byte) buffer that I'm sending over the network. At some point in the future I might want to switch the buffer to a different type like unsigned char or short. I've been thinking about doing something like this: typedef char bufferElementType; And whenever I do anything with a buffer element I declare it as bufferEle...

self referential struct definition?

Hi I haven't been writing C for very long, and so I'm not sure about how I should go about doing these sorts of recursive things... I would like each cell to contain another cell, but I get an error along the lines of "field 'child' has incomplete type". What's up? typedef struct Cell { int isParent; Cell child; } Cell; Thanks, ...

Difference between 'struct' and 'typedef struct' in C++?

In C++, is there any difference between: struct Foo { ... }; and typedef struct { ... } Foo; ...