Hello

This is my base

class BaseClass
{
// base has no body
};

and child

class ChildClass : public BaseClass
{
public:
  void testMethod();
};

I do

BaseClass* base = new BaseClass();
ChildClass* c = static_cast<ChildClass*>(base);
c->testMethod();

Above compile and ChildClass::testMethod() executes fine.

my memory is allocated by BaseClass (new BaseClass()), but I can cast it to ChildClass* and executes fine.

How is this possible? Is it auto initializ everything for ChildClass with static_cast?

The example you posted probably works because child class has no data members, just one method. When you allocate a class with new the compiler only allocates data because the methods are already in memory. If you add some data members to child class I'll bet the program won't work right when trying to access the data.

use static_cast to convert base class pointer to derived class pointer is allowed but not safe.
you can use reinterpret_cast if you guarantee this operation to be safe, it is usually faster than static_cast.
you may use dynamic_cast to make it converted at runtime.

[EDIT]
Ancient Dragos point is correct, when print the member value, it's a garbage. But I was expected a runtime exception. I was wrong.
[EDIT]

Thank you both of you for replying,
I am still confused, beacuse even if i add data embers it doesn't even run time fail

class BaseClass
{

};

class ChildClass : protected BaseClass
{
public:
	ChildClass();
	void testMethod();
	int getX();

private:
	int x;
};

// main
BaseClass* base = new BaseClass();


ChildClass* c = static_cast<ChildClass*>(base);
c->testMethod();

cout << c->getX() << "\n";

I really don't understand, how it cast new BaseClass() to ChildClass*

Well, from the reference page on static_cast:

It can also cast pointers or references down and across the hierarchy as long as such conversion is available and unambiguous. No runtime checks are performed.

You see, static_cast is a compile-time casting operator. It will never do runtime checks (in fact it can't). I am guessing that proper compilers would warn you about such unsafe casts. But, it is allowed. Say, that for some reason, you have a pointer to a BaseClass, but you know that it must actually be pointing to an object of DerivedClass, then, using static_cast would be faster than the only alternative (i.e. dynamic_cast) and still "safe" (well.. as safe as you are sure of the "real" type of the object pointed to).

@deltamaster: >>you can use reinterpret_cast if you guarantee this operation to be safe, it is usually faster than static_cast.
You are wrong. reinterpret_cast is by far the least safe casting operator and it is not faster than static_cast. Also, in this case, it will not work at all because reinterpret_cast does not perform any shifting of the pointer's value. When you cast pointers to classes up or down the hierarchy, there can be an offset necessary on the "this" pointer, especially when multiple inheritance is involved. static_cast will blindly apply this offset at compile-time, while dynamic_cast will safely apply this offset at run-time (with a check). So, static_cast is not as safe as dynamic_cast, but it has no run-time overhead whatsoever (neither does reinterpret_cast, but it is basically erroneous to use it for any non-primitive pointer type).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.