lovecreatesbea...@gmail.com wrote:
> Salt_Peter wrote:
> > lovecreatesbea...@gmail.com wrote:
> > > "Salt_Peter 写道:"
> > > > lovecreatesbea...@gmail.com wrote:
> > > > > Could you tell me how many class members the C++ language synthesizes
> > > > > for a class type? Which members in a class aren't derived from parent
> > > > > classes?
> > > > >
> > > > > I have read the book The C++ Programming Language, but there isn't a
> > > > > detail and complete description on all the class members, aren't they
> > > > > important to class composing? Could you explain the special class
> > > > > behavior in detail? Thank you very much.
> > > >
> > > > Please read the following:
> > > > http://www.parashift.com/c++-faq-lite/how-to-post.html
> > >
> > > Thank you.
> > >
> > > Do you mean it is a homework problem or it is a FAQ problem? Why do you
> > > direct me to that link? I ever read Scott Meyers popular book Effective
> > > C++, in one print of its second edition, he said that a pair of &
> > > operators are synthesized by the language.
> > >
> > > I also think this question is important and not difficult to me. If you
> > > experts know the answer exactly, I would like to ask for your help
> > > sincerely.
> >
> > Those aren't & operators. You are referring to a copy constructor and
> > an assignment operator.
> > Scott Meyers' book explains that quite well.
> > The following are generated for you if a class is declared and defined
> > as empty and _if_ these are needed:
> >
> > class A { };
> >
> > // def ctor
> > A::A() { }
> > // copy ctor
> > A::A(const& copy) { }
> > // destructor
> > A::~A() { }
> > // assignment operator
> > A& Aoperator=(const A& rhv) { }
> >
> > What happens if you add members depends on whether the members are
> > primitive types or other user-types. If primitives, these are
> > allocated, not initialized and bit-copied, if user-types, their
> > corresponding def ctors/d~tors/copies are invoked.
> > It would be nice if you stated which case you wanted to investigate.
> > You need to specify which situation you'ld like considered because a
> > book can be written on that subject alone. A simple example would do
> > with members, perhaps.
> >
> > Otherwise, i'll end up rambling in all directions getting into endless
> > arguements with a few hundred people about what exactly happens at
> > exactly what point in time. I can seriously think of over 10 different
> > configurations off the top of my head involving everything from
> > primitive members, abstract bases, pure virtual diamonds, members of
> > members, virtual d~tors, private derived classes, containers, embedded
> > classes, and the list goes on and on.
>
> Hello peter, thank you very much for the reply.
>
> The author called it operator &. If you have the corresponding print of
> his book. He stated the mistake in errata <url:
> http://www.aristeia.com/BookErrata/ec++2e-errata_frames.html>. I will
> quote the errata of the description on synthesized operator& for
> further discussion.
>
> In the following errata, Mr. Meyers mentioned two more glossary:
>
> 1. "built-in address-of operator" and
> 2. "global operator& function".
These are free compiler operators that are provided automatically. They
are not synthesized.
Adress_of returns the variable's address (not the class).
>
> Is there the third name 3. "class defined operator"?
No, there is no op* synthesized except for pointers and references.
The op* is only predefined for primitive numerics where its a
mutiplication.
>
> What do the built-in, global things do to a class. Can they be data
> members or member functions? How do they affect the other members are
> not built-in and global in a class?
The built-ins are provided by the compiler to support the language.
There is nothing magic about how they work either.
class A { };
int main()
{
A a; // a variable
A* p_a; // a pointer to nothing
p_a = &a; // a pointer to the a variable (&a is address_of a)
A& r_a(a); // a reference to to a (r_a is a reference to a)
}
>
> I still want to know which members will (or have the chance to) be
> provided automatically for a class? Which members can not be inherited
> from the parent classes? Could you master help me?
i'm no master.
All virtual member functions are inherited by a Child class. Of course,
that doesn't inlude constructors and destructors nor any operator.
Which makes perfect sense since these are specialized for the Parent
class.
Look at it this way, any "function" that generates / initializes an
object of type Parent can't be overloaded or overridden in its
derivatives. The Child class can't overide a ctor, for example.
Why don't you simply investigate the issue:
#include <iostream>
class A
{
public:
A() { std::cerr << "A()\n"; }
virtual ~A() { std::cerr << "~A()\n"; }
A(const A& copy) { std::cerr << "A(...) copy\n"; }
A& operator=(const A& rhv)
{
std::cerr << "A op=\n";
if(&rhv == this) return *this;
// assign members
return *this;
}
};
class Child : public A
{
};
int main()
{
Child child; // ctor
Child another = child; // copy
child = another; // assignment
Child* p_child = &child; // pointer to child
std::cout << "p_child = " << p_child << std::endl;
}
/*
A()
A(...) copy
A op=
p_child = 0x7fffc20ce6e0
~A()
~A()
*/