Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Is it possible...

Posted on 2003-02-21
3
Medium Priority
?
222 Views
Last Modified: 2012-05-04
Is it possible (in VC++ 6.0) to take an instance of an object, convert it to an integer (get the actual integer value), then take that integer and convert it back to the object?

(i.e.)

class Entry
{
  // data
}

int main(void)
{
  int
    num;

  Entry
    e1
    e2;
 
  num = (int)e1;  // cast e1 to int

  e2 = (Entry)num;  // cast num to Entry

  return 0;
}

I know I can overload the = operator to assign e1 to e2, but I am just curious if this could be done. When c-casting (like above), most compilers will just warn you, but VC++ gives you an error.  There has to be a way around this though - right?

thanks

Ernie
0
Comment
Question by:fastawdtsi
3 Comments
 
LVL 12

Accepted Solution

by:
Salte earned 240 total points
ID: 7992952
This is possible if and only if e1 has a cast operator defined for int:

class Entry {
public:
   operator int() { .... }

   ...
};


Entry e;
int k = (int)e;

will then call that function defined as the cast operator.

It is also possible to cast that int back to Entry:

e = (Entry)k;

provided the class Entry has a constructor that takes an int as argument:

class Entry {
public:
   Entry(int u) { ... }
};

Then that constructor is called to construct an Entry from an int.

However, it will still fail unless Entry can be assigned to. If you have no assignment operator C++ writes one for you which do a bitwise copy. However, if you do write one that will be used. If you write one but make it private you can't assign to that object from outside and so it will fail. However, it won't fail converting the value to Entry from int, but it will fail when it then tries to assign that Entry value (newly constructed one) into the Entry variable e.

However, I suspect this isn't the answer you were looking for. You appear to say something like you want the exact bits of the Entry variable converted to int and then converted back again etc. If this is possible or not depends on how many bits an Entry is and how the class is defined.

If the class has specific constructors so that a bitwise copy of the object is a bad idea, then you will generally wreck havoc if you try to do this kind of bitwise trickery.

If the class Entry is size char then you can always convert it to int. You can also convert that same value back to Entry and it will work. However, if you change the int value to a value outside that of char the excess bits will be lost when converting back to Entry.

If the class Entry is bigger than an int, i.e. sizeof(Entry) > sizeof(int) then you cannot in any meaningful way convert the Entry object to an int.

To do this kind of bitwise conversion you can do:

int k = reinterpret_cast<int>(e);

and

Entry e2 = reinterpret_cast<Entry>(k);

Be aware that there is no constructor called in this case, it is just a raw copy of the bits. Reinterpret_cast is therefore not a good choice in most cases.

Note that reinterpret_cast<Entry>(k) will fail with compile time error in many cases. For example if sizeof(Entry) != sizeof(int). I also suspect it will fail if Entry has constructors since the reinterpret_cast involves making an Entry without calling a constructor.

You generally don't want to do things like this. You definitely don't want to do it if you are not sure what you're doing or if you are not sure that this is what you want to do.

Alf
0
 
LVL 1

Expert Comment

by:Hoegje
ID: 7992966
You can convert from your class (Entry) to int like this :

class Entry{
  public:
    operator int(){ // do whatever needs to be done }
}

This is called a conversion constructor. But it might be wise to create a function like Entry::make_int() that returns the integer for you, before using the operator function. That way you can always tell when you are converting to an int.

The other way around (converting from int to Entry) could be done by making a constructor for Entry with an int as parameter, and doing everything inside that constructor:

class Entry{
  public:
    Entry(int x);
}

0
 

Author Comment

by:fastawdtsi
ID: 7993007
That's all I needed to know...Thanks!!!

Ernie
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question