Jump to content
  • Advertisement
Sign in to follow this  
luasitdown

Casting pointer

This topic is 4673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

class A{...//class stuff} class B: public A{...//class stuff} A * pA = new A; B * pB = new B; B* p = static_cast<pB*>(pA); I wonder if space pA points to gets more large and be equal to B?

Share this post


Link to post
Share on other sites
Advertisement
What you are doing is the one thing, which is not possible, as b is a chld of a, you can
assign a pointer to object of B to a pointer of an object of A, but not vice versa.

By the way pointer are just addresses on the heap so their size is always the same on one system...that's why you can do so much with things like void* or int*....

Share this post


Link to post
Share on other sites

class A{};
class B : public A {};

int main ()
{
A* pA = new A;
B* pB = new B;
A* pAB = pB;
B* pBA = dynamic_cast< B* >( pAB );
}


You can cast a type of a base class to a type of an inherited class from that base class, that's called down casting and to do this you can use dynamic_cast. If it fails dynamic_cast returns a NULL pointer or throws a bad_cast exception if casting references.

In the example above the dynamic_cast won't fail since pAB points to a B object.

Share this post


Link to post
Share on other sites
see,still confused . ogre example seem do this way.


an example from ogre.
OrgeNode.h OrgeSceneNode.h

SceneNode* SceneNode::createChildSceneNode(const Vector3& translate,
const Quaternion& rotate)
{
return static_cast<SceneNode*>(this->createChild(translate, rotate));
}

virtual Node* createChild(
const Vector3& translate = Vector3::ZERO,
const Quaternion& rotate = Quaternion::IDENTITY )





createChild() is member of class node.
class node is base and class scenenode derive from it.

so it cast Node* to SceneNode* . so what happen?


Be simple :

class A
{A* getA(){return A*;}};

class B : public A
{B* getB(){return static_cast<B*>(getA())}};

[Edited by - luasitdown on July 29, 2005 9:18:20 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by luasitdown
I wonder if space pA points to gets more large and be equal to B?

No.

Casting won't change the fact that pA does not point to a B, and any code using pointer p will probably fail.

[Edited by - JohnBolton on July 30, 2005 4:15:58 PM]

Share this post


Link to post
Share on other sites
I got it.
I see.

class Base
{
virtual Base * create() = 0;
Base * getA(){create();}
};
class Derived
{
virtual Base * create {return new Derived;}
Derived *getB(){return static_cast<B*>getA();}
};

so these kinds of way to use derivation often adopted in oo programming?

Share this post


Link to post
Share on other sites
Nothing actually happens in Ogre's code except that someone can call "getChildSceneNode" and not have to cast to a SceneNode * themselves later. No additional code is generated if the compiler is smart.

What you see here is *only* to make a common create-and-typecast operation easier to write by an end user.

[Edited by - Nypyren on July 29, 2005 10:03:20 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by luasitdown
so these kinds of way to use derivation often adopted in oo programming?


That is called the "Prototype" or "Clone" pattern. In simplest form, it looks like this:
    class Base
{
public:
Base * Clone() = 0;
};
class Derived : public Base
{
public:
Base * Clone() { return new Derived; }
};

Share this post


Link to post
Share on other sites
no.
the snippet work

#include <iostream>

class A
{public:
int i;
};

class B :public A
{public:
char c;
};

main()
{
A *a = new A;
B *b = new B;
B *b1 = static_cast<B*>(a);
b1->c = 'c';
}

Share this post


Link to post
Share on other sites
Quote:
Original post by luasitdown
no.
the snippet work
*** Source Snippet Removed ***


If it works, it is only by accident. Your code has undefined behaviour. It may work once, and start WW3 the next.

You really are writing outside of the object.


#include <iostream>

const int N = 8;

class A
{
public:
short i[N];
};

class B :
public A
{
public:
short j[N];
};

int main()
{
A *a = new A;
for(int k=0; k<N; ++k)
a->i[k] = k;

B *b = new B;
for(int k=0; k<N; ++k)
{
b->i[k] = k;
b->j[k] = k+N;
}

B *b1 = static_cast<B*>(a);
for(int k=0;k<N; ++k)
b1->j[k] = k+N;

for(int k=0;k<N;++k)
std::cout << b->i[k] << ' ';
std::cout << std::endl;
}


Prints "12 13 14 15 4 5 6 7" on my machine, when it should print "0 1 2 3 4 5 6 7". This means that the memory allocation routines have placed b right after a in memory and writing to the elements of b1->j overwrote those of b->i.

Your cast didn't magically provide more memory. All you did was tell the compiler "go ahead, you can write there" when in fact it could not.

And if I were to try and delete b, I'm sure - based on what I know about how GCC handles memory allocation - that the program would crash, as I have overwritten critical data structures that new placed before b (and thus between a and b).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!