lovecreatesbea...@gmail.com wrote:
> Salt_Peter wrote:
> > lovecreatesbea...@gmail.com wrote:
> > > Salt_Peter wrote:
> > > > lovecreatesbea...@gmail.com wrote:
> > > <snip>
> > > > > 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.
> > >
> > > Thank you again, Peter.
> > >
> > > I understand that, for a user-defined class, operators like operator+,
> > > operator-, operator*, operator/ should be overridden to make the class
> > > do plus, minus, multiple and division respectively. (and only
> > > assignment operator is the exception, right?)
> >
> > Yes
> >
> > >
> > > you mean there are no built-in (or global) operators provided for a
> > > user-defined class, do I understand you correctly? I can't distinguish
> > > synthesized, predefined or automatically provided members or behavior.
> >
> > Yes, except for address_of and reference operator, as noted by the
> > author.
> > Type t;
> > Type& ref = t; // reference
> > Type* p = &t; // address_of
>
> I think the & in that book doesn't mean the reference. The author
> referred them to const address-of, non-const address-of operators. I
> don't have the book handy and may be wrong.
>
> > > What are are the differences between built-in and global things on a
> > > user-defined class?
> >
> > Built-in means provided by the compiler. global means globally
> > accessible.
>
> Operators including the assignment operator are all built-in things in
> the language. All functions including ctors, dctor are all not
> built-ins, right?
No, def ctor, copy ctor, d~tor and op= are not bulit-in. They are
simply provided if you chose not to define them. This is an important
characteristic because if i write:
A
{
int n;
public:
A(int x) : n(x) { }
};
The compiler now ceases to generate the default ctor: A() : n() { }. It
does, however, continue to supply a copy ctor and the assigment
operator (which do simple bit-copies of the members.
>
> And only operators are global, right?
not neccessarily, operators can be members of the class. And any
function can be global, except for ctors, copies and assignment ops.
>
> How can the built-in and global properties affect a class?
>
> > > When a programmer defines a class, are there some built-in (or global)
> > > operators or functions be added to the class automatically or
> > > synthesized by the language.
> >
> > Yes, we've discussed them all, ctor, copy, d~tor and op== are
> > automatically generated for you. The address_of and reference are
> > builtin. What you need to know is what happens when you start providing
>
> That is op= but not op== [double equal marks], right?
no, thats a typo, the op== must be defined and is usually global.
>
> Are there exactly 6 members:
> ctor
> copy ctor
> dctor
> op=
> & (address-of)
> & (reference)
> will be provided automatically for a class?
>
> I even learnt that someone mentioned two more:
> , (comma)
> . (dot)
and there are a few others like * and -> for pointers, * for
dreferencing, resolution operator :: and so on.
>
> I am still not clear on the exact list of all automatically provided
> members (data and or functions).
The best way to learn these is by coding.
>
> > you own ctors.
> >
> > >
> > > > 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.
> > >
> > > Are there only these four members:
> > >
> > > default constructor,
> > > copy constructor,
> > > assignment operator,
> > > destructor
> > >
> > > will not be inherited from parent classes?
> >
> > None are "inherited". The parent class can only use them. In some
> > cases, the destructor is declared as virtual to support polymorphism,
> > in that case its the virtuality thats inherited. The Child class needs
> > its own ctors, d~tor and op=.
> >
> > >
> > > As they will be provided (synthesized or predefined) for the child
> > > classes by the language.
> >
> > That doesn't change the fact that the Child class needs its own. When
> > the Child type is used to create an object, the compiler needs a base
> > class ctor to complete the creation of the object (2 ctors are
> > invoked).
> >
> > >
> > > (But how what about the others, how many in total members will be
> > > provided automatically? Which are those?
> >
> > That has already been discussed.
> > Look at it this way, if you write a class without definitions, isn't
> > the compiler still able to generate, copy, destroy and assign the
> > object? How does it do that?
> >
> > class A { };
> >
> > int main()
> > {
> > A a; // ctor
> > A another(a); // copy ctor
> > a = another; // assignment op
> > std::cout << "&a = " << &a << std::endl; // address_of
> > A& ref = another; // reference
> > std::cout << "&ref = " << &ref << std::endl;
> > // a = a + another; // fails, why?
> > }
> >
> > >
> > > And which can not be inherited? I ever wrote some example code, but
> > > still can not get a complete and detail list of these special
> > > functions. Thank you.)
> >
> > Thats a deep issue. What is inherited is any public or protected
> > function that isn't a ctor, d~tor or assignment. Why don't you code
>
> Thanks for this confirmation. I'll code more exercises and go back to
> ask for your help when I get problems.
>
> (but I ever leant that even private members can be inherited though
> cann't be referred to in the child classes or objects of child classes,
> am I right?)
>
> > these? Write 2 classes with std::cerr outputs in their functions and
> > watch what happens. You should leave inheritance out of the picture
> > until you understand how classes work.