khushi
khushi

Reputation: 412

Type casting in c++

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 achieve 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.

Upvotes: 0

Views: 680

Answers (8)

ardsrk
ardsrk

Reputation: 2447

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.

Upvotes: 0

Potatoswatter
Potatoswatter

Reputation: 137940

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.

Upvotes: 0

ardsrk
ardsrk

Reputation: 2447

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

Then

File myFile;
int myFileHandle = myFile;

Upvotes: 1

kennytm
kennytm

Reputation: 523774

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().

Upvotes: 0

uray
uray

Reputation: 11572

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){...}

Upvotes: 0

Kerido
Kerido

Reputation: 2940

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.

Upvotes: 0

Naveen
Naveen

Reputation: 73503

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

Upvotes: 3

MSalters
MSalters

Reputation: 180305

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.

Upvotes: 5

Related Questions