?
Solved

Is it possible...

Posted on 2003-02-21
3
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

770 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