LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2004, 08:05 AM   #1
markhod
Member
 
Registered: Sep 2003
Posts: 103

Rep: Reputation: 15
what does a cast in c++ actually do?


Hi,

If I have a class A and a class B that inherits from it and do:

vector<A> vec;

//fill vec with newed objects of type B

vec::iterator it_begin = vec.begin();
vec::iterator it_end = vec.end();

A *ptrA;
B *ptrB;

for (; it_begin!=it_end; it_begin++){

ptrA = *it_begin;
ptrB = dynamic_cast<ptrB>(ptrA);

//point X

}

At point X what does ptrA point to? is it a random memory address? or does it still point at the object of type B? In this case what is the correct way to delete a B - should I delete ptrA or ptrB at point X? Does it matter?

I also have the same question for doing const_cast's. Most of the webpages tell me how to do this, but dont explain the mechanics of what happens with where the pointers point to at the various stages which is what I would like to understand better.

Thanks,

Mark
 
Old 12-23-2004, 08:43 AM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
In your example, the dynamic_cast doesn't affect the ptrA at all. It essentially allows you to get a pointer to a B object in ptrB. If you were to print out the address values of ptrA and ptrB, you would likely get the same address even. (Note: This might not ALWAYS be the case, such as if you use multiple inheritance...)

After you do the cast , the difference between ptrA and ptrB will be that with ptrB you can access public methods/members that are specific to B, whereas with ptrA, you can only access the methods/members that are specific to A. dynamic_cast will use run-time type information to ensure that A can be cast to B.

const_cast is something that generally isn't a good practice to use. You can use that to cast away the const status of an object. The ONLY time it should be used, is if you have a function that takes a non-const parameter and you KNOW that it won't modify that value.

Here's some example code:
Code:
#include <iostream>

int DoSomething(int *p);

int main()
{
    const int i = 10;
    int *p;

    // Here it's ok to use the const_cast, we know that
    // DoSomething doesn't modify the value
    DoSomething(const_cast<int*>(&i));

    // This is dangerous, because we are modifiying a const!
    p = const_cast<int*>(&i);
    *p = 20;

    std::cout << i << std::endl;

}

int DoSomething(int *p)
{
    // Doesn't modify the value of p, just returns it

    return *p;

}
Result:
Code:
$ g++ -o ugh ugh.cpp
$ ./ugh
10
Note: The value of i wasn't changed as we might expect....

There is also static_cast, which is generally used to cast to naturally similar types. Like float to int, int to float, etc.

reinterpret_cast can generally be used to cast to dissimilar types. It can be used to cast one pointer type to any other pointer type. This also can be inherently unsafe. When possible, use the other casting types. I've used this to cast things like void* to char*, etc...
 
Old 12-23-2004, 09:01 AM   #3
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
I assume that you mean "vector<A*> vec;", yes?

At point X, ptrA contains the same address as ptrB but as far as the compiler is concerned you will only be able to do A-operations on it since ptrA might also be pointing to a non-B object which is derived from A. Imagine that you had a class C that inherits from A, and imagine that you filled the vector with a mix of B and C object pointers. In this case, inside the loop, ptrA would always be the address of the object and it's scope would be limited to A-operations, but ptrB would be zero unless you actually pointed to a B object in which case it would be a valid B*. A dynamic_cast will actually add machine code which checks the runtime-typeinfo of the object to make sure that it is of the type you claim. The static_cast meerly overrides the compiler error you would get by assuming that A is a B.

If you attempt to delete ptrA then this can result in something called "slicing". Whether that is a problem depends on how you have constructed the A-B reletionship. In this case you would not be calling B's destructor unless class A declared its destructor to be virtual, thereby enabling polymorphic destruction.

const_cast, like static_cast, doesn't change the numerical value of the pointer or cause any machine code to be generated. All it does is avoid a compiler error. They are both (const_cast and static_cast) ways of telling the compiler that you know more about what is going on than it does.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can you cast arrays? (c++) nadroj Programming 7 10-13-2005 01:34 AM
type cast alaios Programming 2 07-17-2005 04:58 AM
pointer from integer without a cast bcf2 Programming 7 12-30-2004 02:04 PM
gDesklets weather cast Hammett Linux - Software 1 06-04-2004 05:57 AM
Question on type cast cxel91a Programming 2 12-05-2003 09:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration