What member functions C++ provides and calles silently

L

lovecreatesbeauty

When write a class, we compose some member functions for the class and
the compiler provided the others implicitly. Scott Meyers's `Effective
C++ (2nd)' says in item 45 that 6 member functions will be provided
then called by standard-compliant compilers for a class: (1)default
constructor, (2)copy constructor, (3)destructor, (4)assignment
operator, (5)address-of operator (non-const), (6)address-of operator
(const).

Does standard-compliant compiler only provide these 6 member functions
implicitly? Are there some others?

Can't (only) these 6 member functions be derived from base (father)
class?

Does it mean that we can/have avoided most of the problems if we take
care of these functions.
 
B

ben

lovecreatesbeauty said:
When write a class, we compose some member functions for the class and
the compiler provided the others implicitly. Scott Meyers's `Effective
C++ (2nd)' says in item 45 that 6 member functions will be provided
then called by standard-compliant compilers for a class: (1)default
constructor, (2)copy constructor, (3)destructor, (4)assignment
operator, (5)address-of operator (non-const), (6)address-of operator
(const).

The compiler will synthesize these functions if and only if we don't provide
our own.
Does standard-compliant compiler only provide these 6 member functions
implicitly? Are there some others?
no.


Can't (only) these 6 member functions be derived from base (father)
class?

C++ doesn't enforce inheritance. There's no one universal root class as in
Java.
Does it mean that we can/have avoided most of the problems if we take
care of these functions.

You can take advantage of these functions but it doesnt guarantee fewer
problems.
 
C

codigo

lovecreatesbeauty said:
When write a class, we compose some member functions for the class and
the compiler provided the others implicitly. Scott Meyers's `Effective
C++ (2nd)' says in item 45 that 6 member functions will be provided
then called by standard-compliant compilers for a class: (1)default
constructor, (2)copy constructor, (3)destructor, (4)assignment
operator, (5)address-of operator (non-const), (6)address-of operator
(const).

Not quite right. the 3 constructors-destructors are not called. They are
invoked or invokeable. The compiler ceases to supply a default ctor, d~tor
if you define these. The same goes for the other 4.
Does standard-compliant compiler only provide these 6 member functions
implicitly? Are there some others?

Yes to provide and no to others as far as i know.
Can't (only) these 6 member functions be derived from base (father)
class?

No. You can't derive from a ctor or d~tors. Thats why they can't be
"called". In other words, a derived class can't overide / redefine a base
class's ctor. Hence: invoked.
Does it mean that we can/have avoided most of the problems if we take
care of these functions.

It depends what the class is composed_of and inheriting_from and what state
you want the instance to be in upon construction. Is defining your own ctor
and d~tors often desireable? Yes, often enough. Let me throw a simple
example at you:

class BareNumber
{
int m_n;
public:
void setn(int n) { m_n = n; }
int getn() const { return m_n; }
};

Why let the compiler generate the ctor and d~tors when you have the option
to specifiy what a default object looks like (note the ctor's init list and
the disactivated copy ctor):

class Number
{
int m_n;
public:
Number(int n) : m_n(n) { }
~Number() { }
int getn() const { return m_n; }
private:
Number(const Number& r_copy); // disabled copy ctor
};

in both those cases, the following will compile. The getn() call returning
an unitialized value is *undefined behaviour* or UB.

int main(int argc, char* argv[])
{
BareNumber bn; // uninitialized member
cout << "instance bn holds a value of " << bn.getn(); // no-no
cout << endl;

bn.setn(3); // calling member function
cout << "instance bn holds a value of " << bn.getn();
cout << endl;

Number dn(22);
cout << "instance dn holds a value of " << dn.getn();
cout << endl;

BareNumber bn1 = bn; // ok, copy + =op
cout << "bn1 invokes copy ctor and = op, value = " << bn1.getn();
// Number dn1 = dn; // cannot access

return 0;
}

Perhaps someone might write an example involving base ctor invocation from a
derived ctor's init list. Have fun...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top