Re: a question about classes

From: Mike Tyndall (swtyndall_at_hotmail.com)
Date: 08/09/04


Date: Sun, 8 Aug 2004 17:44:48 -0500


<johnny@n0sq.us> wrote in message
news:0j8Rc.12119$cK.7661@newsread2.news.pas.earthlink.net...
> I'm not sure where to begin here but I'll start with showing you the code
I
> have questions about:
>
> class X
> {
> public:
> X(); //default constructor
> X(double iD, char iS); //explicit constructor

These constructors have no bodies. I'm going to assume that the first is
{ } and the second is { mD = iD; mS = iS; }

>
> X iF() const; //conversion method
>
> private:
> double mD;
> char mS;
> };
>
> X X::iF() const
> {
> switch(mS)
> {
> case 'F': return X(mD, 'F');
> case 'C': return X(mD * 2, 'F');
> case 'K': return X(mD * 3, 'F');
> }
> }
>
> My question is about the definition of <X iF() const>. If I understand
> correctly, when iF() is called, it, in turn, calls the explicit
constructor
> X(double, char) that passes different parameter values based on the value
> of mS. If so, doesn't calling the constructor result in mD and mS being
> modified? If so, then why is the keyword return being used? If I
understand
> correctly, constructors don't return values so how can the keyword return
> be appropriate here?

1) You defined the constructors X() and X(double iD, char iS).
2) In the switch block, you called the second constructor.
3) When a constructor is called, it creates a new object.
4) The function iF() const has a return value of X, meaning it returns an X
object to the caller. The body of the function returns a different new X
object based on the value of the calling object's mS member variable.
5) The constructor called in the switch block's first case creates a new X
object using the values in the constructor's arguments (this->mD, 'F') to
assign values to a new X object's member variables. The member variables
have the following values:
               mD == the mD of the calling X object.
               mS == 'F'
     This object is returned to the caller.

Here's an example, using your code (plus some alterations and comments to
help you see what's going on) and main().

//BEGIN CODE
//<iostream> needed for cout
#include <iostream>
using std::cout;

class X
{
        public:
                //these constructors had no body,
                //so I added them.
                X() { }
                //these are constructors. If you
                //call the one below, for example
                //like X(5.75424, 'B'), it creates
                //a new X object and assigns
                //values to that object's member
                //variables.
                X(double iD, char iS) {
                        mD = iD;
                        mS = iS;
                }
                X iF() const;
                //function to print variable values
                void printVals() const;
        private:
                double mD;
                char mS;
};

X X::iF() const
{
         printVals();
         switch(mS)
                {
                //X(mD, 'F') calls a constructor.
                //in the class declaration, you
                //declared and defined this
                //constructor. The other X()
                //are also constructor calls.
                case 'F': return X(mD, 'F');
                case 'C': return X(mD * 2, 'F');
                case 'K': return X(mD * 3, 'F');
                }
}

//body of printVals()
void X::printVals() const {
         cout << "Member variable values: "
                 << mD << " " << mS << "\n";
}

int main() {
        X x1(5.5, 'C');
/*
When x1 calls iF(), the function uses x1's
member variables in the switch statement.
Because mS has the value 'C', the second
case is executed. This calls the constructor
X(mD * 2, 'F'), which creates a new X
object. return then returns the object to the
caller. The new object's member variables
have the values 11.0 (mD * 2) and 'F'.
These are assigned to an X object called x2.
*/
        X x2 = x1.iF();
        x2.printVals();

        return 0;
}

//END CODE

Hope that explains it.

//mike tyndall



Relevant Pages

  • Re: Rules for constructors
    ... Alex Hunsley wrote: ... > constructor for A, the initialisation of the member variables in class B ... realising of GUI items from a constructor. ...
    (comp.lang.java.programmer)
  • Rules for constructors
    ... This is because the constructor of B calls super, and by ... the initialisation of the member variables in class B ... (or else they will be overridden causing chaos as I have experienced ... overridden methods should not access member variables that are ...
    (comp.lang.java.programmer)
  • Re: Rules for constructors
    ... > b) never call non-final methods from your constructor ... > c) overridden methods should not access member variables that are ... > from this class - should only access superclass member variables ... access any object state from these methods. ...
    (comp.lang.java.programmer)
  • Re: No use of initializer list
    ... "will not be used as member variables": ... compileable code (unless you are getting a compile error and the point ... this constructor, what x and y represent, about f, etc), so we can't ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Help with error
    ... > * a copy constructor ... > Test(const Test& Arg) ... > struct Coord ...
    (alt.comp.lang.learn.c-cpp)