开发者

Type casting in c++

开发者 https://www.devze.com 2022-12-21 20:40 出处:网络
In c++ can we cast an object to an integer ? Clarifying my question - Suppose I have created an interface to handle file management task like create file, open file, read, write and I want to have o

In c++ can we cast an object to an integer ?

Clarifying my question - Suppose I have created an interface to handle file management task like create file, open file, read, write and I want to have one unique handle for every instance of a file. To achiev开发者_StackOverflow中文版e this can I create a file handle for each instance of the file interface by just type casting that instance to integer ?

To all - I hope now i am clear.


Not all objects. Every object in C++ has a type. That type of an object defines whether a cast to int exists, and if so, what algorithm is used.


If you have an object and want to cast it to int then you need to explicitly provide operator int for that class.


class File
{
 public:
  ...
  ...
  operator int() { return int(this); }
  ...
}

Then

File myFile;
int myFileHandle = myFile;


I would rather convert to a long type. This is safer when a pointer value is converted to an integral type on an x64 machine. You can just use reinterpret_cast<long>(myInterfacePointer) in that case.


you can, but it depend on the sizeof(YourObject) compared to sizeof(int), by casting any object to int you will access the first 4 bytes part of your object (assuming sizeof(int) == 4), if your object is smaller than sizeof(int) somewhere you will get access violation or crash. to cast :

`

 MyObject object;
 int castedObject = *((int*)&object);

`

to cast without pointer intermediate, you must provide typecast operator inside MyObject class. or you can declare global static function of int& operator=(const MyObject& object){...}


Edit: Since you are mapping files to a unique handle, you can use a std::vector<std::string> or a vector<shared_ptr<fstream> > or a vector<FILE*>.

On a POSIX-compliant system there is also fileno to convert a FILE* into its file descriptor which is an int.


To get the hash: Use the hash_value function from boost.

To convert any value to integer lexically: Use the lexical_cast<int> function from boost

To cast the value to integer: Just use (int)value.

For the above to work, the class you're going to convert needs to implement some special member functions e.g. operator<< and operator int.

To convert an object into an arbitrary unique integer, use (int)&value.

To get a random integer, use rand().


Why not just get the file descriptor/handle/whatever ID from the operating system and use that? Most systems have some kind of concept like that.

Consider using void * instead of int for the handles if you really want them to be pointers. Then casting a pointer-to-object to a handle is easy, and you can still hide the implementation away.


In response to @potatoswatter's comment to my first response.

I don't like the idea of casting objects. I would rather use a hashing function that produces an integer hash, say based on the filename or read/write flags. Now, I have to post another answer

class File
{
 public:
  ...
  ...
  operator int();
  ...
 private:
  char fileName[];
  int flags;
};

The integer conversion operator is now a hashing function:

File::operator int()
{
 int hash = 0;
 int c;
 char *str = fileName;

 while (c = *str++)
    hash += c;

 hash += flags;

 return hash;
}

I know the hash function is lousy. But you can avoid casts that are lousier and come up with your own hashing function that suits your needs better.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号