Can anyone tell me why this doesn't compile:
struct A { };
struct B : public A { };
int main()
{
B b;
A* a = &b;
B* &b1 = static_cast<B*&>(a);
return 0;
}
Now, if you replace the static cast with:
B* b1 = static_cast<B*>(a);
then it does compile.
Edit: It is obvious that the compiler treats A* and B* as independent types, otherwise this would work. The question is more about why is that desirable?
B is derived from A, but B* isn't derived from A*.
A pointer to a B is not a pointer to an A, it can only be
converted to one. But the types remain distinct (and the
conversion can, and often will, change the value of the
pointer). A B*& can only refer to a B*, not to any other
pointer type.
non-constant lvalue reference (B*&) cannot bind to a unrelated type (A*).
Handling of references is something the compiler does for you, there should be no need to cast to reference.
If we refactor the code to:
B b;
A* a = &b;
B* b_ptr = static_cast<B*>(a);
B*& p1 = b_ptr;
It will compile.
You are trying to cast an A* to a B*. This is the wrong way around and not very useful. You probably want to store a pointer to derived in a pointer to base, which is useful and doesn't even need a cast.
I suppose a dynamic_cast might work here, but the result is implementation defined if I'm not mistaken.
Related
I have a doubt about downcasting an object in C++.
Here it comes an example:
class A { }
class B : public A {
public:
void SetVal(int i) { _v = i; }
private:
int _v;
}
A* a = new A();
B* b = dynamic_cast<B*>(a);
b->SetVal(2);
What would it happen with this example? We are modifying a base clase like if it is a child one... how does it work related with the memory?
With this cast... Is it like creating an instance of B and copying the values of A?
Thanks
A* a;
This just gives you a pointer to an A. It doesn't point anywhere in particular. It doesn't point at an A or B object at all. Whether your code works or not depends on the dynamic type of the object it is pointing at.
So there are two situations you might want to know about. First, this one:
A* a = new A();
B* b = dynamic_cast<B*>(a);
b->SetVal(2);
This will give you undefined behaviour because the dynamic_cast will return a null pointer. It returns a null pointer when the dynamic type of the object is really not a B. In this case, the object is an A. You then attempt to dereference the null pointer with b->SetVal(2), so you get undefined behaviour.
A* a = new B();
B* b = dynamic_cast<B*>(a);
b->SetVal(2);
This will work fine because the object really is a B object. The dynamic cast will succeed and the SetVal call will work just fine.
However, note that for this to work, A must be a polymorphic type. For that to be true, it must have at least one virtual member function.
That shouldn't even compile, because the classes aren't polymorphic so you can't use dynamic_cast.
If it did, it would be undefined behavior.
class B;
class A{
B *b;
public:
void operator= (B *b){
this->b = b;
}
};
B *b = new B()
A *a = new A();
a = b;
I get a "cannot convert B* to A*" error.
Is there a way around this?
Now, if there is a way, and if I use something like:
a = NULL;
Which operator "=" would be used?
You have assigned the pointer instead of the object. Simply replace the last instruction with:
*a = b;
To answer the second question: NULL can be defined in more than one way in the compiler (as of the latest standard, either as the integral 0 or the literal nullptr). Pointers can also be cast to pointer of other types, but passing a void* to an overloaded function that takes an int* or a long* may make the compiler unable to resolve the function being called.
If however, you want to avoid NULL, simply make operator(B& b) instead. References are sure to be pointing at an object.
Your operator= provides assignment from a B* to an A. Your code does not provide a conversion from a B* to a A* (as the error message shows). As such, a=NULL will not use the assignment operator you provided, since a is a pointer, not an A. Your code allows assignment from a B* to an A, like A a= new B();.
If you meant to be using actual objects instead of pointers, remove all the * from your code:
class B{};
class A{
B b;
public:
void operator= (const B& b){ //pass non-primitives by const reference
this->b = b;
}
};
B b;
A a;
a = b;
If you wanted to be using pointers, the only "useful" way to assign a B* to an A* is if a B object derives from A. That appears to not be what you're doing, so assigning a B* to an A* would make no sense in your code.
Change your statement that assigns to a to:
*a = b;
can somebody please help me with an error
conversion from `A' to non-scalar type `B' requested
I have class A and derived from it B, but I have problems with these rows:
A a(1);
A *pb = new B(a);
B b = *pb; //here I have an error
thanks in advance for any help
class A {
protected:
int player;
public:
A(int initPlayer = 0);
A(const A&);
A& operator=(const A&);
virtual ~A(){};
virtual void foo();
void foo() const;
operator int();
};
class B: public A {
public:
B(int initPlayer): A(initPlayer){};
~B(){};
virtual void foo();
};
edited
I have this code and (I can't change it):
A a(1);
A *pb = new B(a);
B b = *pb;
I tried to create constructor for B:
B::B(const A & a):
player(a.player){}
B& B::operator=(const A& a){
if(this == &a){
return *this;
}
player = a.player;
return *this;
}
but it gives me an error, really need help from professionals
Your problem is due to static type checking. When you have this line:
A *pb = new B(a);
The static type of pb is A * and it's dynamic type is B *. While the dynamic type is correct, the compiler is checking the static type.
For this simple code, since you know the dynamic type of pb is always a B, you can fix this with a static cast:
B b = *static_cast<B *>(pb);
But be warned that if the dynamic type of pb was an A * the cast would cause undefined behavior.
When you dereference an 'A' pointer, you get an 'A' even if it points to a 'B'. Polymorphism does not come into play here! To preserve the 'B' properties to the 'A' object you should properly cast the initialization as explained in some of the other answers.
In such situations a dynamic cast is most appropriate. Dynamic cast will invoke the runtime type system to figure out the "real" type of bp and will return 0 if it can't be cast to the requested type. As you know the real type you could also use static_cast here but generally this isn't the case in such situations.
B* b = dynamic_cast<B*>(pb);
*pb will give you a A& and not a B&. It's just like pb being an A* and not a B* even though the actual object is a B.
B b = *pb will attempt to copy-construct a B using a synthesised copy constructor. The copy constructor will therefore be looking for a B& as its argument. You have no constructor to make a B from an A, hence the error.
As #R Samuel Klatchko says you could just cast it or, in the case you've given, you could just make pb actually be a B*.
You are trying to assign an object of type A to object of type B. Which is not allowed unless you define a type cast operator. pb is a pointer to A object and in general it is not a pointer to B (in your case it is, but it doesn't matter for the compiler, since it's declared as pointer to A). To make such assignement possible, you first need to down-cast pb to pointer of B (as R Samuel Klatchko pointed out, in your case static_cast is perfectly fine; in other cases you might need use dynamic_cast).
I have a question about C++, how to assign a Base object to a Derived object? or how to assign a pointer to a Base object to a pointer to a Derived object?
In the code below, the two lines are wrong. How to correct that?
#include <iostream>
using namespace std;
class A{
public:
int a;
};
class B:public A{
public:
int b;
};
int main(){
A a;
B b;
b = a; //what happend?
cout << b.b << endl;
B* b2;
b2 = &a; // what happened?
cout << b->b << endl;
}
It makes no sense to assign a base object to a derived (or a base pointer to a derived pointer), so C++ will do its best to stop you doing it. The exception is when the base pointer really points at a derived, in which case you can use dynamic cast:
base * p = new derived;
derived * d = dynamic_cast <derived *>( p );
In this case, if p actually pointed at a base, the pointer d would contain NULL.
When an object is on the stack, you can only really assign objects of the same type to one another. They can be converted through overloaded cast operators or overloaded assignment operators, but you're specifying a conversion at that point. The compiler can't do such conversions itself.
A a;
B b;
b = a;
In this case, you're trying to assigning an A to a B, but A isn't a B, so it doesn't work.
A a;
B b;
a = b;
This does work, after a fashion, but it probably won't be what you expect. You just sliced your B. B is an A, so the assignment can take place, but because it's on the stack, it's just going to assign the parts of b which are part of A to a. So, what you get is an A. It's not a B in spite of the fact that you assigned from a B.
If you really want to be assigning objects of one type to another, they need to be pointers.
A* pa = NULL;
B* pb = new B;
pa = pb;
This works. pa now points to pb, so it's still a B. If you have virtual functions on A and B overrides them, then when you call them on pa, they'll call the B version (non-virtual ones will still call the A version).
A* pa = new A;
B* pb = pa;
This doesn't work. pa doesn't point B, so you can't assign it to pb which must point to a B. Just because a B is an A doesn't mean than an A is a B.
A a;
B* pb = &a;
This doesn't work for the same reason as the previous one. It just so happens that the A is on the stack this time instead of the heap.
A* pa;
B b;
pa = &b;
This does work. b is a B which is an A, so A can point to it. Virtual functions will call the B versions and non-virtual ones will call the A versions.
So, basically, A* can point to B's because B is an A. B* can't point to A because it isn't a B.
The compiler won't allow that kind of thing. And even if you manage to do it through some casting hack, doing so makes no sense. Assigning a derived object to a pointer of a base makes sense because everything that base can do, derived can do. However, if the opposite case was allowed, what if you try to access a member defined in derived on a base object? You would be trying to access an area of memory filled with garbage or irrelevant data.
b = a; //what happend?
This is plain illegal - A is not B, so you can't do it.
b2 = &a; // what happened?
Same here.
In neither case, the compiler wouldn't know what to assign to int b, hence he prevents you from doing that. The other way around (assigning Derived to Base) works, because Base is a subset of Derived.
Now if you would tell us, what exactly you want to achieve, we might help you.
If it's a case of assigning an A that is known to be a Derived type, you can do a cast:
A* a = new B();
B* b = dynamic_cast<B>(a);
Just remember that if a is not a B then dynamic_cast will return NULL. Note that this method works only on pointers for a reason.
Derived object is a kind of Base object, not the other way around.
#include "iostream"
class A {
private:
int a;
public :
A(): a(-1) {}
int getA() {
return a;
}
};
class A;
class B : public A {
private:
int b;
public:
B() : b(-1) {}
int getB() {
return b;
}
};
int main() {
std::auto_ptr<A> a = new A();
std::auto_ptr<B> b = dynamic_cast<std::auto_ptr<B> > (a);
return 0;
}
ERROR: cannot dynamic_cast `(&a)->std::auto_ptr<_Tp>::get() const
Well, std::auto_ptr<B> is not derived from std::auto_ptr<A>. But B is derived from A. The auto_ptr does not know about that (it's not that clever). Looks like you want to use a shared ownership pointer. boost::shared_ptr is ideal, it also provides a dynamic_pointer_cast:
boost::shared_ptr<A> a = new A();
boost::shared_ptr<B> b = dynamic_pointer_cast<B> (a);
For auto_ptr, such a thing can't really work. Because ownership will move to b. But if the cast fails, b can't get ownership. It's not clear what to do then to me. You would probably have to say if the cast fails, a will keep having the ownership - which sounds like it will cause serious trouble. Best start using shared_ptr. Both a and b then would point to the same object - but B as a shared_ptr<B> and a as a shared_ptr<A>
dynamic cast doesn't work that way. A : public B does not imply auto_ptr<A> : public auto_ptr<B>. This is why boost's shared_ptr provides shared_dynamic_cast. You could write an auto_ptr dynamic cast though:
template<typename R, typename T>
std::auto_ptr<R> auto_ptr_dynamic_cast(std::auto_ptr<T>& in) {
auto_ptr<R> rv;
R* p;
if( p = dynamic_cast<R*>( in.get() ) ) {
in.release();
rv = p;
}
return rv;
}
Just be aware of what happens here. Since auto_ptrs have ownership semantics, a successful downcast means the original more generally typed, auto_ptr no longer has ownership.
The reason is that auto_ptr is not actually a pointer. It's a smart pointer which is a pointer wrapper but not actually a pointer. The type that is passed as a template style argument to dynamic_cast must be a true pointer (or reference) type.
http://msdn.microsoft.com/en-us/library/cby9kycs(VS.80).aspx
You're trying to cast a A* (returned by a.get()) to std::auto_ptr<B>, and since the second is not even a pointer type this fails. Probably you just want to cast it to B*:
std::auto_ptr<A> a(new A());
std::auto_ptr<B> b(dynamic_cast<B*>(a.get()));
This will still not compile, because A and B aren't polymorphic types. A needs to have a virtual function in order to make the types polymorphic. This will compile, but the cast will just throw std::bad_cast, since it isn't really a B*.
And even if it were a B*, it will fail in horrendous ways if you try to use it. Both std::auto_ptrs a and b will assume they own the object and free it later on, resulting in all kinds of memory corruption. You probably want to use a.release() after the cast was successful.
I think c++ stores RTTI (run time type information) in the vtable. Hence to use dynamic_cast<> with an instance object, the object needs have 'vtable'. C++ creates vtable only when at least one function is declared 'virtual' in the class.
The class A and Class B there are no virtual functions. This could be reason for the dynamic_cast failure. Try declaring a virtual destructor in base class.