Re: size of class same as base class if no new data members?
guy.tristram@gmail.com wrote:
Can I assume that if I have:
class A
{
/* a few built in types */
You mean, there are data members here? 'cause your class looks like it
has no data members at all...
};
class B : public A
{
private:
//prevent copy and assignment
B( B const & );
operator=( B const & );
};
instances of class A will be the same size as instances of class B?
Since 'B' adds no data and no virtual functions or virtual base classes,
then yes, most likely.
More particularly, if I have:
std::vector< A > v(2);
B *b = static_cast< B * >( &v[0] );
++b;
will b point to the second element of v?
No. The code has undefined behaviour. You can't use 'static_cast' that
way. The object at the address &v[0] is of type 'A', and it is not a
subobject of some other object of type 'B'.
> Does the standard offer any
guarantees?
Of course not.
> My motivation is that I want to be able to grow a vector
of As, but prevent clients from copying them thereafter.
It would seem that you're solving a wrong problem. If you care to
explain what you're trying to accomplish, folks here might have
suggestions on what you should do to get it.
> I realize
that I am then using As as though they were Bs and I should perhaps
inherit the other way around, making the copy constructor and
assignment operator protected in the client version, then re-
publicizing them in the internal version, but that would mean
explicitly implementing them.
Uh... Don't know.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask