Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about derived class member function call

33 views
Skip to first unread message

fl

unread,
Jun 15, 2015, 10:59:26 PM6/15/15
to
Hi,

When I run the following code, I am puzzled about

















...............
#include <iostream>
using namespace std;

struct BaseConstructor
{
BaseConstructor(int=0)
{}
};

class RealNumber;
class Complex;
class Number;
class Number
{
friend class RealNumber;
friend class Complex;
public:
Number ();
Number & operator = (const Number &n);
Number (const Number &n);
virtual ~Number();
virtual Number operator + (Number const &n) const;
void swap (Number &n) throw ();

static Number makeReal (double r);
static Number makeComplex (double rpart, double ipart);
protected:
Number (BaseConstructor);

private:
void redefine (Number *n);
virtual Number complexAdd (Number const &n) const;
virtual Number realAdd (Number const &n) const;

Number *rep;
short referenceCount;
};

class Complex : public Number
{
friend class RealNumber;
friend class Number;

Complex (double d, double e);
Complex (const Complex &c);
virtual ~Complex ();

virtual Number operator + (Number const &n) const;
virtual Number realAdd (Number const &n) const;
virtual Number complexAdd (Number const &n) const;
double rpart, ipart;
};

class RealNumber : public Number
{
friend class Complex;
friend class Number;

RealNumber (double r);
RealNumber (const RealNumber &r);
virtual ~RealNumber ();

virtual Number operator + (Number const &n) const;
virtual Number realAdd (Number const &n) const;
virtual Number complexAdd (Number const &n) const;

double val;
};

/// Used only by the letters.
Number::Number (BaseConstructor): rep (0), referenceCount (1)
{
cout << "rep(0), referenceCount(1)\n" << rep << ", " << referenceCount << endl;
}

/// Used by user and static factory functions.
Number::Number () : rep (0), referenceCount (0)
{}

/// Used by user and static factory functions.
Number::Number (const Number &n): rep (n.rep), referenceCount (0)
{
cout << "Constructing a Number using Number::Number\n";
if (n.rep)
n.rep->referenceCount++;
}

Number Number::makeReal (double r)
{
Number n;
n.redefine (new RealNumber (r));
return n;
}

Number Number::makeComplex (double rpart, double ipart)
{
Number n;
n.redefine (new Complex (rpart, ipart));
return n; // nCaller
}

int main (void)
{
Number::makeComplex (1, 2);
cout << "Finished\n";
return 0;
}

fl

unread,
Jun 15, 2015, 11:12:39 PM6/15/15
to
Hi,
Excuse me for accidentally post the incomplete message previously.
In main(), line

Number::makeComplex (1, 2);

calls

Number Number::makeComplex (double rpart, double ipart)

Then, it calls constructor:

Number::Number () : rep (0), referenceCount (0)

Next, it calls

Complex::Complex (double d, double e) : Number (BaseConstructor()),
rpart (d), ipart (e)
{
cout << "Constructing a Complex\n";
}

Next, it calls Complex constructor:
Complex::Complex (double d, double e) : Number (BaseConstructor()),
rpart (d), ipart (e)

Next, it calls member function:
void Number::redefine (Number *n)

My question comes here. When it runs line
return n; // nCaller

It calls constructor:
Number::Number (const Number &n): rep (n.rep), referenceCount (0)

I don't understand why it calls the above constructor.

For your reference, here is the console window message:


rep(0), referenceCount(1)
00000000, 1
Constructing a Complex
Constructing a Number using Number::Number



I think that I have not enought understanding on class inheritance.
Hopefully, the answer for this question can teach me some.
Can you help me out?

Thanks,

Melzzzzz

unread,
Jun 15, 2015, 11:14:10 PM6/15/15
to
On Mon, 15 Jun 2015 19:59:16 -0700 (PDT)
fl <rxj...@gmail.com> wrote:

> Hi,
>
> When I run the following code, I am puzzled about
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> ...............
> #include <iostream>
> using namespace std;
>
> struct BaseConstructor
> {
> BaseConstructor(int=0)
> {}
> };
>

I am puzzled buy this, too...

Richard

unread,
Jun 16, 2015, 10:18:46 AM6/16/15
to
[Please do not mail me a copy of your followup]

fl <rxj...@gmail.com> spake the secret code
<5008983f-f182-449b...@googlegroups.com> thusly:

>struct BaseConstructor
>{
> BaseConstructor(int=0)
> {}
>};

Didn't we already go over this in another thread?

It feels like a bunch of people are taking a summer course in C++ using
a confusing and poorly written book and the instructor has recommended
you come here for help.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
0 new messages