class Base{};
class Derived: public Base{};
int main()
{
Base B;
Derived D;
Base B1 = D;//OK
Derived D1 = B;//error: conversion from ‘Base’ to non-scalar
//type ‘Derived’ requested
return 1;
}
I know derived class has a is-a relationship with base class.
What stops the Derived D1 having values from B and remaining member variables(if any) with some garbage value?
Or
What does the error message
conversion from 'Base' to non-scalar type 'Derived' requested Derived D1 = B;
say? What is a scalar type?
The statement
Derived D1 = B;
is an initialization, not an assignment (even if it looks like an assignment).
It attempts to use the Derived
copy constructor, but that copy constructor takes an argument Derived const&
. And the B
instance can't be automatically converted down to a full Derived
.
If you really want a slice assignment – assigning only to the Base
slice of D1
– then you can explicitly use the Base::operator=
:
Derived D1;
D1.Base::operator=( B );
Another way to express that:
Derived D1;
static_cast<Base&>( D1 ) = B;
But it smells bad. ;-)
Re
” What is a scalar type?
That's the same word as in “scale”. A scalar type provides a single magnitude value, so that values of the type can be compared with ==
(and ideally also <
). However, in C++ pointers and even member pointers are regarded as scalar types:
C++11 §3.9/9 [basic.types]:
” Arithmetic types (3.9.1), enumeration types, pointer types, pointer to member types (3.9.2), std::nullptr_t, and cv-qualified versions of these types (3.9.3) are collectively called scalar types .
If B
's member variables are declared private, then that would stop D1
from having B
's values.
As far as your second question, You can assign Base B1 = D;
because the compiler knows D
is a derived class from B
. However, in Derived D1 = B;
, the compiler has no indication of that relationship.
As far as to your question of what a scalar is, scalars are ints, chars, pointers, etc. They are different from structs and classes, which are user-defined types.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.